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


4

2 回答 2

1

如果它对其他人有帮助,对于我在 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 并强制服务(和相关任务)部署新图像。

于 2020-01-17T18:50:50.837 回答
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<-暂时试试这个

对比

$(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"
}
于 2019-12-03T22:05:56.220 回答