Home > OS >  Overwrite Variable in Azure Yaml Pipeline not working
Overwrite Variable in Azure Yaml Pipeline not working

Time:02-17

My intention is to run a pipeline with a default variable. The variable createRelease should indicate which job should run in addition to `TestApplication.

In pseudocode:

RunJob('TestApplication')

if(!createRelease) 
    RunJob('BuildForDev')

if(createRelease)
    RunJob('BuildForUatRelease')

Somehow it does not overwrite the value of createRelease. Anyone any Idea why not?

My Pipeline yaml looks like:

trigger:
- main
- feature/*

pool:
  vmImage: ubuntu-latest

variables:  
  - name: createRelease
    value: false

stages:
  - stage: CI_Build
    displayName: Build and Test App
    jobs:
      - job: TestApplication
        displayName: Building and testing
        condition: always()
        steps:
          - task: Bash@3
            displayName: Maven Test Application
            inputs:
              targetType: 'inline'
              script: |
                echo 'mvn test'
      - job: BuildForDev
        displayName: Build Application for Develop
        dependsOn: TestApplication
        condition: |
          and(
            succeeded('TestApplication'),
            eq(variables['Build.SourceBranchName'], 'main'),
            eq(variables.createRelease, 'false')
          )
        steps:
          - task: Bash@3
            displayName: Maven test Application
            inputs:
              targetType: 'inline'
              script: |
                echo mvn test
      - job: BuildForUatRelease
        displayName: Release Application
        dependsOn: TestApplication
        condition: |
          and(
            succeeded('TestApplication'),
            eq(variables['Build.SourceBranchName'], 'main'),
            eq(variables.createRelease, 'true')
          )
        steps:
          - task: Bash@3
            displayName: Set Git Credentials
            inputs:
              targetType: 'inline'
              script: |
                echo 'mvn release prepare'

Variable definition in Pipeline which can be overwritten:

enter image description here

How I set the createRelease before run:

enter image description here

CodePudding user response:

You're assigning the value of the variable in the YAML which will take precedence over the variable defined at queue time. It might help to think of it like this:

  • You queue the pipeline with the createRelease: True variable
  • Azure DevOps creates an execution context for the pipeline and executes your pipeline.yml
  • Your pipeline defines the value for createRelease: False

To resolve:

  1. Remove the createRelease variable from the yaml

  2. Edit the pipeline:

  3. Click on Variables:

  4. Add a new variable and set the default value for the createRelease variable. Set it to allow users to change the value at queue-time:

  5. Click Ok.

Alternatively, you might want to consider create a parameter instead of a variable:

trigger:

parameters:
- name: createRelease
  displayName: 'Create a Release'
  type: boolean
  default: false

stages:
- stage: ...
  jobs:
  - job: ...
    steps:
    - script: 'hello'
      condition: ${{ eq( parameters.createRelease, 'true') }}
   
  • Related