我有一个包含加密文件的repo,使用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中还是在这个构建过程中?我已经用谷歌搜索过了,我本以为这是一个常见的问题,但到目前为止什么都没有。
提前感谢您所能提供的任何帮助或链接。
布拉德
发布于 2021-06-30 07:46:39
我认为没有人回答的原因是因为几乎不可能回答。关于如何使用runners,有很多种排列方式。因此,我将分享我的解决方案。
我必须意识到的是,运行docker镜像的操作系统镜像中并没有安装git-crypt。这就是我的第一个任务。
- apk add git-crypt
现在二进制文件已经在建筑镜像中,我需要以某种方式将解锁密钥放入镜像中。值得庆幸的是,gitlab有项目变量可供您在构建中使用。然而,他们目前还没有上传二进制文件的方法,解锁密钥就是。那么该怎么做呢?好的,你可以对它进行base64编码。
base64 binaryfile.key > baseecodeded.key
您现在可以将不带cr/lf的文本粘贴到gitlab项目变量中,并确保将其设置为File而不是text。然后,您可以将变量解码回一个文件,并在构建中使用它。
- cat "$CRYPT_KEY" | base64 -d > key-file
- git-crypt unlock key-file最后的.gitlab-ci.yml如下所示。我要做的一件事是将其更改为跳过创建文件。只需通过管道将解码后的变量直接发送到git-crypt解锁。
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-08-21 16:17:44
您不能只使用Gitlab的CI/CD中的File variable type和对称密钥吗?
https://stackoverflow.com/questions/68153984
复制相似问题