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

This is the YML code that I'm trying. I have a requirement that in each branch different parameters need to be set.

For example, sonar analysis only should run in the development branch at the time of auto integration & deployment. I want to know how to configure default property to true based on branch.

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/[email protected]
      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 [email protected] -g
      gzipper compress ./dist
    displayName: 'GZIP'


Solution 1:[1]

You could define variable like this:

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)

You could use condition also on step level.

We aware that in cron you use main branch and in ci triggers master. I'm pretty sure you don't have both.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Krzysztof Madej