[英]buildspec.yml to push to ECR is throwing this error "Command did not exit successfully $(aws ecr get-login --no-include-email --region us-east-1)"
我正在学习使用 CodeBuild 将构建推送到 ECR 的代码管道。 下面是我的 buildspec.yml 文件和 Codebuild 日志中的错误。 谁能阐明我做错了什么? 提前致谢。
构建规范.yml
版本:0.2
阶段:
预构建:
commands:
- echo Logging in to Amazon ECR.....
- aws --version
- $(aws ecr get-login --no-include-email --region us-east-1)
- REPOSITORY_URI=989066xxxxxx.dkr.ecr.us-east-1.amazonaws.com/ecs-cicd-nginx
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
建造:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $REPOSITORY_URI:$IMAGE_TAG .
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"ecs-cicd-nginx","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
工件:文件:imagedefinitions.json
错误日志: [容器] 2021/07/13 11:13:22 运行命令 aws --version aws-cli/2.1.38 Python/3.8.8 Linux/4.14.225-121.362.amzn1.x86_64 exec-env/AWS_ECS_EC2 exe/x86_64.ubuntu.20 提示/关闭
[容器] 2021/07/13 11:13:26 运行命令 $(aws ecr get-login --no-include-email --region us-east-1)
用法:aws [options] [...] [parameters] 要查看帮助文本,您可以运行:
aws 帮助 aws 帮助 aws 帮助
aws:错误:参数操作:无效的选择,有效的选择是:
批量检查层可用性 | 批量删除图像
批量获取图像 | 完整层上传
创建存储库 | 删除生命周期策略
删除注册表策略 | 删除存储库
删除存储库策略 | 描述图像扫描结果
描述图像 | 描述注册表
描述存储库 | 获取授权令牌
get-download-url-for-layer | 获取生命周期策略
获取生命周期策略预览 | 获取注册表策略
获取存储库策略 | 启动层上传
列表图像 | 资源标签列表
放图像 | 放置图像扫描配置
放置图像标签可变性 | 放置生命周期策略
put-registry-policy | 放置复制配置
设置存储库策略 | 开始图像扫描
开始生命周期策略预览 | 标签资源
取消标记资源 | 上传层部分
获取登录密码 | 等待
帮助
[容器] 2021/07/13 11:13:26 命令未成功退出 $(aws ecr get-login --no-include-email --region us-east-1) 退出状态 252 [容器] 2021/07 /13 11:13:26 阶段完成:PRE_BUILD State:失败 [容器] 2021/07/13 11:13:26 阶段上下文状态代码:COMMAND_EXECUTION_ERROR 消息:执行命令时出错:$(aws ecr get-login --no -include-email --region us-east-1)。 原因:退出状态252
从CLI 文档开始, get-login
在 CLI 的 2.x 版中已弃用。 它在最新版本中不存在。
请改用get-login-password
。
下面是CodeBuild 文档中的一个示例: aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
ecr aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
对我来说,问题是我使用$()和get-login-password命令
- $(aws ecr get-login-password --region REGION | docker login --username AWS --password-stdin DOCKER_ID.dkr.ecr.REGION.amazonaws.com)
删除$()工作:
- aws ecr get-login-password --region REGION | docker login --username AWS --password-stdin DOCKER_ID.dkr.ecr.REGION.amazonaws.com
这里的问题可能是codebuild权限,角色应该像下面这样。 注意ecr:GetAuthorizationToken ,这是您缺少的。 如果没有此权限,您将无法登录 ECR。
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: "PushImageToEcr"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ecr:BatchGetImage
- ecr:BatchCheckLayerAvailability
- ecr:CompleteLayerUpload
- ecr:GetDownloadUrlForLayer
- ecr:InitiateLayerUpload
- ecr:PutImage
- ecr:UploadLayerPart
- ecr:GetAuthorizationToken
Resource: "*"
- PolicyName: "CodeBuildLogsRole"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*"
- PolicyName: "GetAndPutArtifacts"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- s3:GetObject
- s3:PutObject
- s3:ListBucket
Resource:
- !GetAtt ArtifactBucket.Arn
- !Sub ${ArtifactBucket.Arn}/*
看起来像 IAM 权限问题。 检查角色所需的权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.