Skip links

How to Set Up a CI/CD Pipeline with GitHub Actions: A Beginner’s Guide

Introduction

Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development. They ensure that every code change is automatically tested and deployed, reducing manual effort and the chance of errors.

In this beginner-friendly guide, you’ll learn how to create a simple yet powerful CI/CD pipeline using GitHub Actions. Whether you’re working on a Node.js app, Python script, or static site, GitHub Actions can streamline your workflow—directly within your GitHub repository.

What is GitHub Actions?

GitHub Actions is an automation platform integrated into GitHub. It allows you to build workflows that run on events like pushing code, opening a pull request, or releasing a new version. You can use it to:

  • Build and test code automatically
  • Deploy to servers, cloud providers, or static hosts
  • Run scheduled jobs like backups or cleanup scripts

Prerequisites

Before we start, make sure you have:

  • A GitHub account
  • A repository (public or private)
  • Basic knowledge of Git and your programming stack (e.g., Node.js, Python)

Step 1: Create a Workflow File

Workflows are defined using YAML files inside the .github/workflows directory of your repository. Let’s create a basic workflow:

name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

This configuration triggers the workflow whenever you push to or create a pull request targeting the main branch. It checks out your code, installs Node.js and dependencies, and runs your test suite.

Step 2: Commit and Push Your Workflow

Save the file as .github/workflows/ci.yml and push it to GitHub:

git add .github/workflows/ci.yml
git commit -m "Add basic CI workflow"
git push origin main

GitHub will automatically detect the workflow and run it. You can view its status in the Actions tab of your repository.

Step 3: Add Deployment to the Pipeline

To turn CI into CI/CD, let’s add a deployment step after tests pass. For example, you can deploy a static site to GitHub Pages:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      ...
      - name: Build the site
        run: npm run build

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./dist

Note: Make sure your build outputs to the correct folder (e.g., dist).

Secrets and Tokens

To deploy securely, you can store sensitive values (like API keys or deploy tokens) in GitHub Secrets:

  1. Go to your repository’s Settings > Secrets and Variables > Actions
  2. Click New repository secret
  3. Add your secret and reference it with ${{ secrets.YOUR_SECRET_NAME }} in workflows

Example Use Cases for GitHub Actions

  • Run unit tests on every pull request
  • Deploy static sites to Netlify or GitHub Pages
  • Publish npm packages automatically
  • Trigger Docker builds and push to Docker Hub
  • Send Slack or Discord notifications on deployment

Tips for Beginners

  • Start simple—add more complexity as needed
  • Use the actions/checkout and actions/setup-* actions
  • Look for prebuilt actions in the GitHub Marketplace
  • Use matrix builds to test across multiple versions
  • Review logs to debug failing builds

Conclusion

Setting up a CI/CD pipeline with GitHub Actions doesn’t have to be intimidating. With just a few lines of YAML, you can automate your entire workflow—from testing to deployment. As your project grows, so can your pipeline—with parallel jobs, environments, conditionals, and more.

Now that you’ve mastered the basics, start exploring the endless possibilities of GitHub Actions and make your development process faster, cleaner, and more efficient.

This website uses cookies to improve your web experience.