Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent triggering an Azure DevOps build pipeline based on commit tags?

I am using Azure pipelines with a Github-based project. I have set up a build pipeline that is triggered only by tagged commits, to keep it separate from automatic daily builds that happen at every commit.

I would like to exclude tagged commits from triggering the daily build pipeline. What is the correct way to do so in a yaml script?

Here is what I did, without success.

According to Azure documentation at this page, to my understanding excluding tags should be possible with something like:

trigger:
  tags:
    exclude:
    - projectname_v*

However, this does not work, and just prevents the build pipeline to run at any commit, be it tagged or not.

I have also tried:

trigger:
  tags:
    include:
    - *
    exclude:
    - projectname_v*

but this is apparently not supported, as it produces error:

/azure-pipelines.yml: (Line: 12, Col: 7, Idx: 220) - (Line: 12, Col: 8, Idx: 221): While scanning an anchor or alias, did not find expected alphabetic or numeric character.

I have also tried the alternative syntax proposed on the doc page:

trigger:
  branches:
    exclude:
      refs/tags/{projectname_v*}

as well as variants with/without braces and wildcards, but all fail with "unexpected value" or "Input string was not in a correct format" errors.

Edit 2019-12-10

After reading wallas-tg's answer below, I have tried the following in the daily build pipeline:

trigger:
  branches:
    include:
    - '*'
    exclude:
    - 'refs/tags/*'

This works, but does not do what I would like:

  • Pushing only a tag triggers the correct pipeline and not the one for daily builds
  • Pushing a commit without tags triggers the daily build pipeline
  • Pushing a tagged commit triggers both pipelines: the daily build pipeline gets triggered by the commit, and the other one by the tag; my desired behavior in this case would be that the daily build pipeline is not triggered.
like image 841
acasta69 Avatar asked Mar 18 '19 15:03

acasta69


People also ask

What triggers a pipeline to run?

If you specify branch filters, a new pipeline is triggered whenever a source pipeline run is successfully completed that matches the branch filters. In the following example, the app-ci pipeline runs if the security-lib-ci completes on any releases/* branch, except for releases/old* .

What are the two categories of triggers in Azure DevOps?

Continuous deployment triggers help you start classic releases after a classic build or YAML pipeline completes. Scheduled release triggers allow you to run a release pipeline according to a schedule. Pull request release triggers are used to deploy a pull request directly using classic releases.

What is the type of trigger that will run a pipeline when a new artifact is created?

Continuous deployment triggers allow you to create a release every time a new build artifact is available.


2 Answers

@acasta69

I think that i found solution for your issue. I've been doing just the opposite scenario, build only features branches and exclude anything else.

For this purposes use this yml snippet on azure-pipelines.yml

resources:         
  repositories:
  - repository: myNetProject
    type: GitHub
    connection: myGitHubConnection
    source: wkrea/DockerHubImages
    trigger:
      batch: true
      branches:
        include:
        - releases/*
        exclude:
        - '*'
      paths:
        exclude:
        - README.md

I was be able to build on DevOps from

builds triggered succesfuly

If this answer was useful for you, let me know commenting and rate my answer to find it more easy next time that anyone need help, because the DevOps Pipelines documentations it's really unclear and confusing at moment :'(

Here you can see checks for my last commit on releases branch

enter image description here

like image 120
William Trigos Avatar answered Oct 01 '22 05:10

William Trigos


The syntax for build pipeline triggers is documented on this page.
Regarding what is exposed in the question, a couple of details are worth highlighting:

  1. There is a default implicit trigger that includes all branches and is overwritten by any user-defined trigger. Thus, it is not possible to specify a trigger that only excludes something: doing that would end up in nothing being included, and the trigger would never fire.
    This explains why the first code snippet shown in the question does not trigger anything.

When you specify a trigger, it replaces the default implicit trigger, and only pushes to branches that are explicitly configured to be included will trigger a pipeline. Includes are processed first, and then excludes are removed from that list. If you specify an exclude but don't specify any includes, nothing will trigger.

  1. The default implicit trigger looks like this (note the comment in last line, which explains the error produced by the second code snippet in the question):
trigger:  
  branches:  
    include:  
    - '*'  # must quote since "*" is a YAML reserved character; we want a string  

Summarizing, a correct way to do exclude tagged commits from triggering the pipeline should be the one shown in the edited part of the question:

trigger:
  branches:
    include:
    - '*'
    exclude:
    - 'refs/tags/*'

Or, which is equivalent:

trigger:
  branches:
    include:
    - '*'
  tags:
    exclude:
    - '*'

However, this does not obtain the desired effect. The following happens instead:

  • Pushing a commit without tags triggers the pipeline
  • Pushing only a tag does not trigger the pipeline
  • Pushing a tagged commit still triggers the pipeline

A final feedback received from Azure DevOps support clarifies that there is no way at the moment to obtain the desired behaviour:

Basically there is no way right now to prevent builds from being triggered if the tags are committed along with the branch changes and the CI on branch changes are enabled on the pipeline. There are couple of options you can use to prevent triggering the build on new tags:

  1. Check-in the tags separately than the branch changes.
  2. Add "[skip ci]" to your commit message to skip triggering the build on that particular commit.

None of the two options fit well to the original request. #1 is already working as intended, i.e. it does not trigger the build unless tags are explicitly included in triggers, which is only a part of the original request. #2 has the desired behaviour, but it requires a specific commit text and would force any user of the pipeline to be aware of its internals.

The workaround that I found in the meantime, as mentioned in a comment, was to use only one pipeline, that is always triggered by any commit, whether tagged or not, and detect the use of tags with dedicated scripts to activate specific pipeline steps when required.

like image 25
acasta69 Avatar answered Oct 01 '22 06:10

acasta69