ECR 镜像推送 AWS CodeBuild 问题

问题描述 投票:0回答:3

COMMAND_EXECUTION_ERROR:执行命令时出错:$(aws ecr get-login --no-include-email --region us-east-1)。原因:退出状态127

下面是我的 buildspec.yml 文件

version: 0.2
phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - $(aws ecr get-login --region ***-east-*)
      - REPOSITORY_URI=***********.dkr.ecr.***-east-*.amazonaws.com/repositoryname
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t $REPOSITORY_URI:latest .
      - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed
      - echo Pushing the Docker images...
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - echo Writing definitions file...
      - printf '[{"name":"project-container","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > taskdefinition.json
artifacts:
    files: taskdefinition.json


amazon-web-services aws-codebuild amazon-ecr
3个回答
2
投票

以防它对其他人有帮助,因为我在 CodeBuild 执行的构建脚本中所做的工作。这些是我必须添加的 IAM 权限(在遇到错误时一一找到它们)。

{
    "Action": [
        "ecr:GetAuthorizationToken",
        "ecr:DescribeRepositories",
        "ecr:CreateRepository",
        "ecr:InitiateLayerUpload",
        "ecr:UploadLayerPart",
        "ecr:CompleteLayerUpload",
        "ecr:BatchCheckLayerAvailability",
        "ecr:PutImage",
        "ecs:UpdateService"
    ],
    "Resource": "*",
    "Effect": "Allow"
}   ' 

我确信,如果您正在做我在构建中没有做的事情,可能需要更多权限。我正在推动 ECR 并强制服务(以及相关任务)部署新映像。


0
投票

您的帖子细节不一致,是故意的吗?如果不是,则可能是问题的原因。你的代码片段说:

$(aws ecr get-login --region ***-east-*)

也许你故意编辑了该区域(顺便说一句,这有什么意义?)但为什么它缺少

--no-include-email
?在你的帖子的更高处,你确实提到了
--no-include-email
,所以我知道你已经意识到了。

在子 shell 之外运行进程以获得更好的日志

为了排除故障,不要在子 shell 中运行它(例如

$(my command)
),而是尝试将子 shell 取出来运行,这样可以获得更好的输出。在此报告结果,以便我们解决您遇到的错误。

aws ecr get-login --no-include-email --region us-east-1
<- try this temporarily

$(aws ecr get-login --no-include-email --region us-east-1)

您是否创建了具有 ECR 权限的 IAM 策略以供 CodeBuild 使用?

这非常重要。 CodeBuild 需要代表您访问 ECR 的权限。这是我在这篇博客文章中找到的一个例子。它可能需要根据您的需求进行调整。 http://beta.awsdocs.com/services/code_build/build_docker_images/

{
    "Action": [
        "ecr:BatchCheckLayerAvailability",
        "ecr:CompleteLayerUpload",
        "ecr:GetAuthorizationToken",
        "ecr:InitiateLayerUpload",
        "ecr:PutImage",
        "ecr:UploadLayerPart"
    ],
    "Resource": "*",
    "Effect": "Allow"
}

0
投票

这有效

aws ecr get-login-password --region **-****-* | docker login --username AWS --password-stdin https://*****.dkr.ecr.us-east-2.amazonaws.com
© www.soinside.com 2019 - 2024. All rights reserved.