Concurrency Control and Job Dependencies
What you'll learn: How to prevent workflow conflicts by controlling when runs execute and orchestrating complex multi-job flows with dependencies.
The Core Problem
When multiple workflow runs trigger simultaneously—say, from rapid commits—they can conflict with shared resources like deployments or test environments. Meanwhile, complex pipelines often need jobs to execute in a specific order, with some waiting for others to complete successfully.
Concurrency Control
GitHub Actions lets you prevent multiple runs from executing simultaneously using the concurrency key. Think of it like a single-occupancy restroom—only one workflow can "enter" at a time.
concurrency:
group: production-deploy
cancel-in-progress: true
The group names your concurrency boundary (workflows with the same group wait for each other). Setting cancel-in-progress: true terminates older runs when a newer one starts—perfect for deployments where only the latest code matters.
You can make groups dynamic using contexts:
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: false
This creates separate concurrency groups per branch, so main and staging deployments don't block each other.
Complex Job Dependencies with needs
While you've seen basic needs for job ordering, you can express sophisticated dependency graphs:
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo "Testing..."
deploy-staging:
needs: test
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to staging..."
deploy-prod:
needs: [test, deploy-staging]
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to production..."
Here, deploy-prod waits for both test and deploy-staging to succeed—a diamond dependency pattern.
Key Takeaway: Use concurrency groups to prevent resource conflicts and cancel outdated runs; use multi-value needs arrays to orchestrate complex job execution flows that ensure proper sequencing and dependencies.