CodeBuild 错误退出状态 127 - 文件名太长

codebuild error exit status 127 - file name too long

提问人:1ups_ 提问时间:11/15/2023 更新时间:11/15/2023 访问量:61

问:

我对 aws 不是很感兴趣,我正在尝试使用 codebuild 构建一个 docker 映像,然后将其发送到 ECR 存储库。

这是我的buildspec.yml文件:

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - $(aws ecr get-login-password --region $AWS_DEFAULT_REGION)

      - REPOSITORY_URI= {URI}
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F":" '{print $2}')

  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 on `date`
      - echo Pushing the Docker images...
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - echo Writing image definitions file...
      - printf '[{"name":"{name}","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
      - cat imagedefinitions.json

artifacts:
  files: imagedefinitions.json

这是我得到的错误:

[Container] 2023/11/15 10:40:59.168094 Running command $(aws ecr get-login-password --region $AWS_DEFAULT_REGION)
/codebuild/output/tmp/script.sh: 4: {long base64 encoded string}: File name too long

[Container] 2023/11/15 10:40:59.876595 Command did not exit successfully $(aws ecr get-login-password --region $AWS_DEFAULT_REGION) exit status 127
[Container] 2023/11/15 10:40:59.879980 Phase complete: PRE_BUILD State: FAILED
[Container] 2023/11/15 10:40:59.879996 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: $(aws ecr get-login-password --region $AWS_DEFAULT_REGION). Reason: exit status 127

底数 64 字符串解密后会给出:

{
   "payload":"/{long string that looks randomly generated}",
   "datakey":"{long string but not as long as the previous one}",
   "version":"2",
   "type":"DATA_KEY",
   "expiration":1700088059
}

我已将“AmazonEC2ContainerRegistryFullAccess”和“AmazonEC2ContainerRegistryPowerUser”策略以及默认 basepolicy 附加到与我的构建项目关联的服务角色

我完全迷路了,因为我在互联网上没有找到有关该错误的任何信息,而 chatgpt 似乎也无济于事

amazon-web-services cicd aws-code构建 amazon-ecr

评论


答:

2赞 Mark B 11/15/2023 #1

您没有捕获 ECR 登录密码,甚至没有尝试将其传递给 Docker 进行登录。相反,您正在生成 ECR 登录密码,然后告诉 Linux “运行”密码,就像它是文件的路径一样。

您需要在环境变量中捕获密码值:

- ECR_PASSWORD=$(aws ecr get-login-password --region $AWS_DEFAULT_REGION)

您还需要获取 AWS 账户 ID,以便在 docker login 命令中使用:

- AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)

然后你需要实际将密码发送到Docker,以实际使用它来登录

- echo -n ${ECR_PASSWORD} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com

一起:

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - ECR_PASSWORD=$(aws ecr get-login-password --region $AWS_DEFAULT_REGION)
      - AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
      - echo -n ${ECR_PASSWORD} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com

      - REPOSITORY_URI= {URI}
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F":" '{print $2}')

评论

0赞 1ups_ 11/15/2023
非常感谢,这很有效!:D正如我所说,我对 aws 不是很感兴趣,所以谢谢你帮助我:p
0赞 Paolo 11/15/2023
不要忘记双引号你的变量。另外,什么是?{URI}
1赞 Mark B 11/15/2023
@Paolo很好的捕获,我更新了答案。至于事情,我只是从原始问题中复制了该部分,以显示我更新的代码将去哪里。不过,这绝对看起来像是构建中的另一个错误。echo -n{URI}
1赞 Mark B 11/15/2023
@1ups_这甚至不是 AWS 特定的东西,它只是你缺少的基本 Bash/Linux 脚本内容。
1赞 Mark B 11/15/2023
@Paolo是的,可能是,但问题中没有足够的信息来确定。整个事情只是将一个环境变量的值复制到另一个环境变量,无论如何这似乎毫无意义。