I’m setting up YAML pipelines in Azure DevOps and want to control when deployment runs based on the branch and pipeline context.
Requirements: We use build validation on PRs targeting sprint/* and master.
During PR validation, we want to run only the build, not the deployment.
After the PR is merged to sprint/* or master, we want both build and deploy to run — but only after the build completes successfully. The issue I am having is the deploy is either triggered on all branch updates, or it doesn't trigger at all.
My Approach: To separate build and deploy, I’ve created two pipelines:
Build YAML:
# Pipeline name: Build Pipeline
trigger:
- master
- sprint/*
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
stages:
- stage: Build
displayName: 'Build Project'
jobs:
- job: BuildJob
steps:
- task: NuGetToolInstaller@1
- task: NuGetCommand@2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild@1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest@2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
Release YAML
# Pipeline name: Release Pipeline
trigger: none
resources:
pipelines:
- pipeline: build-pipeline
source: Build Pipeline
trigger:
stages:
- Build
branches:
- master
- sprint/*
pool:
vmImage: windows-latest
stages:
# Release stages, jobs, and steps here
Issue: The build pipeline runs as expected for both PR validations and merges.
The deploy pipeline does NOT trigger after a successful build on master or sprint/*.
If I remove trigger: none from the deploy pipeline, it runs on all branches — which I don’t want.
What I need: How can I configure my YAML so that:
PRs trigger only the build pipeline for validation.
After merging to sprint/* or master, the deploy pipeline runs only after a successful build pipeline completes on those branches.
I’ve followed Microsoft’s docs on pipeline resources, but it’s still not behaving as expected.
There was a good answer that appears to have been deleted, so I'll summarize what was in that answer that solved the issue.
The trigger: none
is required to prevent execution of the triggered pipeline by other commits, and the triggers will not work unless the YAML is in the default branch (for example main or master). Once the YAML was committed to the master branch, the trigger started working correctly.