Build and test on every push

Add a GitHub Actions workflow that installs, tests and builds your project whenever you push or open a pull request.

· Updated

Continuous integration runs your tests on every change, so a broken build is caught in minutes rather than in production. This guide adds a GitHub Actions workflow to a Node.js project; the same shape works for any language.

Before you start

  • A project on GitHub with a package.json that has test and build scripts.
  • Permission to push to the repository.

Steps

  1. Create the folder .github/workflows at the top of your repository.

  2. Save this in it as ci.yml:

    .github/workflows/ci.yml
    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: 22
              cache: npm
          - run: npm ci
          - run: npm test
          - run: npm run build
  3. Commit and push it:

    bash
    git add .github/workflows/ci.yml
    git commit -m "Add CI workflow"
    git push
  4. Open the repository on GitHub and select Actions. Your workflow runs, and each step shows its output.

What happens next

Every push to main and every pull request now runs the workflow, and pull requests show a tick or a cross beside each commit.

Tip

In Settings → Branches, add a rule for main that requires the test check to pass. Then nothing can be merged while the tests are failing.

Next steps