john mich
john mich

Reputation: 2873

How to set Tags based azure pipeline stage triggering?

I have the below Azure YAML pipeline file whose stages get executed based on a tag some-tag-v* reference pushed to the Azure repo.

trigger:
  branches:
    include:
      - refs/tags/some-tag-v*
    exclude:
      - refs/tags/some-branch

pool:
  name: some-name
  demands:
  - Agent.Name -equals some-name

stages:
- stage: A
  jobs:
  - job: A
  steps:
  - checkout: self
  - bash: |
      some-bash-commands
- stage: B
  jobs:
  - job: B
    steps:
    - bash: |
       More-bash-commands

Problem: I want stage B to be executed only when some-tag-2-v* is pushed instead of some-tag-v*. I am looking at the condition-based triggering but I am not sure if that's the right way to do it.

Upvotes: 2

Views: 1465

Answers (2)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35099

I suggest that you could directly use the IF Expression in Stages. This will be more convenient.

Here is an example:

trigger:
  branches:
    include:
      - - refs/tags/some-tag-v*
      
    exclude:
      - refs/tags/some-branch


stages:
- ${{ if eq(variables['Build.SourceBranch'], 'refs/tags/some-tag-v') }}:

  - stage: A
    jobs:
    - job: A
      steps:
       - checkout: self
       - bash: echo 1
      
- ${{ if eq(variables['Build.SourceBranch'], 'some-tag-2-v') }}:

  - stage: B
    jobs:
    - job: B
      steps:
       - bash: echo 2

When the pipeline is triggered by some-tag-2-v, it will only run the stage B.

Upvotes: 1

psfinaki
psfinaki

Reputation: 2132

I think you can make use of YAML conditions.

stages:
- stage: A
  condition: ...
  jobs:
  - job: A
  steps:
  - checkout: self
  - bash: |
      some-bash-commands

For the condition, you can probably leverage pipeline and build variables, e.g.:

variables:
  isSomeTag: $[eq(variables['Build.SourceBranch'], 'refs/tags/some-tag-v')]

Upvotes: 3

Related Questions