如何使用yml文件为Azure管道中的不同分支设置不同的参数

How to set different parameters for different branches in azure pipeline using yml file

这是我正在尝试的 YML 代码。我有一个需求,每个分支需要设置不同的参数。

例如声纳分析在自动集成部署时只需要在开发分支运行。我想知道如何根据分支将 default 属性 配置为 true。

parameters:
- name: sonar_analysis
  displayName: Sonarqube Analysis
  type: boolean
  default: true
- name: docker_build_push
  displayName: Docker Build and Push
  type: boolean
  default: false
- name: sonar_publish
  displayName: Sonarqube Publish
  type: boolean
  default: false
- name: owasp_dep_check
  displayName: OWASP Dependency Check
  type: boolean
  default: false
- name: angular_build
  displayName: Angular Build
  type: boolean
  default: false
- name: angular_build_gzip
  displayName: Angular Build GZIP
  type: boolean
  default: false



# specific path build
trigger:
  batch: true
  branches:
    include:
    - master
    - development

# YAML file in the main branch
schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - main
pool:
  vmImage: ubuntu-latest

variables:
  docker_ng_tag: 0.0.1
  

steps:

- task: NodeTool@0
  inputs:
    versionSpec: '10.16.0'

- ${{ if eq(parameters.angular_build, true) }}:
  - script: |
      npm install -g @angular/cli@11.2.2
      npm install
      npm link @angular/cli   
      ng build --prod --aot  --base-href /app/tms/ --deploy-url /app/tms/
    displayName: 'npm install and build'

- ${{ if eq(parameters.angular_build_gzip, true) }}:
  - script: |
      npm i gzipper@4.4.0 -g
      gzipper compress ./dist
    displayName: 'GZIP'

您可以这样定义变量:

variables:
  isDevelopment: $[eq(variables['Build.SourceBranch'], 'refs/heads/development')]

stages:
- stage: A
  jobs:
  - job: A1
    steps:
      - script: echo Hello Stage A!

- stage: B
  condition: and(succeeded(), eq(variables.isDevelopment, 'true'))
  jobs:
  - job: B1
    steps:
      - script: echo Hello Stage B!
      - script: echo $(isDevelopment)

您也可以在步骤级别使用条件。

我们知道您在 cron 中使用 main 分支,在 ci 中使用触发器 master。我很确定你没有两者。