CI/CD Pipelines with GitHub Actions
Continuous integration and continuous deployment turn "build, test, and ship" into something that happens automatically on every push, instead of a manual checklist someone has to remember. GitHub Actions makes this straightforward to set up directly inside your repository.
The Basic Building Blocks
A workflow lives in .github/workflows/ as a YAML file, triggered by events like a push or a pull request:
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test
Every push to main and every pull request now runs this job automatically, and GitHub shows the result directly on the PR.
Adding a Build and Deploy Step
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci && npm run build
- run: ./deploy.sh
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
needs: test makes sure deployment only runs after tests pass, and the if condition restricts it to the main branch so pull requests don't trigger a deploy.
Secrets, Not Hardcoded Values
Credentials and tokens belong in repository or environment secrets (Settings → Secrets and variables → Actions), referenced as ${{ secrets.NAME }}. They're encrypted at rest and masked in logs, unlike values committed to the YAML file.
Caching Dependencies
actions/setup-node can cache your package manager's store automatically:
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
This alone often cuts several seconds to minutes off every run, especially as a dependency tree grows.
Matrix Builds
To test against multiple versions or platforms without duplicating jobs:
test:
strategy:
matrix:
node-version: [18, 20, 22]
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
Summary
A CI/CD pipeline doesn't need to be elaborate to be valuable — even a workflow that just runs tests on every PR catches regressions long before they reach production. Start with test automation, then layer in build artifacts, deployment, and matrix testing as the project grows.
