Di bawah perintah, saya mencoba menjalankan dengan pemicu cloud build tetapi masih mengambil file enviroment.ts

Cloudbuild.yaml

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

Di bawah ini adalah kode skrip Package.json

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

Kode penggantian file dari 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"
  }
}

Bisakah Anda membantu saya di mana saya mendapatkan ini salah? Saya mencobanya dengan --prod juga tetapi tidak berhasil, selalu membutuhkan file evironment.ts. Namun, itu berfungsi dengan baik ketika saya melakukannya secara lokal. Saya melihat cloud build memiliki konsep variabel substitusi tidak yakin cara terbaik menggunakannya.

EDIT

Bahkan jika saya memiliki perintah pementasan di bawah ini di cloud build, itu hanya membutuhkan ng build selalu. merujuk ke gambar. Saya tidak melihat ada kesalahan semuanya berhasil.

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

enter image description here

0
Ishika Jain 5 Januari 2021, 10:15

3 jawaban

Jawaban Terbaik

Ubah cloudbuild.yaml menjadi di bawah

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']

Ganti --prod ke --configuration=staging jika Anda menginginkannya untuk lingkungan selain prod

1
rakshit jain 11 Januari 2021, 10:59

Coba tentukan fileReplacements di objek build juga (ini digunakan untuk ng serve).

  "build": {
    "options": {
      [...]
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        }
      ]      
    },
    "configurations": {
      // other - non default - configurations 
    }
  }
0
Nibrass H 5 Januari 2021, 16:14

Buat file di folder lingkungan bernama environment.staging.ts dan gunakan perintah di bawah ini

ng build --configuration=staging
-1
Arokia Lijas 5 Januari 2021, 09:54