CodeBuild 使用 ACL 将构建工件上传到 S3

CodeBuild upload build artifact to S3 with ACL

我有 2 个 AWS 账户。让我们说 A 和 B.

账户 A 使用 CodeBuild 构建工件并将其上传到 B 拥有的 S3 存储桶。B 账户已为该存储桶设置 ACL 权限,以便向 A 授予写入权限。

工件文件已成功上传至S3 存储桶。但是,B 帐户对该文件没有任何权限,因为该文件属于 A。

账户 A 可以通过 运行ning

更改所有权
aws s3api put-object-acl --bucket bucket-name --key key-name --acl bucket-owner-full-control

但这必须在每次从 A 帐户构建后手动 运行。如何通过 CodeBuild 程序向账户 B 授予权限?或者帐户 B 如何覆盖此所有权权限错误。

CodeBuild 使用 web-hooks 自动启动,我的构建规范是这样的:

 version: 0.2
 env:
 phases:
  install:
    runtime-versions:
      java: openjdk8
    commands:
      - echo Entered the install phase...
  build:
    commands:
      - echo Entered the build phase...
  post_build:
    commands:
      - echo Entered the post_build phase...
artifacts:
  files:
    - 'myFile.txt'

我是使用构建阶段的 aws cli 命令完成的。

version: 0.2
phases:
  build:
    commands:
      - mvn install...
      - aws s3 cp my-file s3://bucketName --acl bucket-owner-full-control

我正在使用构建阶段,因为 post_build 即使构建不成功也会执行。

编辑:使用示例更新了答案。

CodeBuild 本身不支持将工件写入不同的帐户,因为它没有在跨帐户对象上设置正确的 ACL。这就是在 CodePipeline 文档中提出以下限制的原因:

Cross-account actions are not supported for the following action types:

  • Jenkins build actions
  • CodeBuild build or test actions

https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-create-cross-account.html

一种解决方法是在 CodeBuild 中自行设置工件的 ACL:

version: 0.2
phases:
  post_build:
    commands:
      - aws s3api list-objects --bucket testingbucket --prefix CFNtest/OutputArti >> $CODEBUILD_SRC_DIR/objects.json
      - |
        for i in $(jq -r '.Contents[]|.Key' $CODEBUILD_SRC_DIR/objects.json); do
          echo $i
          aws s3api put-object-acl --bucket testingbucket --key $i --acl bucket-owner-full-control
        done