0

在命令下面,我正在尝试使用云构建触发器运行,但它仍然会获取 enviroment.ts 文件

cloudbuild.yaml

- name: gcr.io/cloud-builders/npm
  args: [ run, build, --configuration=Staging ]

下面是 Package.json 脚本代码

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"}

来自 angular.json 的文件替换代码

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "projectname": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/projectname",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.scss",
              "src/assets/css/style.css"
            ],
            "scripts": [
              "src/assets/js/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js",
              "node_modules/chart.js/dist/Chart.bundle.min.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "5mb",
                  "maximumError": "8mb"
                }
              ]
            },
            "staging": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.staging.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "5mb",
                  "maximumError": "8mb"
                }
              ]
            },
            "dev": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.dev.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "5mb",
                  "maximumError": "8mb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "projectname:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "projectname:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "projectname:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "projectname-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "projectname:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "projectname:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "projectname",
  "cli": {
    "analytics": "51d20bdf-c925-4ff9-b30c-d7fe27412dc1"
  }
}

你能帮我解决这个问题吗?我也用 --prod 尝试过,但不起作用,它总是需要 evironment.ts 文件。但是,当我在本地执行此操作时,它可以正常工作。我看到云构建具有替代变量概念,不确定使用它的最佳方式。

编辑

即使我在云构建中有以下暂存命令,它也总是需要ng 构建。参考图像。我没有看到任何错误,一切都成功了。

- name: gcr.io/cloud-builders/npm
  args: [ run, build, --configuration=staging ]

在此处输入图像描述

4

3 回答 3

1

将 cloudbuild.yaml 更改为以下

steps:

- name: gcr.io/cloud-builders/npm
  args: [ install ]


- name: gcr.io/cloud-builders/npm
  args: ['run', 'build','--','--prod']

- name: gcr.io/cloud-builders/gcloud
  args: ['app', 'deploy', '--version=$SHORT_SHA']

如果您想要它用于除产品以外的环境,请替换--prod--configuration=staging

于 2021-01-11T10:39:03.423 回答
0

尝试fileReplacementsbuild对象中指定 (这个用于ng serve)。

  "build": {
    "options": {
      [...]
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        }
      ]      
    },
    "configurations": {
      // other - non default - configurations 
    }
  }
于 2021-01-05T16:14:00.823 回答
-1

在名为的环境文件夹中创建一个文件environment.staging.ts并使用以下命令

ng build --configuration=staging
于 2021-01-05T09:54:11.947 回答