0
votes

Context

Azure Pipelines supports different kinds of CI triggers (docs).

Example A:

trigger:
  branches:
    include:
    - master

The pipeline will run if new commits are detected in the master branch.

Example B:

trigger:
  branches:
    include:
      - refs/tags/v*

The pipeline will run if new tags beginning with v are detected.

I want my pipeline to run when both of the above conditions are true: branch and tag.

Example C (not working as desired):

trigger:
  branches:
    include:
    - master
    - refs/tags/v*

Combining the two triggers seems to act like an or-condition, not an and-condition.

Question

How can I trigger a pipeline on the master branch only when there are new v* tags?

2

2 Answers

2
votes

In my opinion it is not possible. Following documentation:

If you specify tags in combination with branch filters that include file paths, the trigger will fire if the branch filter is satisfied and either the tag or the path filter is satisfied.

Unfortunatelly adding condition will not help here because we don't have sufficient information. For instance for tag trigger we have this

BUILD_SOURCEBRANCH=refs/tags/release-07
BUILD_SOURCEBRANCHNAME=release-07

and for branch trigger this:

BUILD_SOURCEBRANCH=refs/heads/master
BUILD_SOURCEBRANCHNAME=master

And even if you try to check manually branch name with git branch you will get:

* (HEAD detached at 154ce86)

For me this is a good candidate for a feature request at developer community.

0
votes

I found a way of doing this by using templates and parameters.

Example

First, create a common.yaml with parameterized tasks and scripts:

parameters:
  - name: myMessage
    type: string
    default: 'Hello from template!'
  - script: |
      echo ${{ parameters.myMessage }}

This yaml can contain any number of parameters, shared tasks and scripts.

Then, create a master.yaml for the master branch:

trigger:
  branches:
    include:
    - master
extends:
  template: common.yml
  parameters:
      myMessage: 'Hello from master!' # Overrides default parameter value

Create another tags.yaml to run for each new version tag:

trigger:
  branches:
    include:
    - refs/tags/v*
extends:
  template: common.yml
  parameters:
      myMessage: 'Hello from tags!' # Overrides default parameter value

Lastly, create two Pipelines within Azure DevOps web interface. Connect one of the Pipelines to master.yaml and the other to tags.yaml.

With this setup, each trigger is independent with minimal yaml duplication.