In .gitlab-ci.yml, stages enforces order strictly inside a single pipeline. When two commits hit a branch back to back, GitLab starts both pipelines in parallel, kicking off a deployment race condition between them.
You push a feature, spot a minor bug, and push a quick fix right behind it. The first pipeline stalls on tests while the second finishes and deploys the fresh release to production. A minute later, the first pipeline reaches deploy and rolls out the old artifact on top. The UI shows two green checkmarks, logs are clean, and users see the fixed bug again.
Why resource_group Alone Is Not Enough
The first instinct is adding resource_group to your deploy job. It acts as a mutual-exclusion lock (mutex): while one job holding production deploys, others with the same key wait, while builds and tests keep running in parallel.
By default, however, resource_group uses process_mode: unordered. The lock prevents two simultaneous deploys, but it doesn't enforce queue order. If the first pipeline was delayed in tests and the second already released the lock, the older job happily starts next and rolls production backward.
Every deploy job targeting the same environment needs two fields together: a shared resource_group and a matching environment.
stages:
- build
- test
- deploy
build_app:
stage: build
script:
- echo "Building artifact..."
test_app:
stage: test
script:
- echo "Running tests..."
deploy_prod:
stage: deploy
script:
# Deploy the specific commit to production
- echo "Deploying $CI_COMMIT_SHORT_SHA to production"
# Environment GitLab uses to track release freshness
environment:
name: production
action: start
# Lock: only one job deploys to production at a time
resource_group: production
Next, turn on protection in project settings: go to Settings > CI/CD > General pipelines, check Prevent outdated deployment jobs (ci_forward_deployment_enabled in the API), and save. Now, if a newer deploy has already reached production, any late job fails with failed outdated deployment job.
Setting Queue Order with process_mode
If a branch receives frequent pushes, switch unordered via the GitLab REST API for that resource_group key.
process_mode | How the Next Job Is Picked | When It Fits |
|---|---|---|
unordered | Arbitrary (default) | Order doesn't matter |
oldest_first | Strictly oldest to newest (FIFO) | Every migration must run in order |
newest_first | Newest pipeline, including created | Rare pushes; frequent ones stall queue |
newest_ready_first | Newest among ready jobs (waiting_for_resource) | Active branches with frequent pushes |
newest_first can easily stall: it even considers created jobs still waiting on tests, so frequent pushes keep pushing back the start. For active branches, GitLab recommends newest_ready_first, which picks the newest job among those already ready to run.
# Switch queue mode from unordered to newest_ready_first
API="https://gitlab.example.com/api/v4/projects/<project_id>"
curl --request PUT \
--header "PRIVATE-TOKEN: <your_access_token>" \
--header "Content-Type: application/json" \
--data '{"process_mode": "newest_ready_first"}' \
"$API/resource_groups/production"
You run this request once after resource_group appears in your pipeline. Paired with Prevent outdated deployment jobs, the newest build ships while older ones get blocked. You can also add interruptible: true and auto-cancel redundant pipelines so stale builds don't waste runners.
Manual Runs, Rollbacks, and Staging Verification
GitLab measures a deploy job's 'age' by start time and pipeline ID, not commit timestamp. With protection enabled, the Run button on an outdated manual job is disabled with a warning.
For rollbacks, Settings > CI/CD > General pipelines offers Allow job retries for rollback deployments (ci_forward_deployment_rollback_allowed). It is enabled by default so you can re-run an older successful job via retry. If rollbacks must only go through new commits, uncheck it.
Before rolling this out to production, test the setup on staging.
Staging Race-Condition Protection Check
- Trigger two pipelines back to back and delay the first before deploy (for example,
sleep 60intest). - Confirm the second pipeline deploys the newer commit and deploy jobs do not overlap.
- Verify the older job fails with
failed outdated deployment job. - Check the actual deployed app version and the
Operate > Environmentslog.
A green checkmark in the pipeline list proves nothing — the actual version on the server and the deploy history in Operate > Environments are the real source of truth.


