How to Set Up Continuous Integration with GitHub Actions: A Step-by-Step Guide
Continuous Integration (CI) automatically builds and tests your code every time you push changes, catching bugs early. GitHub Actions makes this simple by letting you define workflows directly in your repository. This tutorial walks you through setting up a basic CI pipeline for a Node.js project – the same pattern works for any language.
To follow along, you need a GitHub repository with a package.json file (or equivalent) that includes a test script. No external CI server required – everything runs inside GitHub’s infrastructure. Let’s get started.
Step 1: Create a Workflow File
Inside your repository, create the folder .github/workflows. Then add a YAML file, for example ci.yml. This file defines your CI pipeline.
- In your GitHub repo, click the “Actions” tab and select “New workflow”. Or manually create the file locally and push it.
- Name the file something descriptive like
node-ci.yml. - Use a simple template to start; you can always edit it later.
Step 2: Define the Trigger
Specify when the workflow should run. The most common trigger is on every push or pull request to the main branch.
- Start with
name: Node.js CI. - Add
on: [push, pull_request]to run on any branch, oron: push: branches: [main]to limit to the main branch only. - You can also filter by file paths to avoid running CI on documentation changes.
Step 3: Set Up the Job
Jobs run on a virtual machine. Use runs-on: ubuntu-latest. Each job contains a series of steps.
- Step 1:
actions/checkout@v3– checks out your repository code. - Step 2:
actions/setup-node@v3withnode-version: '18'– installs the Node.js version you need. - Step 3:
run: npm ci– installs dependencies (faster and more reliable thannpm install). - Step 4:
run: npm test– runs your test suite.
Step 4: Commit and Verify
Push the workflow file to your repository. GitHub will automatically detect it and start the CI pipeline. Go to the “Actions” tab to see the run.
- Green checkmark means all steps passed. Red means a failure – click into the run to view logs and debug.
- If tests fail, check your YAML syntax or ensure your test script is correct in
package.json. - Once verified, you can add extra steps like linting, code coverage, or deployment to staging.
That’s it! You now have a fully automated CI pipeline with GitHub Actions. Every commit to your repository will trigger a build and test run, keeping your codebase healthy. Start simple and expand as your project grows.