YAML中的Azure管道多阶段与单独发布

时间:2020-07-10 18:53:10

标签: azure azure-devops azure-pipelines

Azure管道在YAML中支持多个阶段。一个典型的例子就是:

trigger:
- master

pool:
  name: Default
  demands:
  - npm
  - msbuild
  - visualstudio

stages:

  - stage: build

    jobs:
     - job: Build app

  - stage: deploy
   
    jobs:
    - job: Deploy to dev

我不习惯这样工作。通常,我会运行管道来构建我的应用程序并将工件放到放置文件夹中。不管发布以后要针对的环境如何,管道都是相同的。 然后,我选择运行集成,UAT或生产版本。

但是,拥有多级管道,我们将构建和发布混合在一起。那么我将如何在给定的环境中释放?我必须在每个环境中重复此管道吗?

1 个答案:

答案 0 :(得分:0)

您可以在此处使用模板结构。在此,您将需要为不同的作业和变量创建单独的文件。然后,您将需要在每个阶段使用适当的可变模板文件执行模板。

目录结构 Directory Structure

管道: Pipeline stages

环境 Environments

这是示例管道:

trigger:
- master

variables:
  - name: vmImage
    value: 'ubuntu-latest'

stages:
  - stage: Build
    displayName: Build stage
    jobs:
    - job: BuildJob
      pool:
        vmImage: $(vmImage)
      steps:
      - template: Jobs/build.yml
 
  - stage: NonProd
    displayName: Deploy non prod stage
    jobs:
    - deployment: DeploymentJob1
      pool:
        vmImage: $(vmImage)
      environment: non-prod
      variables:
        - template: Variables/non-prod.yml
      strategy:
        runOnce:
          deploy:
            steps:
            - template: Jobs/deploy.yml

  - stage: Prod
    displayName: Deploy prod stage
    jobs:
    - deployment: DeploymentJob2
      pool:
        vmImage: $(vmImage)
      environment: prod
      variables:
        - template: Variables/prod.yml
      strategy:
        runOnce:
          deploy:
            steps:
            - template: Jobs/deploy.yml
    

作业/build.yml

steps:
- script: echo I am building!
  displayName: 'Run Build'

工作/职位.yml

steps:
- script: echo I am deploying to $(Environment)!
  displayName: 'Run Deployment'

变量/非prod.yml

variables:
- name: Environment
  value: non-prod

变量/prod.yml

variables:
- name: Environment
  value: prod