使用代码管道在 AWS S3 上部署 Angular 7 应用程序时出错

Error while deploying Angular 7 app on AWS S3 using codepipeline

我想使用 AWS 代码管道在 S3 上自动部署 Angular 7 个应用程序(作为静态网站)。 我已经创建了 Angular 应用程序并推送到我的 git 存储库。 我创建了新的 AWS S3 存储桶并创建了 AWS Codepipline 并集成了 git 存储库

当 aws code-pipelineb 构建应用程序时,我遇到以下错误: COMMAND_EXECUTION_ERROR:执行命令时出错:ng build。原因:退出状态 1

我正在使用 buildspect.yml 文件

version: 0.2

env:
    variables:
        S3_BUCKET: "<bucket name>"
        BUILD_ENV : "prod"

phases:
    install:
        runtime-versions:
            nodejs: 10
        commands:
            # install dependencies
            - echo Installng source NPM dependencies...
            - npm install npm@latest -g
            - npm install -g @angular/cli

    pre_build:
        commands:
            - echo Prebuild steps
            - npm install

    build:
        commands:
            # Builds Angular application. You can also build using custom environment here like mock or staging
            - echo Build started on `date`
            - ng build

    post_build:
        commands:
            # Clear S3 bucket.
            - aws s3 rm s3://${S3_BUCKET} --recursive
            - echo S3 bucket is cleared.
            # Copy dist folder to S3 bucket, As of Angular 6, builds are stored inside an app folder in distribution and not at the root of the dist folder
            - aws s3 cp dist s3://${S3_BUCKET} --recursive
            - echo Build completed on `date`

artifacts:
    files:
        - '/'
    discard-paths: yes
    base-directory: 'dist*'

感觉代码构建环境配置不正确。我的意思是 Nodejs 和 npm 没有正确安装。请检查上面的 yml 文件并帮助我确定是否遗漏了什么。

您的 Buildspec.yml 文件中缺少构建环境。

添加这篇文章并检查是否有帮助 -

build:
    commands:
        # Builds Angular application. You can also build using custom environment here like mock or staging
        - echo Build started on `date`
        - ng build --${BUILD_ENV}

这非常适合我!

这个 yml 文件对我来说工作得很好:

version: 0.2

env:
    variables:
        S3_BUCKET: "pratik-portfolio"
phases:
install:
    runtime-versions:
        nodejs: 10
    commands:
    - echo $CODEBUILD_SRC_DIR
    - npm install -y npm@latest
    - npm install -g @angular/cli
    - rm package-lock.json
pre_build:
    commands:
    - npm install
build:
    commands:
    - echo build started on `date`
    - ng build
    - ls -l -F
post_build:
    commands:
            # Clear S3 bucket.
            - aws s3 rm s3://${S3_BUCKET} --recursive
            - echo S3 bucket is cleared.
            - aws s3 cp dist/{Your app name} s3://${S3_BUCKET} --recursive
            - echo Build completed on `date`
artifacts:
    files:
        - '/'
    discard-paths: yes
    base-directory: 'dist*'
cache:
paths:
    - node_modules/