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

时间:2019-06-29 12:03:25

标签: angular amazon-s3 aws-codepipeline aws-codebuild

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

当AWS Code-Pipelineb启动应用程序时,我遇到以下错误: COMMAND_EXECUTION_ERROR:执行命令ng build时出错。原因:退出状态1

enter image description here

我正在使用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文件,并帮助我确定是否缺少任何内容。

2 个答案:

答案 0 :(得分:1)

您在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}

这完全适合我!

答案 1 :(得分:0)

这个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/