AWS CodeBuild错误:第2行的预期阶段为非空

问题描述 投票:1回答:1

我正在尝试在AWS CodeBuild上部署Angular 8应用程序。但我收到此错误:DOWNLOAD_SOURCE Failed YAML_FILE_ERROR: Expected Phases to be non-empty at line 2

以及构建日志事件中的ACCESS DENIED错误,尽管在创建S3存储桶时我授予了所有公共访问权限。 COMMAND_EXECUTION_ERROR: Error while executing command: aws s3 cp dist s3://${S3_BUCKET} --recursive. Reason: exit status 1

我像here所述向我的S3存储桶添加了访问策略,但这没有帮助。

我的buildspec.yml文件如下所示,阶段列表不为空:

version: 0.1
phases:
  install:
    commands:
      - npm install -g @angular/cli
  build:
    commands:
      - npm install
      - ng build --prod --configuration=${BUILD_ENV}
  post_build:
    commands:
      - aws s3 cp dist s3://${S3_BUCKET} --recursive
      - aws configure set preview.cloudfront true
      - echo ${S3_BUCKET}
artifacts:
  files:
    - '**/*'
base-directory: 'dist*'
discard-paths: yes

我以前从未使用过YAML,在这里我做错了什么?

以下是构建日志事件:

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Entering phase PRE_BUILD

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Phase complete: PRE_BUILD State: SUCCEEDED

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Phase context status code: Message:

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Entering phase BUILD

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Running command npm install

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00
> [email protected] postinstall /codebuild/output/src970973936/src/node_modules/core-js

2020-06-13T10:13:07.379+02:00
> node -e "try{require('./postinstall')}catch(e){}"

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00
[96mThank you for using core-js ([94m https://github.com/zloirock/core-js [96m) for polyfilling     
JavaScript standard library![0m

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00
[96mThe project needs your help! Please consider supporting of core-js on Open Collective or 
Patreon: [0m

2020-06-13T10:13:07.379+02:00
[96m>[94m https://opencollective.com/core-js [0m

2020-06-13T10:13:07.379+02:00
[96m>[94m https://www.patreon.com/zloirock [0m

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00 
[96mAlso, the author of core-js ([94m https://github.com/zloirock [96m) is looking for a good 
job -)[0m

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00
> @angular/[email protected] postinstall /codebuild/output/src970973936/src/node_modules/@angular/cli

2020-06-13T10:13:07.379+02:00
> node ./bin/postinstall/script.js

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:09.415+02:00
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/webpack-dev-server
/node_modules/fsevents):

2020-06-13T10:13:09.415+02:00
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted 
{"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

2020-06-13T10:13:09.415+02:00
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/watchpack/node_modules
/fsevents):

2020-06-13T10:13:09.415+02:00
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted     
{"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

2020-06-13T10:13:09.415+02:00
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):

2020-06-13T10:13:09.415+02:00
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted    
{"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

2020-06-13T10:13:09.415+02:00

2020-06-13T10:13:09.415+02:00
 added 1413 packages from 1124 contributors and audited 1418 packages in 22.639s

2020-06-13T10:13:09.415+02:00

2020-06-13T10:13:09.415+02:00
run `npm fund` for details

2020-06-13T10:13:09.415+02:00

2020-06-13T10:13:09.415+02:00
found 5 vulnerabilities (3 low, 2 high)

2020-06-13T10:13:09.415+02:00
run `npm audit fix` to fix them, or `npm audit` for details

2020-06-13T10:13:09.415+02:00

2020-06-13T10:13:09.415+02:00
[Container] 2020/06/13 08:13:09 Running command ng build --prod --configuration=${BUILD_ENV}

2020-06-13T10:13:15.474+02:00
Compiling @angular/animations : es2015 as esm2015

2020-06-13T10:13:15.474+02:00
Compiling @angular/core : es2015 as esm2015

2020-06-13T10:13:19.504+02:00
Compiling @angular/animations/browser : es2015 as esm2015

2020-06-13T10:13:21.526+02:00
Compiling @angular/animations/browser/testing : es2015 as esm2015

2020-06-13T10:13:21.526+02:00
Compiling @angular/common : es2015 as esm2015

2020-06-13T10:13:23.553+02:00
Compiling @angular/common/http : es2015 as esm2015

2020-06-13T10:13:23.553+02:00
Compiling @angular/common/http/testing : es2015 as esm2015

2020-06-13T10:13:25.579+02:00
Compiling @angular/forms : es2015 as esm2015

2020-06-13T10:13:27.607+02:00
Compiling @angular/platform-browser : es2015 as esm2015

2020-06-13T10:13:27.607+02:00
Compiling @angular/platform-browser/animations : es2015 as esm2015

2020-06-13T10:13:29.635+02:00
Compiling @angular/core/testing : es2015 as esm2015

2020-06-13T10:13:29.635+02:00
Compiling @angular/platform-browser-dynamic : es2015 as esm2015

2020-06-13T10:13:31.660+02:00
Compiling @angular/platform-browser/testing : es2015 as esm2015

2020-06-13T10:13:31.660+02:00
Compiling @angular/compiler/testing : es2015 as esm2015

2020-06-13T10:13:31.660+02:00
Compiling @angular/platform-browser-dynamic/testing : es2015 as esm2015

2020-06-13T10:13:33.685+02:00
Compiling @angular/common/testing : es2015 as esm2015

2020-06-13T10:13:33.685+02:00
Compiling @angular/router : es2015 as esm2015

2020-06-13T10:13:35.712+02:00
Compiling @angular/router/testing : es2015 as esm2015

2020-06-13T10:14:09.752+02:00
Generating ES5 bundles for differential loading...

2020-06-13T10:14:23.800+02:00
ES5 bundle generation complete.

2020-06-13T10:14:23.800+02:00

2020-06-13T10:14:23.800+02:00
chunk {0} runtime-es2015.1eba213af0b233498d9d.js (runtime) 1.45 kB [entry] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {0} runtime-es5.1eba213af0b233498d9d.js (runtime) 1.45 kB [entry] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {1} main-es2015.cfbcf44215fba926de2a.js (main) 299 kB [initial] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {1} main-es5.cfbcf44215fba926de2a.js (main) 362 kB [initial] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {2} polyfills-es2015.690002c25ea8557bb4b0.js (polyfills) 36.1 kB [initial] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {3} polyfills-es5.9e286f6d9247438cbb02.js (polyfills-es5) 129 kB [initial] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {4} styles.9235c00a82b231f12fd9.css (styles) 586 bytes [initial] [rendered]

2020-06-13T10:14:23.800+02:00
Date: 2020-06-13T08:14:22.612Z - Hash: a55ad60a4042b04ff781 - Time: 70971ms

2020-06-13T10:14:23.800+02:00

2020-06-13T10:14:23.800+02:00

2020-06-13T10:14:23.800+02:00

2020-06-13T10:14:23.800+02:00
**[Container] 2020/06/13 08:14:22 Phase complete: BUILD State: SUCCEEDED**

2020-06-13T10:14:23.800+02:00
[Container] 2020/06/13 08:14:22 Phase context status code: Message:

2020-06-13T10:14:23.800+02:00
**[Container] 2020/06/13 08:14:22 Entering phase POST_BUILD**

2020-06-13T10:14:23.800+02:00
[Container] 2020/06/13 08:14:22 Running command aws s3 cp dist s3://${S3_BUCKET} --recursive

2020-06-13T10:14:29.834+02:00
**upload failed: dist/studileih/styles.9235c00a82b231f12fd9.css to s3://studileih-cicd-env.com
/studileih/styles.9235c00a82b231f12fd9.css An error occurred (AccessDenied) when calling the 
PutObject operation: Access Denied**

2020-06-13T10:14:29.834+02:00
**Completed 586 Bytes/847.7 KiB (0 Bytes/s) with 9 file(s) remaining upload failed: dist/studileih  
/runtime-es5.1eba213af0b233498d9d.js to s3://studileih-cicd-env.com/studileih/runtime-
es5.1eba213af0b233498d9d.js An error occurred (AccessDenied) when calling the PutObject operation: 
Access Denied**

2020-06-13T10:14:29.834+02:00
**Completed 2.0 KiB/847.7 KiB (0 Bytes/s) with 8 file(s) remaining upload failed: dist/studileih
/index.html to s3://studileih-cicd-env.com/studileih/index.html An error occurred (AccessDenied) 
when calling the PutObject operation: Access Denied**

2020-06-13T10:14:29.834+02:00
**Completed 3.0 KiB/847.7 KiB (0 Bytes/s) with 7 file(s) remaining upload failed: dist/studileih
/runtime-es2015.1eba213af0b233498d9d.js to s3://studileih-cicd-env.com/studileih/runtime-
es2015.1eba213af0b233498d9d.js An error occurred (AccessDenied) when calling the PutObject 
operation: Access Denied**

[...followed by some more similar ACCESS DENIED LOGS]

2020-06-13T10:14:29.834+02:00
**[Container] 2020/06/13 08:14:28 Command did not exit successfully aws s3 cp dist s3://${S3_BUCKET}    
--recursive exit status 1**

2020-06-13T10:14:29.834+02:00
**[Container] 2020/06/13 08:14:28 Phase complete: POST_BUILD State: FAILED**

2020-06-13T10:14:29.834+02:00
**[Container] 2020/06/13 08:14:28 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error   
while executing command: aws s3 cp dist s3://${S3_BUCKET} --recursive. Reason: exit status 1**

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Expanding base directory path: .

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Assembling file list

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Expanding .

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Expanding file paths for base directory .

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Assembling file list

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Expanding **/*

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Found 57833 file(s)

2020-06-13T10:14:43.865+02:00
[Container] 2020/06/13 08:14:42 Phase complete: UPLOAD_ARTIFACTS State: SUCCEEDED

2020-06-13T10:14:43.865+02:00
[Container] 2020/06/13 08:14:42 Phase context status code: Message:

已解决:在Marcin回答如下后,我在CodeBuild中附加到Build项目的服务角色-现在构建成功!:

the s3 bucket policy

amazon-web-services amazon-s3 deployment yaml aws-codebuild
1个回答
1
投票

命令aws s3 cp dist s3://${S3_BUCKET} --recursive失败,因为与您的代码构建项目关联的角色不具有对s3:PutObject${S3_BUCKET}权限。

您提供给S3存储桶策略的链接是错误的策略,因为它应该允许访问CodeBuild使用的IAM角色,而不是IAM用户。

要解决此问题,您具有两个选项

  1. 向用于构建项目的CodeBuild role添加所需的权限。该链接还显示了具有S3权限的示例CodeBuild角色。
  2. 将角色添加到存储桶策略中。

您尚未发布您使用的存储桶策略,因此我仅提供一个示例:

{
    "Id": "MyPolicyForCodeBuild",
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowCodeBuild",
            "Action": "s3:*",
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::<your-bucket>/*",
                "arn:aws:s3:::<your-bucket>"
            ],
            "Principal": {
                "AWS": [
                    "<arn-of-codebuild-role>"
                ]
            }
        }
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.