1

我有一个使用 git-crypt 加密文件的仓库。我已将密钥导出到文件中。现在我在 gitlab 上使用默认的 docker 镜像构建模板来构建我的镜像。管道工作得很好。我只是不知道如何在构建期间“解锁”文件,以便图像具有明文文件供使用。管道构建如下所示:

docker-build:
  # Use the official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  # Default branch leaves tag empty (= latest tag)
  # All other branches are tagged with the escaped branch name (commit ref slug)
  script:
    - |
      if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
        tag=""
        echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
      else
        tag=":$CI_COMMIT_REF_SLUG"
        echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
      fi
      echo $CI_REGISTRY_IMAGE${tag}
    - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
    - docker push "$CI_REGISTRY_IMAGE${tag}"
  # Run this job in a branch where a Dockerfile exists
  rules:
    - if: $CI_COMMIT_BRANCH
      exists:
        - Dockerfile

我只是不确定解锁发生的地点或时间。它是否发生在 Dockerfile 或此构建过程中?我用谷歌搜索过,并认为这是一个常见的问题,但到目前为止还没有。

提前感谢您提供的任何帮助或链接。

布拉德

4

2 回答 2

1

我认为没有人回答的原因是因为几乎不可能回答。如何使用跑步者有很多排列。所以我将分享我的解决方案。

我必须意识到的是,运行 docker 映像的操作系统映像中没有安装 git-crypt。所以这是我的第一个任务。

- apk add git-crypt

现在二进制文件在构建图像中,我需要以某种方式将解锁密钥放入图像中。值得庆幸的是,gitlab 有可以在构建中使用的项目变量。但是,他们目前没有办法上传解锁密钥所在的二进制文件。那么该怎么办。那么你base64编码它。

base64 binaryfile.key > baseecodeded.key

您现在可以将没有 cr/lf 的文本粘贴到 gitlab 项目变量中,并确保将其设置为 File not text。然后您可以将变量解码回文件并在您的构建中使用它。

- cat "$CRYPT_KEY" | base64 -d > key-file
- git-crypt unlock key-file

最终的 .gitlab-ci.yml 如下。我要做的一件事是将其更改为跳过创建文件..并将解码的变量直接通过管道传输到 git-crypt unlock。

docker-build:
  # Use the official docker image.
  image: docker:latest
  stage: build
  tags:
    - "docker"
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - apk add git-crypt
    - cat "$CRYPT_KEY" | base64 -d > key-file
    - git-crypt unlock key-file
  script:
    - |
      if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
        tag=""
        echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
      fi

    - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
    - docker push "$CI_REGISTRY_IMAGE${tag}"

  # Run this job in a branch where a Dockerfile exists
  rules:
    - if: "$CI_COMMIT_BRANCH =~ /^dev/"
      when: never
    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"
      exists:
        - Dockerfile
于 2021-06-29T23:46:38.580 回答
0

Couldn't you just use the File variable type from Gitlab's CI/CD with a symmetric key?.

于 2021-08-21T16:17:43.667 回答