How to Set Up a CI/CD Pipeline with GitHub Actions: A Step-by-Step Guide
GitHub Actions automates software workflows directly in your repository. With CI/CD, you can build, test, and deploy your code automatically every time you push changes. This eliminates manual errors and speeds up delivery. All you need is a GitHub repository and a YAML workflow file.
A workflow is a configurable automated process defined in .github/workflows/. It runs when a specific event occurs, like a push or pull request. You can run multiple jobs in parallel or sequence, each with its own set of steps. The rest of this tutorial shows you how to create a basic CI/CD pipeline.
1. Create Your First Workflow File
In your repository, create a new directory .github/workflows/. Inside, add a .yml file, for example ci.yml. Name it whatever fits your project. Every workflow starts with:
name:– A descriptive name for the workflow.on:– The event that triggers the workflow, e.g.,push,pull_request.
2. Define the Trigger
Specify when the pipeline runs. For a typical CI/CD pipeline, trigger on pushes to the main branch and pull requests:
on:
push:
branches: [main]
pull_request:
branches: [main]
You can also schedule runs with schedule or trigger manually via workflow_dispatch.
3. Add Jobs and Steps
Under jobs:, define a job (e.g., build). Each job runs on a runner – use ubuntu-latest for Linux. Steps can check out code, set up dependencies, run tests, and deploy. Example:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
4. Deploy Using Secrets
For deployment, store sensitive data (API keys, SSH keys) in GitHub Secrets (Settings → Secrets and variables). Reference them as ${{ secrets.MY_SECRET }}. Add a deployment step using a ready-made action or a custom script.
- name: Deploy to production
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
run: ./deploy.sh
Conclusion
GitHub Actions makes CI/CD simple. Start with a basic workflow, test it, then expand with parallel jobs, caching, or custom actions. Your code will move from commit to production automatically, reducing manual work and increasing reliability.