Jobs & Steps
Parallel Execution¶
Steps can run in parallel using the strategy key, which is a job-level configuration. This allows you to define multiple job runs with different parameters.
Example: Parallel Job with Strategy
jobs:
build-and-test:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- name: Run tests
run: npm test
Note: The strategy key must be placed at the job level, not under steps. Matrix strategies define how a job splits into multiple parallel runs based on specified parameters (e.g., OS, Node.js versions).
Conditional Logic for Workflow Control¶
Use if: conditions to control whether steps or jobs execute based on dynamic criteria. Conditions evaluate expressions that determine workflow flow.
Example: Conditional Step Based on Environment Variable
Example: Conditional Job Based on Branch
jobs:
deploy:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
steps:
- name: Deploy application
run: ./deploy.sh
Key Concepts:
- Use $ to reference variables, job status, or context values.}
- Combine conditions with logical operators (&&, ||, !) for complex rules.
- Conditional logic enables workflows to skip steps/jobs, reroute execution, or adapt to runtime inputs.