Search code examples
azure-devopsazure-pipelinesmultistage-pipeline

Skipping stage based on commit message


I am trying to set Azure DevOps to skip a stage on a multi-stage pipeline if a message does not start with a given text.

From the examples documentation, I think it is just

  - stage: t1
    condition: not(startsWith(variables['Build.SourceVersionMessage'], '[maven-release-plugin]'))
    jobs:
      - job: ReleasePrepare
        displayName: Prepare release
        pool:
          vmImage: 'ubuntu-16.04'
        steps:
          - script: |
              env | sort

However, this gets executed regardless. Here's an example of where I expect the t1 task to not be run based on the commit message https://dev.azure.com/trajano/experiments/_build/results?buildId=110&view=results

The output of env shows that the message was passed in correctly

enter image description here

Just in case it is a bug I reported it here as well https://developercommunity.visualstudio.com/content/problem/697290/startswith-buildsourceversionmessage-variable-not.html


Solution

  • It appears that Build.SourceVersionMessage at the time of this post is only resolvable on the steps.

    Here's a working example that stores the value in a variable in one step and use it in the next job (which can be a deployment)

    trigger:
      batch: true
      branches:
        include:
          - master
    
    stages:
      - stage: ci
        displayName: Continuous Integration
        jobs:
          - job: Build
            pool:
              vmImage: 'ubuntu-16.04'
            steps:
              - script: |
                  env | sort
                  echo "$(Build.SourceVersionMessage)"
      - stage: t1
        displayName: Release
        condition: eq(variables['Build.SourceBranch'],'refs/heads/master')
        jobs:
          - job: GetCommitMessage
            displayName: Get commit message
            steps:
              - bash: |
                  echo "##vso[task.setvariable variable=commitMessage;isOutput=true]$(Build.SourceVersionMessage)"
                  echo "Message is '$(Build.SourceVersionMessage)''"
                name: SetVarStep
                displayName: Store commit message in variable
          - job: ReleasePrepare
            displayName: Prepare release
            dependsOn: GetCommitMessage
            pool:
              vmImage: 'ubuntu-16.04'
            condition: not(startsWith(dependencies.GetCommitMessage.outputs['SetVarStep.commitMessage'], '[maven-release-plugin]'))
            steps:
              - script: |
                  echo this would be a candidate for release
                  env | sort
                displayName: Don't do it if maven release
          - job: NotReleasePrepare
            displayName: Don't Prepare Release
            dependsOn: GetCommitMessage
            pool:
              vmImage: 'ubuntu-16.04'
            condition: startsWith(dependencies.GetCommitMessage.outputs['SetVarStep.commitMessage'], '[maven-release-plugin]')
            steps:
              - script: |
                  echo this not be a candidate for release because it was created by the plugin
                  env | sort
                condition: startsWith(variables.commitMessage, '[maven-release-plugin]')
                displayName: Do it if maven release
    
    

    The build can be found in https://dev.azure.com/trajano/experiments/_build/results?buildId=133&view=logs&s=6fc7e65a-555d-5fab-c78f-9502ae9436c4&j=b5187b8c-216e-5267-fcdb-c2c33d846e05