如何在GitLab CI中的其他任何内容之前先在PATH中设置docker-credential-ecr-login 关于apk陷阱的相关评论

我正在使用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

谢谢。

zyh810908 回答:如何在GitLab CI中的其他任何内容之前先在PATH中设置docker-credential-ecr-login 关于apk陷阱的相关评论

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

下面的模板文件.gitlab-ci.yml是根据OP的示例改编而成的,使用了我在Docker-in-Docker中建议的this other SO answer方法,该方法本身受GitLab CI doc dealing with dind的启发:

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的所谓的block style

上面的模板已经包含注释,但是必须是独立的:

  • 如果您的Bash命令包含双引号,则需要转义双引号,因为整个代码都被docker run … ""包围;
  • 您还需要转义局部Bash变量(请参见上面的\$PWD),否则这些变量将在运行docker run … "$IMAGE" /bin/bash -c "…"命令本身之前被解析。
  • 我用更有效的colon对应项替换了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的示例同时包含一些{{1 }}和apt命令…]

关于apk陷阱的相关评论

请注意以下脚本有错误:

set -e

即,改写:

set -e
command1 && command2
command3

或:

set -e
command1 ; command2
command3

要对此说服,您可以尝试运行:

set -e
( command1 && command2 )
command3
,

来自GitLab文档。为了与您的AWS账户进行交互,GitLab CI / CD管道要求在您的GitLab设置中的设置> CI / CD>变量下定义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,以更清楚地了解如何正确地进行操作。

,

使用gitlabRunner的docker executor模式时,我遇到了同样的问题。

SSH进入EC2实例表明docker-credential-ecr-login中存在/usr/bin/。要将其传递到容器,我必须将此软件包安装到gitlabRunner容器。

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启用docker

    sudo yum install amazon-ecr-credential-helper

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

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

    { “ credHelpers”: { “ aws_account_id.dkr.ecr.region.amazonaws.com”:“ ecr登录” } }

  • 源〜/ .bashrc

  • systemctl重新启动docker

  • 还从您的GitLab >> CI / CD >>变量中删除任何DOCKER_AUTH_CONFIG的引用

就这样

本文链接:https://www.f2er.com/2824480.html

大家都在问