Why Azure Pipelines manual runs deploy the wrong branch
A manual Azure Pipeline run can deploy the wrong branch for simple reasons: the default branch was left selected, a similar branch name was chosen from autocomplete, a PR target branch was copied instead of the source branch, or the pipeline YAML ignores the selected branch in a later step.
The fix is to make the selected source branch visible and reduce the number of places where a user has to type or copy it.
Most common causes
The default branch was selected
The Run pipeline dialog often opens with the pipeline's default branch selected. If the user does not change it, the run may deploy main or develop instead of the feature branch.
The branch name was copied manually
Long ticket branch names are easy to mistype or choose incorrectly from autocomplete. Similar names such as feature/1234-checkout and feature/1234-checkout-fix are a common source of wrong-branch runs.
The PR target branch was used
For a PR from feature/checkout-flow into main, the branch to test before merge is usually the source branch. Copying the target branch deploys the shared branch instead.
The YAML checks out a different ref
Most pipelines check out the selected source branch by default. But custom checkout logic, scripts, or deployment templates can override that behavior. If the run claims to use one branch but deploys another, inspect the checkout and deployment steps.
Branch filters or conditions skip deployment stages
The run may start on the right branch, but a deployment stage may be skipped because a YAML condition only allows main or release/*. This can make users rerun the pipeline on a different branch until something deploys.
How to diagnose it
Add an early diagnostic step to print the branch Azure Pipelines thinks it is running on:
steps:
- script: |
echo "Build.SourceBranch=$(Build.SourceBranch)"
echo "Build.SourceBranchName=$(Build.SourceBranchName)"
displayName: Print selected branch Then check the run summary and logs:
- If
Build.SourceBranchis wrong, the run was queued with the wrong source branch. - If
Build.SourceBranchis right but the deployed artifact is wrong, inspect checkout, build caching, artifact download, and deployment scripts. - If the deployment stage skipped, inspect branch conditions and stage conditions.
Manual fix
- Open the work item or PR that contains the change.
- Copy the exact source branch, including the correct prefix such as
feature/orbugfix/. - Open the deployment pipeline.
- Click Run pipeline.
- Replace the default branch with the source branch you copied.
- Confirm any variables or parameters.
- Queue the run and verify
Build.SourceBranchin the logs.
BranchDeploy fix
BranchDeploy reduces wrong-branch runs by removing the manual branch-copy step. It reads the linked branch or PR source branch from the Azure Boards work item and shows it in a confirmation step before queueing the pipeline.
This does not replace good pipeline design. The pipeline still needs to deploy the selected source branch. But it prevents the most common input error: a person choosing the wrong branch in the Run pipeline dialog.
Prevention checklist
- Create branches from work items so the Development link is created automatically.
- Print
Build.SourceBranchearly in deployment pipelines. - Use branch allowlists to block branches that should never deploy to the target environment.
- Keep pipeline branch conditions aligned with the branches your team actually deploys.
- Use BranchDeploy for QA and UAT handoffs that start from Azure Boards work items.