简体   繁体   English

Angular 项目 - Azure DevOps - 3 个环境文件。 在 yaml 文件中,我将指定要使用哪个环境文件的变量放在哪里?

[英]Angular project - Azure DevOps - 3 Environment files. In the yaml file, where do I put the variable that designates which environment file to use?

Angular 12 I have 3 environment files environment.dev.ts environment.prod.ts environment.ts Angular 12 我有3个环境文件environment.dev.ts environment.prod.ts environment.ts

I have 2 jobs in my yaml file.我的 yaml 文件中有 2 个工作。 Build_Dev and Build_Prod Build_DevBuild_Prod

Here is my yaml file:这是我的 yaml 文件:

azure-pipelines.yml azure-pipelines.yml

name: $(date:yyyyMMdd)$(rev:.r)

trigger:
  - master

variables:
  baseHref: ""
  environmentName: "dev"
  prodEnvironmentName: "prod"
  nodeVersion: 12.14

jobs:
  - job: Build_Dev
    steps:
      parameters:
        environmentName: 'dev'
      - task: NodeTool@0
        displayName: "Use Node Version"
        inputs:
          versionSpec: ${{variables.nodeVersion}}
      - task: Npm@1
        displayName: "npm install"
        inputs:
          verbose: false
      - task: Npm@1
        displayName: "npm run build"
        condition: and(succeeded(), eq('${{variables.baseHref}}', ''))
        inputs:
          command: custom
          verbose: false
          customCommand: "run build -- --output-path=dist/${{variables.environmentName}} --verbose"
      - task: ArchiveFiles@2
        displayName: "Archive dist folder"
        inputs:
          rootFolderOrFile: dist/${{variables.environmentName}}
          includeRootFolder: false
          archiveFile: "$(Build.ArtifactStagingDirectory)/${{variables.environmentName}}/$(Build.BuildId).zip"
      - task: PublishBuildArtifacts@1
        displayName: "Publish Artifact"
        inputs:
          PathtoPublish: "$(Build.ArtifactStagingDirectory)/${{variables.environmentName}}"
          ArtifactName: ${{variables.environmentName}}
  - job: Build_Prod
    steps:
      - task: NodeTool@0
        displayName: "Use Node Version"
        inputs:
          versionSpec: ${{variables.nodeVersion}}
      - task: Npm@1
        displayName: "npm install"
        inputs:
          verbose: false
      - task: Npm@1
        displayName: "npm run build"
        condition: and(succeeded(), eq('${{variables.baseHref}}', ''))
        inputs:
          command: custom
          verbose: false
          customCommand: "run build -- --output-path=dist/${{variables.prodEnvironmentName}} --verbose"
      - task: ArchiveFiles@2
        displayName: "Archive dist folder"
        inputs:
          rootFolderOrFile: dist/${{variables.prodEnvironmentName}}
          includeRootFolder: false
          archiveFile: "$(Build.ArtifactStagingDirectory)/${{variables.prodEnvironmentName}}/$(Build.BuildId).zip"
      - task: PublishBuildArtifacts@1
        displayName: "Publish Artifact"
        inputs:
          PathtoPublish: "$(Build.ArtifactStagingDirectory)/${{variables.prodEnvironmentName}}"
          ArtifactName: ${{variables.prodEnvironmentName}}

Both jobs run fine, other than the fact that they both pull from the environment.ts file.除了它们都从environment.ts文件中提取之外,这两个作业都运行良好。

I can't figure out where to add the parameter in each job to specify which environment file to use.我不知道在每个作业中在哪里添加参数以指定要使用的环境文件。

environment.ts环境.ts

export const environment = {
  production: false,
  development: false,
...
}

environment.dev.ts环境.dev.ts

export const environment = {
  production: false,
  development: true,
...
}

environment.prod.ts环境.prod.ts

export const environment = {
  production: true,
  development: false,
...
}

To solve your problem -为了解决您的问题 -

Where do I put the variable that designates which environment file to use?我应该把指定使用哪个环境文件的变量放在哪里?

First organize your pipeline into multiple stages, using the stages keyword.首先使用stages关键字将您的管道组织成多个阶段。 Multi-stage pipelines are the new way to configure your release via code and it helps you to logically divide your pipeline.多阶段管道是通过代码配置发布的新方法,它可以帮助您在逻辑上划分管道。

Normally, you can set up a multi-stage pipeline that contains the main processes for your application, such as "Build", "Test" and "Deploy".通常,您可以设置一个多阶段管道,其中包含应用程序的主要流程,例如“构建”、“测试”和“部署”。 And like release pipeline, you also can set a stage for each deployment environment in the same pipeline.和发布管道一样,您也可以在同一管道中为每个部署环境设置一个阶段。

stages:
- stage: Build
  jobs:
  - job: A1

- stage: Deploy_dev
  jobs:
  - job: B1
...
...

After that add the replacement tokens in your environment files which we can utilize during our deployment stages to replace the tokens with the actual values.之后,在您的环境文件中添加替换令牌,我们可以在部署阶段利用这些替换令牌将令牌替换为实际值。

// environment.prod.ts
export const environment = {
  production: true,
  auth: {
    clientID: '#{authClientID}#',
    domain: '#{authDomain}#',
  },
  apiEndpoint: '#{apiEndpoint}#',
};

And install the Replace Tokens extension needs at organization level to replace the tokens we used above.并在组织级别安装Replace Tokens扩展需求以替换我们上面使用的令牌。

Now create two variable groups to store the actual variables that will be used to substitute the tokens for your Dev and Prod.现在创建两个变量组来存储实际变量,这些变量将用于替换您的 Dev 和 Prod 的令牌。 Go to Library section of the pipeline and add this variable groups . Go 到管道的库部分并添加此变量组 Then lastly add replace token task to replace them with actual value.然后最后添加替换令牌任务以将它们替换为实际值。

I would suggest to read this How to use Angular environment files in your Azure DevOps multi-stage yml release pipeline for detailed step by step procedure.我建议阅读这篇How to use Angular environment files in your Azure DevOps multi-stage yml release pipeline ,了解详细的分步过程。 And also check this similar thread raised in stack overflow.并检查堆栈溢出中引发的类似线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular + Azure DevOps(TFS):替换 environment.ts 文件中的令牌,与环境无关的角度构建 - Angular + Azure DevOps(TFS): replace tokens in environment.ts file, environment agnostic angular build Azure Devops Angular 环境变量 - Azure Devops Angular Environment Variables 使用 JSON 文件而不是环境文件仅用于 angular 9 中的构建应用程序 - Use JSON file instead of environment files only for build application in angular 9 Angular 6,我应该在 environment.ts 文件中放置秘密环境变量吗? - Angular 6, should I put secret environment variables in environment.ts file? 如何使用 gulpfile.js 文件 Angular 8 中的环境变量? - How can i use the environment variable in gulpfile.js file Angular 8? 如何在Angular2应用程序中将.env文件与环境变量一起使用? - How can I do to use .env file with environment variables in Angular2 application? 在 index.html 文件中使用环境变量 - Use environment variable in index.html file JSON 文件放在 Angular 项目中的什么位置? - Where to put the JSON file in the Angular Project? 我可以在angular4环境中进行angular2投影吗? - can i do angular2 project in angular4 environment? 使用 docker-build.yaml 环境变量在 angular package.Z466DEEC76ECDF65FCA6D3857DFZ - Use docker-build.yaml environment variable in angular package.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM