Triggering a job only if a tag is created in azure devops

Viewed 16

I want to execute a job only when a tag is set. I have set the trigger and have given the variables as well, along with the condition as shown below:


trigger:
  tags:
    include:
    - '*'

  branches:
    include:
    - '*'
  

pr:
 branches:
   include:
     - main

pool:
  vmImage: ubuntu-latest


variables:
- name: isCommitToMain
  value: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
- name: isPullRequest
  value: $[eq(variables['Build.Reason'],'PullRequest')]
- name: isTag
  value: $[eq(variables['Build.SourceBranch'],'refs/tags/*')]


stages:  
- stage: Analysis  
  jobs:  
  - job: without_any_condition
    steps:     
    - script: |
        echo "without any condition"
        
  - job: with_commit_to_main_condition
    condition: eq(variables.isCommitToMain,true)
    steps:
      - script: |
          echo "with commit to master condition"

  - job: with_pr_condition
    condition: eq(variables.isPullRequest,true)
    steps:
      - script: |
          echo "this is with PR condition"
  
  - job: with_tag_condition
    condition: eq(variables.isTag,true)
    steps:
      - script: |
          echo "this is with tag condition"

When I create a Tag, I expect the job without_any_condition and job with_tag_condition to execute. But on creating the tag, I see that the with_tag_condition job is skipped.

Tag response

Suggestions please? What am I doing wrong here?

1 Answers

You can define the isTag variable in a different way as follows:

- name: isTag
  value: $[startsWith(variables['Build.SourceBranch'],'refs/tags/')]

Related