How to Set Up GitHub Actions for CI/CD
Set up GitHub Actions for continuous integration and deployment. Automate tests and deploys on every push with a reusable workflow.
CI/CD in one file
GitHub Actions runs a YAML workflow on your repo’s runners. A single file in .github/workflows/ can install dependencies, run tests, build, and deploy, all triggered by a push or pull request.
Step 1: Create the workflow
.github/workflows/ci.yml:
name: CI
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- run: npm ci
- run: npm run typecheck
- run: npm test
- run: npm run build
- uses: actions/upload-artifact@v7
with:
name: dist
path: dist
npm ci installs exactly what package-lock.json pins, which is deterministic in CI (unlike npm install).
Step 2: Add caching and concurrency
The cache: npm option on setup-node already restores the npm cache between runs. Concurrency stops two deploys racing:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
cancel-in-progress kills a superseded run the moment a newer push lands, so you never pay twice for the same commit.
Step 3: Test multiple versions with a matrix
If your library or CLI must work across Node versions, a matrix runs the same job for each combination:
strategy:
matrix:
node-version: [22, 24]
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node-version }}
cache: npm
You get one check per version in the PR, each with its own logs, the cheapest way to catch a version-only regression before users do.
Step 4: Skip CI when you don’t need it
Include [skip ci] in a commit message and GitHub Actions skips the run entirely, handy for pure documentation edits that don’t need a full test cycle. It’s a small habit that keeps the queue focused on code that actually changes behavior.
Step 5: Deploy to Cloudflare Pages
The deploy job runs on a separate runner, so it cannot see the build job’s dist folder unless the build job uploads it and the deploy job downloads it. That is why Step 1 uploads the artifact, and this job downloads it first:
deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
permissions:
contents: read
deployments: write
steps:
- uses: actions/checkout@v7
- uses: actions/download-artifact@v8
with:
name: dist
path: dist
- uses: cloudflare/wrangler-action@v4
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy dist --project-name=your-project
Step 6: Add secrets
GitHub → Settings → Secrets and variables → Actions → New repository secret:
CF_API_TOKEN: a Cloudflare API token with Pages edit scope.CLOUDFLARE_ACCOUNT_ID: your Cloudflare account ID.
Secrets are encrypted at rest, masked in logs, and never exposed to forks’ pull requests.
Troubleshooting
npm cifails on a missing lockfile: commitpackage-lock.json; CI must not generate it.- Workflow doesn’t run: YAML must live in
.github/workflows/*.ymlon the default branch, and theon:triggers must match. - Deploy fails with no
distfolder: check the upload step in the build job and the download step in the deploy job. Jobs do not share a filesystem. - Secrets empty in a PR from a fork: by design. Secrets are only available in workflows from your own branches.
Summary
Every push to main now typechecks, tests, builds, and deploys automatically. The workflow is the source of truth for “is this shippable?”: no manual steps.
The same pattern extends further: How to Build and Secure an MCP Server shows how to add a protected API route to a workflow, and Deploy Astro on Cloudflare Pages covers the Pages-side setup this deploys to. Keep the workflow’s Node version aligned with the project’s .nvmrc or engines field so local and CI builds use the same runtime.
Comments
One comment per thread every 30 minutes · edits are unlimited.