我有一个带有加密文件的存储库,使用 git-crypt。我已将 key 导出到文件中。现在我在 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 中还是在这个构建过程中发生的?我用谷歌搜索过,本以为这是一个常见问题,但到目前为止什么都没有。
提前感谢您提供的任何帮助或链接。
布 pull 德
最佳答案
我认为之所以没有人回答是因为几乎不可能回答。如何使用运行者有很多排列组合。所以我将分享我的解决方案。
我必须意识到的是,运行 docker 镜像的操作系统镜像没有安装 git-crypt。所以这是我的首要任务。
- apk 添加 git-crypt
既然二进制文件在构建镜像中,我需要以某种方式将解锁 key 放入镜像中。值得庆幸的是,gitlab 有您可以在构建中使用的项目变量。但是,他们目前没有办法上传二进制文件,而解锁 key 就是这个文件。那么该怎么办。那么你对其进行 base64 编码。
base64 binaryfile.key > baseecodeded.key
您现在可以将没有 cr/lf 的文本粘贴到 gitlab 项目变量中,并确保将其设置为文件而不是文本。然后您可以将变量解码回文件并在您的构建中使用它。
- 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
关于git - 如何在 gitlab ci/cd 管道上的 docker build 期间解锁 git-crypt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68153984/