Azure Pipelines does not inherently support pausing and resuming builds based on pull request (PR) approval counts directly within the YAML pipeline configuration. However, you can achieve a similar outcome by using branch policies and environment approvals in Azure DevOps.
Here's how you can approach it:
-
Branch Policies for PR Approvals: First, configure branch policies on your target branch (e.g., main or develop) to require a minimum number of reviewer approvals before a PR can be completed. This ensures that no code gets merged without the necessary approvals.
-
Environment Approvals for Deployments: For the scenario where you want to pause a deployment (rather than a build, since builds typically compile/test code and you wouldn't usually halt this process for approvals), you can use environments with approval checks in your pipeline.
Given these constraints, here's an illustrative example that involves using an environment with manual approval as a way to introduce a "pause" in your pipeline:
Step 1: Configure Branch Policies
- Go to your Azure DevOps project.
- Navigate to Repos > Branches.
- Next to your target branch, click the three dots (...) and select "Branch policies".
- Under "Pull request validation", add a build policy that triggers your build pipeline on PR creation or update.
- Ensure you set "Minimum number of reviewers" under "Reviewer requirements" to 2 (or your desired number) and check "Require a minimum number of approvals".
Step 2: Define an Azure Pipeline with an Environment for Deployment
trigger: none # Avoid automatic triggering
pr:
branches:
include:
- main # Adjust to your target branch
stages:
- stage: Build
jobs:
- job: BuildJob
pool:
vmImage: 'ubuntu-latest'
steps:
- script: echo "Build steps go here"
- stage: Deploy
dependsOn: Build
condition: succeeded()
jobs:
- deployment: DeploymentJob
environment: 'staging' # Adjust to your target environment
pool:
vmImage: 'ubuntu-latest'
strategy:
runOnce:
deploy:
steps:
- script: echo "Deployment steps go here"
Step 3: Configure Environment with Approvals
- Go to Pipelines > Environments.
- Click "New environment", name it (e.g.,
staging), and create.
- Inside the environment, click "More actions" (three dots), then "Approvals and checks".
- Add an "Approval" check. You can specify approvers here; however, for your requirement, you might not specify individual users but use this step as a manual gate after the PR is approved based on your branch policies.
Caveats
- This doesn't pause the build but rather the deployment stage, acting as a manual check following PR approvals and build completion.
- Direct build pausing until PR approval isn't natively supported as build policies are designed to ensure code quality before merge, rather than controlling pipeline progression.
This approach ensures that your code is built and tested automatically on PR creation/update, but deployment requires manual approval, indirectly ensuring that at least the branch policies (including PR approvals) are satisfied before proceeding.