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.jsonthat hastestandbuildscripts. - Permission to push to the repository.
Steps
Create the folder
.github/workflowsat the top of your repository.Save this in it as
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 buildCommit and push it:
git add .github/workflows/ci.yml git commit -m "Add CI workflow" git pushOpen 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.