CI/CD with GitHub Actions: Patterns That Work
GitHub Actions has become the default CI/CD platform for most teams, and for good reason. It's deeply integrated with your code, the marketplace has actions for nearly everything, and the free tier is generous. But the difference between a working pipeline and a fast, reliable one is significant.
Speed comes first
Speed is the most important quality of a CI pipeline. If CI takes 20 minutes, developers stop running it before pushing. They batch changes, context-switch while they wait, and start treating a red build as someone else's problem.
Target under 5 minutes for the critical path: lint, type check, test. Everything else can run in parallel or after merge.
Cache everything you can
Caching is how you get fast.
- Cache
node_moduleswithactions/cachekeyed on your lockfile hash. - Cache Docker layers with
docker/build-push-action's built-in cache. - Cache Terraform provider plugins.
- uses: actions/cache@v4
with:
path: node_modules
key: deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
Keying on the lockfile hash means the cache is reused until dependencies actually change. Every minute saved per run multiplies across every PR, every developer, every day.
Run jobs in parallel
The job structure matters. Separate lint, type check, test and build into individual jobs that run in parallel. Use needs to express dependencies. Deploy needs build, but lint and test can run simultaneously.
jobs:
lint:
runs-on: ubuntu-latest
steps: [...]
test:
runs-on: ubuntu-latest
steps: [...]
build:
runs-on: ubuntu-latest
steps: [...]
deploy:
needs: [lint, test, build]
runs-on: ubuntu-latest
steps: [...]
This parallel execution can cut pipeline time in half. It also gives clearer feedback: a failed lint job says so by name, without anyone scrolling through one long log.
Deploy without service account keys
For deployment, Workload Identity Federation eliminates service account keys entirely.
The old approach is to store a GCP service account key as a GitHub secret. That key can leak, and it never expires. With WIF, GitHub authenticates directly with GCP using short-lived tokens.
permissions:
id-token: write
contents: read
steps:
- uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ vars.WIF_PROVIDER }}
service_account: ${{ vars.DEPLOY_SERVICE_ACCOUNT }}
The setup is more complex initially but dramatically more secure. There is no long-lived credential left to steal.
Enforce quality with branch protection
Branch protection rules enforce CI quality gates.
- Require status checks to pass before merging.
- Require PR reviews.
- Prevent force pushes to main.
These rules transform CI from a suggestion into a guarantee. A check that can be skipped will be skipped on the day it matters.
Share pipelines with reusable workflows
Reusable workflows, using the workflow_call trigger, eliminate duplication across repositories. Define your standard CI pipeline once and call it from each repository's workflow file.
jobs:
ci:
uses: my-org/workflows/.github/workflows/node-ci.yml@v1
with:
node-version: 24
When you update the shared workflow, every repository benefits. This is particularly valuable for organizations with many microservices, where thirty copies of the same YAML would otherwise drift apart.
Test combinations with matrix builds
Matrix builds handle testing across multiple versions or configurations. Test against two Node versions. Run against two PostgreSQL versions.
strategy:
matrix:
node: [22, 24]
postgres: [16, 17]
The matrix strategy runs these combinations in parallel without duplicating workflow code.
Deploy a preview for every pull request
One pattern I've adopted recently: deploy previews for every PR. A GitHub Actions workflow deploys the PR's branch to a temporary Cloud Run revision with a unique URL.
Reviewers can test the actual deployment, not just read the code. A layout bug or a broken form is obvious in a running app and easy to miss in a diff. The preview is cleaned up when the PR is merged or closed.
Pin third-party actions to a commit SHA
Security: never use third-party actions with @main or @latest. Pin to a specific commit SHA.
# Instead of some-org/some-action@v3
- uses: some-org/some-action@<full-commit-sha>
An action author can push malicious code to a tag or branch. Pinning to a SHA makes this impossible, because a SHA always points at the same code. Renovate or Dependabot can automate SHA updates for you, so pinning doesn't mean falling behind.
The patterns in one list
- Keep the critical path under five minutes.
- Cache dependencies, Docker layers and provider plugins.
- Split work into parallel jobs.
- Authenticate with Workload Identity Federation.
- Require checks and reviews with branch protection.
- Share pipelines through reusable workflows.
- Use a matrix for versions and configurations.
- Give every pull request a preview deployment.
- Pin third-party actions to a commit SHA.
The same pipeline shape works for infrastructure. I describe that in Terraform at Scale: Managing Multi-Environment GCP Infrastructure. For the image you're building along the way, see Docker for Production: Beyond the Basics.