In GitLab CI/CD, if you want to ensure that only one pipeline runs at a time for a given branch or project, but within that single pipeline multiple stages can run (sequentially or in parallel as defined), you can use the resource_group or pipeline concurrency control features.
Key points:
- You want only one pipeline active at a time (no concurrent pipelines).
- Inside that pipeline, multiple stages/jobs can run as usual.
How to ensure only one pipeline runs at a time
Option 1: Use resource_group at the pipeline level (recommended)
resource_group is typically used to serialize jobs, but if you apply it to a job that runs early in the pipeline, it can effectively serialize pipelines.
Example:
stages:
- prepare
- build
- test
- deploy
prepare_job:
stage: prepare
script:
- echo "Preparing..."
resource_group: pipeline_lock
build_job:
stage: build
script:
- echo "Building..."
test_job:
stage: test
script:
- echo "Testing..."
deploy_job:
stage: deploy
script:
- echo "Deploying..."
- Here,
prepare_job uses resource_group: pipeline_lock.
- This means only one job with
resource_group: pipeline_lock can run at a time.
- Since this job is in the first stage, it effectively blocks other pipelines from starting their
prepare_job until the current one finishes.
- Other jobs in the pipeline run normally after
prepare_job.
Note: This approach serializes pipelines because the first job in each pipeline must acquire the same resource group lock.
Option 2: Use pipeline: concurrency (GitLab Premium/Ultimate feature)
GitLab Premium and above support pipeline concurrency control via the concurrency keyword in .gitlab-ci.yml.
concurrency:
group: $CI_COMMIT_REF_NAME
cancel_in_progress: true
group defines the concurrency group (e.g., per branch).
cancel_in_progress: true cancels any running pipeline in the same group when a new pipeline starts.
- This ensures only one pipeline per branch runs at a time.
- Inside the pipeline, stages/jobs run normally.
Option 3: Use only/except with manual pipeline triggers (less ideal)
You can manually control pipeline runs or use API to cancel running pipelines before starting a new one, but this is more complex and less automatic.
Summary
| Method | Description | Requires GitLab Plan |
|---|
resource_group on first job | Serializes pipelines by locking first job | Free and above |
concurrency keyword | Controls pipeline concurrency per branch | Premium/Ultimate |
| Manual control | Cancel pipelines via API or manual triggers | Any, but manual and complex |
If you want a simple free-tier solution, use resource_group on the first job in your pipeline to ensure only one pipeline runs at a time, while allowing multiple stages/jobs inside that pipeline.
If you want, I can help you write a full example .gitlab-ci.yml for your use case!