Format GitHub Actions Workflow YAML
Format GitHub Actions workflow YAML files with correct indentation for jobs, steps, and matrix strategies. Validate common CI/CD workflow patterns and catch syntax errors.
Detailed Explanation
GitHub Actions YAML Formatting
GitHub Actions workflows are defined in YAML files stored in .github/workflows/. These files describe CI/CD pipelines with triggers, jobs, steps, and environment configurations. Consistent formatting is critical because GitHub Actions is strict about YAML structure.
Workflow File Structure
name: CI Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm test
deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Deploy to production
run: |
echo "Deploying..."
./deploy.sh
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
Key Formatting Rules
- Top-level keys —
name,on,permissions,env,jobsare all at the root level - Job indentation — Jobs are nested under
jobs:with 2-space indentation - Steps list — Each step starts with
-under thesteps:key - Multi-line scripts — Use the pipe (
|) operator for multi-lineruncommands - Expression syntax —
${{ }}expressions must not be confused with YAML syntax
Common Mistakes
- Forgetting to quote
on:— In some YAML parsers,onis interpreted as a boolean (true). GitHub handles this correctly, but other tools may not - Incorrect matrix indentation — The
matrixkey must be properly nested understrategy - Step ordering —
usesandrunare mutually exclusive within a single step - Missing pipe for multi-line commands — Without
|, line breaks inruncommands are collapsed
Reusable Workflows and Composite Actions
Formatting also applies to reusable workflow files (workflow_call trigger) and composite action definitions (action.yml). These follow similar YAML structure but with different top-level keys.
Use Case
GitHub Actions workflow formatting is essential for teams managing CI/CD pipelines. A misindented step or incorrectly nested matrix strategy can cause workflow failures that are hard to debug. Formatting workflows before committing catches structural errors early and makes pull request reviews of workflow changes much easier to follow.