如何在 GitLab CI 中先在 PATH 中设置 docker-credential-ecr-login



我正在使用 AWS ECR 托管私有 Dockerfile 映像,我想在 GitLab CI 中使用它。

根据文档,我需要设置 docker-credential-ecr-login 来获取私有映像,但我不知道如何在其他任何事情之前做到这一点。这是我的 .gitlab-ci 文件:

image: 0222822883.dkr.ecr.us-east-1.amazonaws.com/api-build:latest
tests:
stage: test
before_script:
- echo "before_script"
- apt install amazon-ecr-credential-helper
- apk add --no-cache curl jq python py-pip
- pip install awscli
script:
- echo "script"
- bundle install
- bundle exec rspec
allow_failure: true # for now as we do not have tests

谢谢。

我确认 GitLab CI 中尚不可用相关功能;但是我最近看到可以实现一种通用解决方法,以在从私有 Docker 映像获取的容器中运行专用 CI 脚本。

下面.gitlab-ci.yml的模板文件改编自OP的示例,使用了我在另一个SO答案中建议的Docker-in-Docker方法,该方法本身受到处理dind的GitLab CI文档的启发:

stages:
- test
variables:
IMAGE: "0222822883.dkr.ecr.us-east-1.amazonaws.com/api-build:latest"
REGION: "ap-northeast-1"
tests:
stage: test
image: docker:latest
services:
- docker:dind
variables:
# GIT_STRATEGY: none  # uncomment if "git clone" is unneeded for this job
before_script:
- ': before_script'
- apt install amazon-ecr-credential-helper
- apk add --no-cache curl jq python py-pip
- pip install awscli
- $(aws ecr get-login --no-include-email --region "$REGION")
- docker pull "$IMAGE"
script:
- ': script'
- |
docker run --rm -v "$PWD:/build" -w /build "$IMAGE" /bin/bash -c "
export PS4='+ e[33;1m($CI_JOB_NAME @ line $LINENO) $e[0m '  # optional
set -ex
## TODO insert your multi-line shell script here ##
echo "One comment"  # quotes must be escaped here
: A better comment
echo $PWD  # interpolated outside the container
echo $PWD  # interpolated inside the container
bundle install
bundle exec rspec
## (cont'd) ##
"
- ': done'
allow_failure: true # for now as we do not have tests

此示例假定 Docker$IMAGE包含/bin/bash二进制文件,并依赖于所谓的 YAML 块样式。

上面的模板已经包含注释,但要自包含:

  • 如果您的 Bash 命令包含双引号,则需要对其进行转义,因为整个代码被docker run … ""包围;
  • 您还需要转义本地 Bash 变量(参见上面的$PWD(,否则这些变量将在运行docker run … "$IMAGE" /bin/bash -c "…"命令本身之前解析。
  • 我用更有效的冒号替换了echo "stuff"左右的命令:
    set -x
    : stuff
    : note that these three shell commands do nothing
    : but printing their args thanks to the -x option.
    

[欢迎反馈,因为我无法直接测试此配置(我不是 AWS ECR 用户(,但我对 OP 的示例同时包含一些aptapk命令的事实感到困惑......

关于set -e陷阱的相关评论

请注意,以下脚本有问题:

set -e
command1 && command2
command3

即,改为写:

set -e
command1 ; command2
command3

或:

set -e
( command1 && command2 )
command3

要确信这一点,您可以尝试运行:

bash -e -c 'false && true; echo $?; echo this should not be run'
→ 1
→ this should not be run
bash -e -c 'false; true; echo $?; echo this should not be run'
bash -e -c '( false && true ); echo $?; echo this should not be run'

来自GitLab文档。为了与您的 AWS 账户进行交互,GitLab CI/CD 管道需要在 GitLab 设置中的 CI/CD>变量>下的 GitLab 设置中定义AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY。然后添加到之前的脚本中:

image: 0222822883.dkr.ecr.us-east-1.amazonaws.com/api-build:latest
tests:
stage: test
before_script:
- echo "before_script"
- apt install amazon-ecr-credential-helper
- apk add --no-cache curl jq python py-pip
- pip install awscli
- $( aws ecr get-login --no-include-email )
script:
- echo "script"
- bundle install
- bundle exec rspec
allow_failure: true # for now as we do not have tests

另外,你有一个错别字是awscli,而不是awsclir。然后添加构建、测试并相应地推送。

我认为在这种情况下您有某种逻辑错误。 生成配置中的image是 CI 脚本运行器映像,而不是生成和部署的映像。

我认为您在任何情况下都不必使用它,因为它只是一个具有实用程序和与GitLab CI等连接的图像。映像通常不应具有项目的任何依赖项。

请查看 https://gist.github.com/jlis/4bc528041b9661ae6594c63cd2ef673c 这样的示例,以更清楚地了解如何以正确的方式进行操作。

我在使用 gitlab 运行器的 docker 执行器模式时遇到了同样的问题。

通过 SSH 进入 EC2 实例,显示docker-credential-ecr-login存在于/usr/bin/中。为了将其传递给容器,我必须将此包挂载到 gitlab 运行器容器中。

gitlab-runner register -n 
--url '${gitlab_url}' 
--registration-token '${registration_token}' 
--template-config /tmp/gitlab_runner.template.toml 
--executor docker 
--tag-list '${runner_name}' 
--description 'gitlab runner for ${runner_name}' 
--docker-privileged 
--docker-image "alpine" 
--docker-disable-cache=true 
--docker-volumes "/var/run/docker.sock:/var/run/docker.sock" 
--docker-volumes "/cache" 
--docker-volumes "/usr/bin/docker-credential-ecr-login:/usr/bin/docker-credential-ecr-login" 
--docker-volumes "/home/gitlab-runner/.docker:/root/.docker"

有关此线程的更多信息:https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1583#note_375018948

我们有一个类似的设置,我们需要基于托管在 ECR 上的映像运行 CI 作业。 要遵循的步骤:-

  • 请在此处遵循本指南>> https://github.com/awslabs/amazon-ecr-credential-helper

  • 上述链接的要点是,如果您使用的是"Amazon Linux 2">

    sudo amazon-linux-extras enable docker

    Sudo yum install amazon-ecr-credential-helper

  • 在VI编辑器的gitlab运行器上打开~/.docker/config.json

  • 将此代码粘贴到 ~/.docker/config.json 中

    { "信任助手": { "aws_account_id.dkr.ecr.region.amazonaws.com": "ecr-login" } }

  • 来源 ~/.巴什尔克

  • 系统CTL重启码头工人

  • 同时从 GitLab>>CI/CD 中删除DOCKER_AUTH_CONFIG的任何引用>>

    变量

就是这样

最新更新