【问题标题】:Jenkins pipeline Docker agent from AWS ECR来自 AWS ECR 的 Jenkins 管道 Docker 代理
【发布时间】:2020-01-25 09:26:55
【问题描述】:

我需要在 Docker 中作为代理执行 Jenkins 管道,

Docker 镜像位于 AWS ECR,

如何通过 AWS ECR 进行身份验证以为代理提取图像?

【问题讨论】:

    标签: jenkins-pipeline aws-ecr


    【解决方案1】:
    agent {
      docker {
        alwaysPull true
        image '<aws-account-Id>.dkr.ecr.us-west-2.amazonaws.com/<ecr-repo>:<tag>'
        registryUrl 'https://<aws-account-Id>.dkr.ecr.us-west-2.amazonaws.com'
        registryCredentialsId 'ecr:us-west-2:<Jenkins Credential ID>'
      }
    }
    

    要在 jenkins 中使用来自 AWS ECR 存储库的图像作为代理,首先您需要添加类型的凭证AWS Credentials。 现在只需在管道代码中的代理块中使用上面的代码。 确保更换

    1. &lt;aws-account&gt; 使用 AWS 账户 ID。

    2. &lt;ecr-repo&gt; 带有 ECR 存储库名称

    3. &lt;tag&gt; 带有您要使用的 ECR 图像标签。

    4. &lt;Jenkins Credential ID&gt; 使用 Jenkins 凭据 您在 Jenkins 中保存凭据时获得的 ID。

    5. us-west-2 替换为您的 ecr repo 区域

    您可以使用https://&lt;jenkins.url&gt;/directive-generator/ 为您生成此代码。

    【讨论】:

      【解决方案2】:

      你可以试试这个:

          agent { 
              docker { 
                  label "buildDockerNode"
                  image "nodejs10-test-v1"
                  alwaysPull true
                  registryUrl "*aws_account_id*.dkr.ecr.us-west-2.amazonaws.com/*project*"
                  registryCredentialsId "ecr:us-west-2:*cred_id*"
              }
          }
      

      【讨论】:

        【解决方案3】:

        根据此页面https://aws.amazon.com/blogs/compute/authenticating-amazon-ecr-repositories-for-docker-cli-with-credential-helper/ 应该可以使用以下内容:

        sh """#!/bin/bash
              docker login -u=${USER} -p=${PASS} https://aws_account_id.dkr.ecr.us-east-1.amazonaws.com
        """
        

        【讨论】:

        • 我对 Jenkins 管道比较陌生,但是,如果这是选择的解决方案,我很想知道它适合管道的什么位置,以便利用基于 Docker 的 agent正如 minhluantran017 和 Sourabh 在他们的回答中所描述的那样。
        【解决方案4】:

        意味着您需要在从 ECR 提取映像之前获得授权令牌,这意味着您还需要在 Jenkins 服务器上安装 AWS-CLI。最好的方法是分配角色并在您的管道中运行以下命令以获取授权令牌,如果它很复杂,请使用下面的 ECR 插件。

        在它可以推送和拉取图像之前,Docker 客户端必须以 AWS 用户身份向 Amazon ECR 注册中心进行身份验证。 AWS CLI get-login 命令为您提供身份验证凭证以传递给 Docker。有关详细信息,请参阅Registry 身份验证。 使用JENKINS/Amazon+ECR

        注意: 用于基于 AWS 注册自动创建令牌,或者您可以在拉取之前在 jenkins 文件中运行此命令

        $(aws ecr get-login --no-include-email --region us-west-2)
        

        并且对于 go 需要在 Docker 中作为代理执行 Jenkins 管道 更喜欢这个link

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-03-23
          • 2019-03-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多