Skip to main content

Building a CI/CD Pipeline from Scratch Using GitHub Actions

·5 mins

CI/CD Pipeline
Image by Freepik

Building a CI/CD Pipeline from Scratch Using GitHub Actions #

As a DevOps engineer, one of my favorite tasks is automating workflows to make the development and deployment process seamless. Recently, I had the opportunity to explore GitHub Actions to set up a CI/CD pipeline for a Node.js application. Here, I’ll share my experience and guide you through creating your own pipeline from scratch. Even if you’re working with Python, the steps will be remarkably similar. Through trial and error, I discovered several tips and tricks that I’ll share along the way.


What is GitHub Actions? #

GitHub Actions is an automation tool provided by GitHub that enables you to create workflows directly in your repositories. These workflows can automate tasks like building, testing, and deploying your code. One of its standout features is its seamless integration with GitHub repositories, which simplifies setup and usage compared to other CI/CD tools.


Prerequisites #

Before we dive in, ensure you have the following:

  1. A GitHub account.
  2. A repository with a simple Node.js or Python application.
  3. Basic knowledge of YAML syntax (used for configuring workflows).
  4. Familiarity with running basic commands in your terminal.

If you’re working on a collaborative project, ensure you have the necessary permissions to add workflows to the repository.


Step 1: Setting Up Your Repository #

If you don’t already have a repository, create one:

  1. Log in to GitHub and click on New Repository.
  2. Initialize it with a README and clone it to your local system.
  3. Add your Node.js or Python application files and push them to GitHub.

For a Node.js app, ensure you include a package.json file. This file should define your dependencies and scripts like npm test and npm run build. For Python, include a requirements.txt file listing all the dependencies needed for your application.


Step 2: Creating Your First Workflow #

GitHub Actions workflows are defined in YAML files stored in the .github/workflows/ directory of your repository.

  1. In your repository root, create the directory:

    mkdir -p .github/workflows
    
  2. Create a new YAML file for your workflow, e.g., ci-cd-pipeline.yml:

    touch .github/workflows/ci-cd-pipeline.yml
    
  3. Open the file in your favorite editor and define the workflow:

Example Workflow for a Node.js Application #

name: CI/CD Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: "16"

      - name: Install Dependencies
        run: npm install

      - name: Run Tests
        run: npm test

      - name: Build Application
        run: npm run build

  deploy:
    runs-on: ubuntu-latest
    needs: build

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Deploy Application
        run: echo "Deploying application..." # Replace with your deployment script

Step 3: Understanding the Workflow #

Let’s break down what’s happening:

  1. Trigger Events:

    • The pipeline triggers on push and pull_request events to the main branch. You can customize this to fit your workflow, such as triggering on specific tags or other branches.
  2. Jobs:

    • Build Job: Checks out the code, sets up Node.js, installs dependencies, runs tests, and builds the application. Each step is a discrete task that simplifies debugging.
    • Deploy Job: Depends on the build job and deploys the application (replace the placeholder script with your actual deployment logic).
  3. Runs-on:

    • Specifies the environment where the job will run. Here, it’s using the latest version of Ubuntu.

Step 4: Committing and Testing the Workflow #

  1. Add the workflow file to your repository:

    git add .github/workflows/ci-cd-pipeline.yml
    git commit -m "Add CI/CD pipeline workflow"
    git push origin main
    
  2. Check the Actions tab in your GitHub repository. You should see the workflow executing automatically on the main branch. If there are any errors, the logs will provide detailed information to help you troubleshoot.

  3. Experiment with small changes to the workflow, such as modifying the test script or adding environment variables, to see how the pipeline adapts.


Step 5: Extending the Workflow #

Once your basic CI/CD pipeline is up and running, you can enhance it:

  • For Python Applications: Replace Setup Node.js and npm commands with Python equivalents:

    - name: Setup Python
      uses: actions/setup-python@v4
      with:
        python-version: "3.9"
    
    - name: Install Dependencies
      run: pip install -r requirements.txt
    
    - name: Run Tests
      run: pytest
    
  • Parallel Jobs: Add parallel jobs for tasks like running integration tests or building Docker images.

  • Add Deployment: Integrate with cloud services like AWS, Azure, or Google Cloud for production deployment. For instance, use AWS CLI or GitHub Actions integrations to push your application to an S3 bucket or deploy a container to ECS.

  • Environment-Specific Workflows: Create separate workflows for staging and production environments, each with distinct triggers and configurations.


Troubleshooting Tips #

  • Error Logs: Use the logs provided in the Actions tab to identify and debug issues. Pay close attention to syntax errors in YAML files as these can break workflows.
  • Secrets: For sensitive information like API keys, store them in GitHub Secrets and reference them in your workflow. This ensures security and keeps credentials out of your codebase.
  • Community Resources: Explore GitHub Marketplace for pre-built actions to simplify complex tasks.
  • Documentation: GitHub’s official Actions Documentation is a treasure trove of information and examples to help you troubleshoot and expand your workflows.

Conclusion #

Building a CI/CD pipeline using GitHub Actions was an exciting journey for me. One challenge I encountered was ensuring the pipeline supported both testing and deployment without introducing bottlenecks. By breaking down tasks into smaller, independent steps and leveraging parallel jobs, I was able to streamline the process and achieve faster results. It’s a powerful tool that simplifies the automation of repetitive tasks, enabling teams to focus on building great applications. Whether you’re working on a Node.js or Python project, the steps above should help you get started with ease.

Over time, as I explored additional features like matrix builds and reusable workflows, I realized how scalable and flexible GitHub Actions can be. If you’re just starting, don’t hesitate to experiment and tweak the pipeline to suit your project’s needs. Automation can seem daunting at first, but it becomes a rewarding experience once you see your workflows in action.

Happy automating!