Home > OS >  Azure DevOps YAML Pipelines: Add Git Tag based condition
Azure DevOps YAML Pipelines: Add Git Tag based condition

Time:03-24

I have a Azure DevOps YAML pipeline which always runs if I push a commit to my git main branch. That's Ok so.

But now I want to use git tags and conditons to run specific jobs in this pipeline only if the commit contains a specific tag.

How can I get the git tags to use them in a condition?

Thank you in forward

Best regards Matthias

CodePudding user response:

What do you mean about "if the commit contains a specific tag"?

  1. If it means a git tag created from a commit:
  • For example, I have the tags like as the format 'release-xxx'.

    enter image description here

  • To run particular job in the pipeline when a tag is created as the format 'release-xxx', you can set the YAML pipeline like as below. You can use the variable Build.SourceBranch to get the branch/tag name which triggered the current pipeline run.

    trigger:
      branches:
        include:
        - main
      tags:
        include:
        - 'release-*'
    
    pool:
      vmImage: windows-latest
    
    jobs:
    - job: A
      displayName: 'JobA'
      steps:
      . . .
    
    # JobB only runs when the pipeline is triggered by the tags like as the format 'release-xxx'.
    - job: B
      displayName: 'JobB'
      condition: contains(variables['Build.SourceBranch'], 'refs/tags/release-')
      steps:
      . . .
    
  1. If it means the specified keywords contained in the commit message:
  • For example, a commit message contains the keyword like as 'release-xxx'.

    enter image description here

  • To run particular job in the pipeline when a commit message contains the keyword like as 'release-xxx', you can set the YAML pipeline like as below. You can use the variable Build.SourceVersionMessage to get the commit message.

    trigger:
      branches:
        include:
        - main
    
    pool:
      vmImage: windows-latest
    
    jobs:
    - job: A
      displayName: 'JobA'
      steps:
      . . .
    
    # JobB only runs when the commit message contains the keyword like as 'release-xxx'.
    - job: B
      displayName: 'JobB'
      condition: contains(variables['Build.SourceVersionMessage'], 'release-')
      steps:
      . . .
    

CodePudding user response:

You can check the below pipeline. It is triggered based on tag and also executes a specific task based on the tag.

trigger:
  tags:
    include:
    - release.*

pool:
  vmImage: ubuntu-latest

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'

- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
  displayName: 'Run a multi-line script'

- script: echo this pipeline was triggered from the first pipeline
  displayName: 'Second pipeline'
  condition: eq(variables['Build.SourceBranch'], 'refs/tags/release.v5')

When running the pipeline with release.v4 the task second pipeline is skipped.

enter image description here

When running the pipeline with release.v5 the task second pipeline is executed.

enter image description here

  • Related