GitHub_Actions_and_Pipelines
GitHub Actions and Pipelines
Advanced workflow patterns.
π Reusable Workflows
Create Reusable Workflow
.github/workflows/reusable-test.yml:
name: Reusable Test
on:
workflow_call:
inputs:
node-version:
required: true
type: string
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- run: npm test
Use Reusable Workflow
jobs:
call-tests:
uses: ./.github/workflows/reusable-test.yml
with:
node-version: "20"
Calls reusable workflow.
π¦ Composite Actions
Create Composite Action
.github/actions/setup/action.yml:
name: "Setup Project"
description: "Setup Node and install deps"
runs:
using: "composite"
steps:
- uses: actions/setup-node@v4
with:
node-version: "20"
- shell: bash
run: npm ci
Use Composite Action
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
- run: npm test
Uses local composite action.
π Matrix Builds
Basic Matrix
strategy:
matrix:
node: [16, 18, 20]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
Runs on multiple versions and OS.
Include/Exclude
strategy:
matrix:
node: [16, 18, 20]
os: [ubuntu-latest, windows-latest]
exclude:
- node: 16
os: windows-latest
include:
- node: 20
os: macos-latest
Customizes matrix combinations.
π Job Dependencies
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: npm run build
test:
needs: build
runs-on: ubuntu-latest
steps:
- run: npm test
deploy:
needs: [build, test]
runs-on: ubuntu-latest
steps:
- run: npm run deploy
Deploy runs after build AND test.
π€ Artifacts
Upload Artifact
- uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/
Saves build output.
Download Artifact
- uses: actions/download-artifact@v4
with:
name: build-output
Gets artifact from earlier job.
π Secrets in Actions
Use Secrets
env:
API_KEY: ${{ secrets.API_KEY }}
run: |
npm run deploy
Uses repository secret.
β° Conditional Execution
If Condition
steps:
- name: Deploy to prod
if: github.ref == 'refs/heads/main'
run: npm run deploy-prod
Only runs on main branch.
On Success/Failure
- name: Notify on failure
if: failure()
run: echo "Build failed!"
- name: Cleanup
if: always()
run: npm run cleanup
Runs based on status.
π‘ Tips
Cache Dependencies
- uses: actions/cache@v4
with:
path: node_modules
key: modules-${{ hashFiles('package-lock.json') }}
Concurrency
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Cancels duplicate runs.
π Related
#github #actions #pipeline #advanced