我需要在 Docker 中作为代理执行 Jenkins 管道,
Docker 镜像位于 AWS ECR,
如何通过 AWS ECR 进行身份验证以为代理提取图像?
我需要在 Docker 中作为代理执行 Jenkins 管道,
Docker 镜像位于 AWS ECR,
如何通过 AWS ECR 进行身份验证以为代理提取图像?
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>'
}
}
要首先使用来自 AWS ECR 存储库的图像作为 jenkins 中的代理,您需要添加种类的凭证AWS Credentials
。现在只需在管道代码中的代理块中使用上面的代码。确保更换
<aws-account>
使用 AWS 账户 ID。
<ecr-repo>
带有 ECR 存储库名称
<tag>
带有您要使用的 ECR 图像标签。
<Jenkins Credential ID>
在 Jenkins 中保存凭据时获得的 Jenkins 凭据 ID。
us-west-2
替换为您的 ecr 回购区域
您可以使用https://<jenkins.url>/directive-generator/
为您生成此代码。
你可以试试这个:
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*"
}
}
意味着您需要在从 ECR 提取图像之前授权令牌,这意味着您还需要在 Jenkins 服务器上安装 AWS-CLI。最好的方法是分配角色并在您的管道中运行以下命令以获取授权令牌,如果它很复杂,请使用下面的 ECR 插件。
在它可以推送和拉取图像之前,Docker 客户端必须以 AWS 用户身份向 Amazon ECR 注册中心进行身份验证。AWS CLI get-login 命令为您提供身份验证凭证以传递给 Docker。有关详细信息,请参阅注册表身份验证。使用詹金斯/亚马逊+ECR
注意:对于基于 AWS 注册自动创建令牌,或者您可以在拉取之前在 jenkins 文件中运行此命令
$(aws ecr get-login --no-include-email --region us-west-2)
而对于 go 需要在 Docker 中作为代理执行 Jenkins 管道 Prefer this link。
根据此页面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
"""