Configure Task Dependencies and Execution Order
This document introduces how to control the execution order of multiple Jobs using needs and stages, achieving serial dependencies, parallel execution, and DAG topology orchestration.
When you need to control the execution order of multiple jobs to implement a serial process like build → test → deploy, or more complex DAG topologies.
Prerequisites
- The workflow contains multiple jobs.
- Understand the
needsdependency andstagesstage mechanism.
Quick Example
Method 1: Using needs Configuration
name: pipeline-with-needs
on:
push:
branches:
- main
jobs:
build:
name: Build
runs-on: [ubuntu-latest, x64, small]
steps:
- name: Run build
run: echo "build"
test:
name: Test
runs-on: [ubuntu-latest, x64, small]
needs: build
steps:
- name: Run test
run: echo "test"
deploy:
name: Deploy
runs-on: [ubuntu-latest, x64, small]
needs: test
steps:
- name: Run deploy
run: echo "deploy"
Method 2: Using stages Mechanism
name: pipeline-with-stages
on:
push:
branches:
- main
stages:
build-stage:
name: Build Stage
fail_fast: true
jobs:
build:
runs-on: [ubuntu-latest, x64, small]
steps:
- name: Run build
run: echo "build"
test-stage:
name: Test Stage
jobs:
unit-test:
runs-on: [ubuntu-latest, x64, small]
steps:
- name: Run unit test
run: echo "unit test"
integration-test:
runs-on: [ubuntu-latest, x64, small]
steps:
- name: Run integration test
run: echo "integration test"
deploy-stage:
name: Deploy Stage
jobs:
deploy:
runs-on: [ubuntu-latest, x64, small]
steps:
- name: Run deploy
run: echo "deploy"
Configuration Description
needs Dependency Mechanism
The needs configuration defines dependencies between jobs:
- The current job is executed only after the dependent job completes.
- The current job is executed only after all dependent jobs complete in parallel.
- By default, the current job does not execute if the dependent job fails (unless configured with
if: ${{ always() }}).
Serial dependency example:
jobs:
build:
name: Build
runs-on: [ubuntu-latest, x64, small]
steps:
- name: Run build
run: echo "build"
test:
name: Test
needs: build
runs-on: [ubuntu-latest, x64, small]
steps:
- name: Run test
run: echo "test"
deploy:
name: Deploy
needs: test
runs-on: [ubuntu-latest, x64, small]
steps:
- name: Run deploy
run: echo "deploy"