Automation

Azure DevOps: qualify an exclusive lock before canceling a queued production deployment

A production runbook for distinguishing an Azure DevOps exclusive lock from agent queueing, choosing runLatest or sequential, validating concurrency and preserving rollback before releasing production.

02 Nov 2026 azureazure-devopspipelinesenvironmentsexclusive-lockconcurrencydeploymentautomationdevopsobservabilityguardrailsrunbookrollbackproduction

A production stage has passed its approval, but remains waiting while another deployment uses the same Azure DevOps environment. A third run arrives with a newer artifact. The oldest run looks stuck, the release window is closing, and canceling everything except the latest run appears to be the quickest recovery.

That decision is safe only when releases are cumulative and the newest artifact truly supersedes the others. A database migration, infrastructure transition or tenant-by-tenant rollout may require every approved run to execute in order. This runbook separates an exclusive-lock wait from agent capacity and failed checks, then ends with an explicit decision: retain only the latest run, drain all runs sequentially, freeze the queue, or restore the last qualified release.

Freeze the deployment contract

Do not cancel or retry while the queue is still ambiguous. Record the protected resource, every run waiting for it, the artifact each run carries, and the version actually active in production.

yaml 01-exclusive-lock-evidence.yml
target:
environment: orders-production
service: orders-api
active_version: <runtime-version>
active_artifact_digest: sha256:<digest>
protected_resource:
type: environment
name: orders-production
check: exclusive-lock
runs:
- id: <active-run-id>
  branch: main
  commit: <sha>
  artifact_digest: sha256:<digest>
  stage_state: inProgress
- id: <waiting-run-id>
  branch: main
  commit: <sha>
  artifact_digest: sha256:<digest>
  stage_state: waiting
- id: <latest-run-id>
  branch: main
  commit: <sha>
  artifact_digest: sha256:<digest>
  stage_state: waiting
change_contract:
cumulative_releases: <true-or-false>
ordered_migrations: <true-or-false>
rollback_artifact: sha256:<known-good-digest>

The run number is not the release identity. Preserve the commit, immutable artifact digest, environment, approvals and any migration version. If the active production version cannot be attributed, stop adding runs: the first problem is state reconstruction, not lock policy.

Prove where the run is waiting

Azure DevOps evaluates checks on every protected resource consumed by a stage before the stage begins. The exclusive lock is the last check category. A stage waiting there has not yet entered its deployment job and has not acquired an agent for that job.

Separate these states:

text 02-classify-wait-state.txt
EXCLUSIVE LOCK WAIT
Stage shows approvals/checks in progress
Exclusive lock is the pending check
Another stage holds the same protected resource
Deployment job has not started

AGENT QUEUE
Checks have completed
Job is queued for an agent pool
Demands or parallel-job capacity block assignment

FAILED OR TIMED-OUT CHECK
A branch, approval, REST, business-hours or monitor check failed
Stage did not reach the exclusive lock
Retry requires new evidence for that check

TARGET-SIDE BUSY STATE
Job started and deployment engine reports another operation
Azure DevOps lock may already be released or cover the wrong resource
Diagnose the target lease separately

Adding agents does not release an exclusive lock. Bypassing an approval does not repair a lock scope. Inspect the environment, service connection, agent pool, variable group or other protected resources consumed by the stage and identify the resource whose check is pending.

Identify the real lock scope

The lock must represent the shared production state. If two pipelines deploy the same environment through different protected resources, each can acquire its own lock and still collide. Conversely, a single environment shared by unrelated services can serialize deployments that have no common state.

text 03-lock-scope-review.txt
One lock is appropriate when runs can modify the same:
application slot or namespace
database schema
traffic route
infrastructure state
tenant-wide configuration

Separate locks are appropriate when targets have:
independent runtime and data state
no shared migration or route
distinct rollback units
independent validation signals

Paths that must be inventoried:
YAML deployment jobs
manual pipeline runs
scheduled promotions
infrastructure pipelines
emergency and rollback pipelines

The environment deployment history helps attribute which pipeline reached the target. It does not prove that scripts outside this environment cannot write to the same target. Keep a target-side precondition when another control path exists.

Choose runLatest or sequential from release semantics

An exclusive lock check supports two behaviors. With runLatest, only the latest eligible run acquires the lock; this is the default when lockBehavior is omitted. With sequential, all runs acquire the protected resource in sequence.

Use runLatest when each artifact is cumulative, the latest approved release contains the earlier changes, and skipped runs have no independent side effects. Its replacement behavior is branch-sensitive, so explicitly test every branch that can reach the same production resource rather than assuming one global latest run. Use sequential when each run carries an ordered migration, a non-cumulative package, a tenant batch or an audit obligation that cannot be compacted.

Make the choice explicit in YAML.

yaml 04-production-lock-policy.yml
# Pipeline-wide policy: keep only the latest cumulative release.
lockBehavior: runLatest

stages:
- stage: Build
jobs:
- job: BuildAndTest
  steps:
  - script: ./ci/build-and-test.sh

- stage: DeployProduction
dependsOn: Build
jobs:
- deployment: DeployOrdersApi
  environment: orders-production
  strategy:
    runOnce:
      deploy:
        steps:
        - script: ./ops/preflight.sh
        - script: ./ops/deploy.sh
        - script: ./ops/validate.sh

For an ordered stage, scope the alternative locally:

yaml 05-sequential-migration-stage.yml
stages:
- stage: ApplyOrderedMigration
lockBehavior: sequential
jobs:
- deployment: ApplyMigration
  environment: orders-production
  strategy:
    runOnce:
      deploy:
        steps:
        - script: ./ops/check-migration-base.sh
        - script: ./ops/apply-migration.sh
        - script: ./ops/validate-migration.sh

Do not select sequential merely to avoid canceled runs. A long queue increases staleness: an old run can reach production after assumptions, approvals or incident state have changed.

Reject a stale run after it acquires the lock

The lock prevents overlap; it does not prove that the waiting artifact is still valid. Re-read production state inside the deployment stage, after the lock is acquired and immediately before the first write.

bash 06-preflight-after-lock.sh
set -euo pipefail

EXPECTED_BASE="<version-qualified-by-this-run>"
EXPECTED_DIGEST="sha256:<artifact-digest>"
ACTIVE_VERSION="$(./ops/read-active-version.sh)"
APPROVAL_EXPIRES_AT="<utc-timestamp>"

./ops/verify-approval-freshness.sh "$APPROVAL_EXPIRES_AT"
./ops/verify-artifact-digest.sh "$EXPECTED_DIGEST"

if [ "$ACTIVE_VERSION" != "$EXPECTED_BASE" ]; then
echo "Production changed while this run waited for the lock."
echo "Expected base: $EXPECTED_BASE"
echo "Active version: $ACTIVE_VERSION"
exit 42
fi

./ops/verify-no-target-side-operation.sh orders-production

The preflight must have no deployment effect. A mismatch should stop the run in a diagnosable state, not automatically force production back to the expected base. Revalidate the artifact against the new base or deliberately select a rollback.

Validate with three runs

Use a non-production environment with the same check configuration. Start one slow deployment, then queue two distinct immutable artifacts. Capture check events, stage states, job start times and the final active version.

text 07-three-run-validation.txt
TEST A — runLatest
Run 1 holds the lock
Runs 2 and 3 wait
Run 3 is the only waiting run allowed to acquire the lock
Final version equals run 3 artifact
No deployment job overlaps

TEST B — sequential
Run 1 holds the lock
Runs 2 and 3 acquire it in order
Each run revalidates its expected base
Final version equals run 3 artifact
No migration is skipped

TEST C — stale base
Change target state while a run waits
Waiting run acquires the lock
Preflight exits before the first write
Evidence identifies expected and observed base

Also exercise a manual rollback pipeline and any second pipeline targeting the same environment. The test is incomplete if only one YAML definition participates.

Decide release, hold or rollback

text 08-exclusive-lock-decision.txt
KEEP runLatest
Releases are cumulative
Latest artifact supersedes every waiting artifact
Skipped runs have no required side effects
Three-run validation ends on the latest digest

KEEP sequential
Every run represents a required ordered transition
Each transition is idempotent or checkpointed
Preflight rejects stale bases
Queue age and approval freshness are bounded

HOLD THE QUEUE
Lock owner cannot be identified
Production version is unknown
Pipelines use different locks for one target
A target-side operation can outlive the job
Migration order or artifact ancestry is ambiguous

ROLL BACK
Active run changed production but failed validation
Previous immutable artifact and data compatibility are proven
Rollback uses the same protected resource and lock
Post-rollback probes confirm version, health and migration state

Cancel only runs proven obsolete. Canceling a stage that already started can leave a partial target state; first determine whether the deployment command, migration or traffic switch completed. A rollback is a new serialized production change, not a way to escape the lock.

Conclusion

An Azure DevOps exclusive lock answers one question: which stage may consume a protected resource now? It does not establish artifact freshness, migration order, target-side exclusivity or the version currently serving traffic.

Keep runLatest for cumulative releases and sequential for transitions that must all run, but make either choice observable and test it with three competing runs. Release production only when the lock scope matches the real write boundary, the post-lock preflight accepts the current base, and a qualified rollback remains available.