A reliable GitHub Actions pipeline should make the same decisions every time: install the intended runtime, validate the code, build a deployable artifact, and release it only under the right conditions. This guide provides reusable GitHub Actions workflow examples and a practical checklist for pull requests, pushes, matrix testing, caching, secrets, approvals, and deployment.
Overview
GitHub Actions CI/CD workflows are defined in YAML files inside .github/workflows/. A workflow can run when code is pushed, when a pull request changes, on a schedule, or when someone starts it manually. Each workflow contains one or more jobs, and each job runs a sequence of steps on a hosted or self-managed runner.
A useful web project pipeline normally separates verification from release:
- Pull request checks: install dependencies, format-check files, lint, run tests, and build the project.
- Branch checks: repeat the required checks after code is merged into a protected branch.
- Deployment: publish only after verification succeeds, usually from a designated branch or release event.
Keeping these responsibilities visible makes failures easier to diagnose. It also prevents a deployment job from quietly becoming the only place where important tests run.
Choose triggers deliberately
| Trigger | Best use | Check before enabling |
|---|---|---|
pull_request | Validate proposed changes before merging | Whether forked contributions can access secrets |
push | Check commits on a branch or start a release | Which branches should run deployment jobs |
workflow_dispatch | Run a controlled manual build or deployment | Required inputs and approval expectations |
schedule | Run maintenance or periodic checks | Whether the job is safe to run without a code change |
For many repositories, a pull request workflow and a separate deployment workflow are easier to reason about than one large file. If you keep them together, use explicit job conditions so a pull request cannot accidentally deploy.
Checklist by scenario
1. Pull request validation
Start with a small workflow that proves the project can be installed and checked from a clean environment. Adapt the commands to the package manager and framework used by the repository.
name: Validate
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Check formatting
run: npm run format:check
- name: Lint
run: npm run lint
- name: Test
run: npm test -- --runInBand
- name: Build
run: npm run build
Replace npm ci and the scripts with the commands your project actually supports. A workflow should fail when a required script is missing rather than silently skipping the check. If formatting is not part of the project, remove that step or add a clearly defined formatter check.
2. Testing multiple runtime versions
Use a matrix when compatibility across several supported runtime versions matters. Keep the matrix limited to versions you intend to support; testing arbitrary versions increases execution time without improving the release decision.
jobs:
test:
strategy:
fail-fast: false
matrix:
node: [18, 20, 22]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm test
Document why each matrix value exists. If only one version may publish, keep deployment in a separate job rather than allowing every matrix copy to deploy.
3. Caching dependencies
Dependency caching can make repeated workflows faster, but it should not replace a clean install. Configure caching through the runtime setup action where supported, and ensure the cache key changes when the lockfile changes. Commit the lockfile and use the package manager's lockfile-aware install command.
For a project using another package manager, configure the corresponding cache dependency path, such as the relevant lockfile. Do not copy an npm cache configuration into a pnpm or Yarn workflow without checking the package manager's expected files and install command.
4. Deploying after a successful build
Build and deployment should have a clear dependency. A simplified pattern is:
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
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: site-build
path: dist/
deploy:
needs: build
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: ubuntu-latest
environment: production
permissions:
contents: read
steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: site-build
path: dist/
- name: Deploy
run: ./scripts/deploy.sh
The final deployment command depends on the hosting provider. Keep provider-specific credentials in the repository's environment or secret store, and use the provider's documented action or CLI rather than placing tokens in YAML.
5. Adding an approval gate
Assign the deployment job to a protected environment such as production. Configure required reviewers and environment-specific secrets in repository settings. The workflow then pauses before the job can access protected values or complete its release, according to the environment configuration.
What to double-check
- Runtime and package manager: The runner version, Node.js version, package-manager version, lockfile, and install command should agree. Avoid relying on an unpinned default runtime.
- Permissions: Declare only the permissions jobs require. A read-only
contentspermission is a sensible starting point for validation jobs. - Secrets: Secrets should be referenced through expressions such as
${{ secrets.DEPLOY_TOKEN }}, never committed to the repository or printed in logs. Treat pull requests from untrusted contexts carefully. - Action versions: Review the major version or immutable reference used by marketplace actions. Update action references deliberately and test the workflow after changes.
- Build output: Confirm the output directory, generated files, redirects, environment configuration, and asset paths before deployment.
- Concurrency: Prevent obsolete deployments from racing with newer ones when only the latest branch revision should publish.
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: true
Use cancellation carefully. It is useful for preview or branch deployments, but a production release may need a different policy if an in-progress deployment must finish safely.
Before release, also test the application itself. For accessible UI checks, see the web accessibility testing workflow and color contrast checker guide. If the pipeline tests an API, a repeatable cURL API testing workflow can make request and authentication failures easier to reproduce.
Common mistakes
- Running deployment on every event. Scope the deployment job to an explicit branch, tag, or manual event.
- Using secrets in pull request jobs without checking the source. Fork-based pull requests and untrusted changes require a conservative secret-access design.
- Installing with a command that ignores the lockfile. Reproducible CI depends on installing the declared dependency graph.
- Testing only the build. A successful build does not prove that linting, unit tests, accessibility checks, or runtime configuration are correct.
- Duplicating YAML across many workflows. Repeated setup steps drift. Consider reusable workflows or composite actions once the process is stable.
- Hiding failures behind conditional steps. Conditions should express a deliberate policy, not conceal a command that is failing or missing.
- Debugging only from the final error line. Inspect the first failing step, expanded inputs, runner operating system, dependency install output, and relevant environment variables without exposing secrets.
When to revisit
Review the workflow whenever the underlying inputs change. That includes changing the framework, package manager, lockfile, build directory, supported runtime, hosting provider, branch model, or deployment credentials. Revisit it before a seasonal planning cycle or a major release so obsolete checks do not become release blockers.
Use this short maintenance checklist:
- Run the workflow from a fresh branch and confirm that a clean install succeeds.
- Open a test pull request to verify validation triggers and that no deployment occurs.
- Merge a controlled change and confirm the intended branch workflow starts exactly once.
- Test a failed lint, test, or build step and verify that deployment is blocked.
- Review action references, runner images, runtime versions, permissions, secrets, and environment approvals.
- Confirm that logs provide enough diagnostic information without revealing credentials or sensitive configuration.
- Record the supported commands and deployment assumptions near the workflow so the next editor can update it safely.
Start with one dependable validation workflow, then add matrix testing, artifacts, approvals, and deployment automation as the project requires them. A smaller pipeline with explicit conditions is usually easier to trust than a large workflow whose behavior nobody can explain.