How do I restrict pipelines to specific branches without using triggers?

Viewed 1275

How can I restrict on-demand (i.e. not triggered) pipelines in Azure DevOps so that they only run on specific branches?

For instance, I have a pipeline (using the newer YAML syntax) that should only run on the master branch, as it is used to push code through to production, and can only be run by certain developers (we handle a lot of PII data, so our audit controls are fairly demanding). I have another pipeline that should run on any branch except master, as it is used to push code through to integration/testing environments, and can be run by anyone.

If I specified a trigger, I can tell it only to fire on certain branches; but if I set trigger: none in my YAML then I can't apply those restrictions. Any ideas?

2 Answers

I came across this thread because I had the same requirement. In my case I wanted to restrict the pipeline to releases/* branches. In case that helps, what I did is to put a condition on the stage :

trigger:
  branches:
    include:
    - releases/*

...

- name: isReleasesBranch
  value: $[startsWith(variables['Build.SourceBranchName'], 'releases/')]

...

stages:
- stage: Production
  displayName: 'Production Deploy'
  dependsOn: QA
  condition: and(succeeded(), or(variables.isReleasesBranch))

...

In the end, I just went with the trigger approach; you typically won't be merging code back to master unless you're wanting to deploy it somewhere (hopefully to production).

Related