Write a GitHub Actions workflow that runs tests on every PR and push to main.
BeginnerHands-onWrite-a-pipeline tasks
Answer
name: ci
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm test -- --ci
Key trick → cache: npm in setup-node caches downloads keyed on the lockfile, and a top-level permissions: contents: read makes the GITHUB_TOKEN least-privilege.
More on Write-a-pipeline tasks
Write a workflow that builds a Docker image and pushes it to ECR using OIDC (no access keys).IntermediateWrite a matrix job that tests on Python 3.10, 3.11 and 3.12 on Linux and Windows.BeginnerWrite a declarative Jenkinsfile with build, test, Docker push and a manual approval before prod.AdvancedWrite a GitHub Actions deploy job that requires approval for production using environments.BeginnerWrite a reusable workflow and call it from another repo.IntermediateWrite a workflow that only runs the backend job when files under `backend/` change.Intermediate