How to Set Up a CI Pipeline with GitHub Actions: A Step-by-Step Guide
Continuous Integration (CI) automates building and testing your code every time you push. GitHub Actions makes this easy with YAML-based workflows that run directly in your repository. In this guide, you’ll learn the essentials to create a basic CI pipeline that checks code quality and runs tests.
1. Create Your First Workflow File
In your repo, create a folder .github/workflows/ and add a file named ci.yml. This YAML file defines when and how the pipeline runs.
Define the Trigger and Job
- Trigger: Set
on: [push, pull_request]to run on every push or PR. - Job: Use
jobs:to define a job likebuild:that runs onubuntu-latest.
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
2. Add Steps for Dependencies and Testing
Each step runs sequentially. For a Node.js project, you’ll check out code, install packages (npm ci for clean installs), and then execute tests. Adjust commands for your language (e.g., pip install for Python, mvn test for Java).
3. Use Pre-Built Actions and Secrets
GitHub Marketplace offers actions like actions/setup-node to configure runtimes. To protect sensitive data, use Secrets (Settings > Secrets > Actions) and reference them as ${{ secrets.MY_SECRET }} in your workflow.
4. Customize with Matrix Builds and Caching
Speed up your pipeline:
- Matrix builds: Run tests across multiple OS/versions using
strategy.matrix. - Caching: Use
actions/cacheto storenode_modulesand avoid re-downloading dependencies.
Example Matrix Snippet
strategy:
matrix:
node-version: [14, 16, 18]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
Conclusion
You’ve now built a functional CI pipeline on GitHub Actions. Start with the basic workflow, then expand with caching, matrix builds, and custom actions. Every push will automatically validate your code, giving you faster feedback and a more robust development cycle.