GitHub Actions
Automate workflows with CI/CD
Skills taught: github.actions, github.workflows, github.ci-cd
Your first workflow: run tests on every PR
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
# 1. Check out the code
- uses: actions/checkout@v4
# 2. Set up Node.js
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
# 3. Install dependencies
- run: npm ci
# 4. Run tests
- run: npm test
# 5. Build
- run: npm run build
Deploy workflow: GitHub Pages
name: Deploy
on:
push:
branches: [ main ]
permissions:
contents: read
pages: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm run build
env:
VITE_BASE: /
- uses: actions/upload-pages-artifact@v3
with:
path: ./dist
deploy:
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
uses: actions/deploy-pages@v4
Using secrets
# Add secrets in: repo → Settings → Secrets and variables → Actions
# Then use them as environment variables:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- run: npm run deploy
env:
API_KEY: ${{ secrets.MY_API_KEY }} # from GitHub Secrets
NODE_ENV: production # plain value
# Common secrets:
# VERCEL_TOKEN — deploy to Vercel
# AWS_ACCESS_KEY — deploy to AWS
# SLACK_WEBHOOK — post notifications to Slack
Frequently Asked Questions
How do I avoid exposing secrets in Actions logs?
Use ${{ secrets.SECRET_NAME }} — GitHub automatically redacts secrets from logs. Never echo or print a secret directly. For complex secrets, use OIDC (keyless auth) instead of storing long-lived credentials at all.
My workflow is slow — how do I speed it up?
Add caching: actions/cache or the cache: 'npm' option in actions/setup-node. Break independent jobs out so they run in parallel. Use a matrix only when you genuinely need multiple OS/version coverage.