我们将 docker 镜像仓库由 harbor 改成 AWS ECR,如何推送镜像?

Retrieve an authentication token and authenticate your Docker client to your registry. Use the AWS CLI:

aws ecr get-login-password --region ap-east-1 | docker login --username AWS --password-stdin <aws account>.dkr.ecr.<region>.amazonaws.com

如此我们不仅要改变原有 DSL 结构,并且还需要手动安装 aws cli , 还有安全凭证泄露的风险。

我们在不改变原有结构的情况下,同时兼容两种镜像仓库的管理呢?

Amazon ECR 插件实现了 Docker Token 生成器,将 Amazon 凭证转换为 Jenkins 的 API,该 API 由(大多数)所有 Docker 相关插件使用。

使用 Docker Pipeline Plugin 时,为了获取 ECR 登录凭证,您必须使用 ecr 提供程序前缀:

script {
  docker.withRegistry("https://${REGISTRY}", "ecr:ap-east-1:aws-credentials") {
    def img = docker.build("${REGISTRY}/${APP_ID}:${BuildTag}")
    img.push()
  }
}

IAM 权限

执行 docker push 到 ecr 的绝对最小权限集

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchGetImage",
                "ecr:BatchCheckLayerAvailability",
                "ecr:CompleteLayerUpload",
                "ecr:DescribeImages",
                "ecr:DescribeRepositories",
                "ecr:GetDownloadUrlForLayer",
                "ecr:InitiateLayerUpload",
                "ecr:ListImages",
                "ecr:PutImage",
                "ecr:UploadLayerPart"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "ecr:GetAuthorizationToken",
            "Resource": "*"
        }
    ]
}
Last modified: March 27, 2025

Author

Comments

Write a Reply or Comment

Your email address will not be published.