Home > Back-end >  Azure pipeline yml - Change value in task and execute based on if else condition
Azure pipeline yml - Change value in task and execute based on if else condition

Time:08-06

In the below task, based on the branch, how do I specify a different value for assembleQaDebug. If the branch is qa it has to be assembleQaDebug; if develop, it has to be assembleDevelopDebug . And if prod branch, it has to be assembleProdRelease. What is the best approach doing this, if there is one. Any help is much appreciated.

- task: Gradle@3
        displayName: 'Build Task'
        continueOnError: false
        inputs:
          tasks: assembleQaDebug -PversionCode=$(Build.BuildId) -PdisablePreDex --no-daemon
          publishJUnitResults: false

CodePudding user response:

The values has to be specified in 'variables' section:

variables:
  - name: tf_version
    value: latest
  - name: variablegroupname
    ${{ if eq(variables['Build.SourceBranchName'], 'develop') }}:
      value: assembleDevelopDebug
    ${{ if eq(variables['Build.SourceBranchName'], 'qa') }}:
      value: assembleQaDebug
  - name: env
    ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
      value: prod
    ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}:
      value: develop

Like this you can add your variables and depending on your stage you can replace the values.

CodePudding user response:

Not sure if it's the best way to do this, but I love using variables for those kind of use cases, so check this one :

...


variables:
  - name: assembleDebug
    ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/prod') }}:
      value: assembleProdRelease
    ${{ elseif eq(variables['Build.SourceBranch'], 'refs/heads/qa') }}:
      value: assembleQaDebug
    ${{ elseif eq(variables['Build.SourceBranch'], 'refs/heads/develop') }}:
      value: assembleDevelopDebug 
    

...

- task: Gradle@3
  displayName: 'Build Task'
  continueOnError: false
  inputs:
    tasks: $(assembleDebug) -PversionCode=$(Build.BuildId) -PdisablePreDex --no-daemon
    publishJUnitResults: false

  • Related