DEV Community

Isaac Tonyloi - SWE
Isaac Tonyloi - SWE

Posted on

4 1 1 1 1

Getting Started with CI/CD: A Beginner's Guide to Automating Your First Pipeline (With Github Actions)

Continuous Integration and Continuous Deployment (CI/CD) are essential for modern software development, enabling teams to automate the process of building, testing, and deploying applications. A well-designed CI/CD pipeline ensures faster releases, improved code quality, and more efficient collaboration among developers.

In this guide, we’ll explore GitHub Actions, a powerful CI/CD tool that seamlessly integrates with GitHub repositories. Whether you’re an individual developer or part of a large team, GitHub Actions provides a flexible and scalable solution for automating software workflows. This guide will take you through the entire process of setting up a CI/CD pipeline using GitHub Actions—from creating workflows to deploying applications.

What is CI/CD?

Before diving into GitHub Actions, let’s break down the key components of CI/CD.

Continuous Integration (CI)

Continuous Integration is the practice of frequently integrating code changes into a shared repository. Every code commit triggers an automated process that builds, tests, and validates the application.

Key benefits:

  • Frequent Code Merges: Developers push code changes often, reducing integration issues.
  • Automated Testing: Code is tested automatically after each commit.
  • Early Bug Detection: Issues are identified and fixed before deployment.

Continuous Delivery (CD)

Continuous Delivery extends CI by ensuring that code is always in a deployable state. Code changes pass through automated testing and are prepared for deployment.

Key benefits:

  • Reliable Releases: Code is always tested and packaged for production.
  • Manual Approval for Production: Deployment is automated up to the staging environment but requires human approval for production.
  • Fast Feature Releases: Teams can release updates more frequently.

Continuous Deployment

Continuous Deployment takes things further by automating deployments to production without manual approval. Every successful code change is deployed immediately.

Key benefits:

  • Zero Manual Intervention: If all tests pass, the change is deployed instantly.
  • Faster Software Releases: New features and bug fixes reach users quickly.
  • Efficient DevOps Process: Developers can focus on writing code rather than deploying it.

Benefits of Using GitHub Actions for CI/CD

GitHub Actions provides a native CI/CD experience directly within GitHub repositories, offering several advantages:

Tight GitHub Integration: Workflows run directly in your GitHub repository, reducing the need for third-party tools.

Easy-to-Use YAML Configuration: Define workflows using simple YAML files.

Pre-Built Actions & Marketplace: Access a vast collection of pre-configured actions for various tasks.

Scalability & Flexibility: Run workflows on different environments, including Linux, Windows, and macOS.

Event-Driven Triggers: Workflows can be triggered by push events, pull requests, issue creation, and more.

Now, let’s set up a CI/CD pipeline with GitHub Actions for a sample application.

How to Set Up a CI/CD Pipeline with GitHub Actions

Step 1: Set Up a GitHub Repository

Before configuring GitHub Actions, ensure your project is stored in a GitHub repository.

  1. Go to GitHub and create a new repository.
  2. Clone the repository to your local machine:
   git clone https://github.com/your-username/my-ci-cd-project.git
   cd my-ci-cd-project
Enter fullscreen mode Exit fullscreen mode
  1. Add your project files (e.g., a Node.js application with package.json and a test directory).
  2. Commit and push your code:
   git add .
   git commit -m "Initial commit"
   git push origin main
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a GitHub Actions Workflow

GitHub Actions workflows are defined using YAML files stored in the .github/workflows/ directory.

  1. Inside your project, create a new directory:
   mkdir -p .github/workflows
Enter fullscreen mode Exit fullscreen mode
  1. Create a new file named ci-cd-pipeline.yml:
   touch .github/workflows/ci-cd-pipeline.yml
Enter fullscreen mode Exit fullscreen mode

Step 3: Define the CI/CD Workflow

Now, let’s define a basic CI/CD pipeline for a Node.js application.

Open ci-cd-pipeline.yml and add the following configuration:

name: CI/CD Pipeline

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

jobs:
  build-test:
    runs-on: ubuntu-latest

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

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

      - name: Install Dependencies
        run: npm install

      - name: Run Tests
        run: npm test

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

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

      - name: Deploy to Production
        run: echo "Deploying application..."
Enter fullscreen mode Exit fullscreen mode

Breaking Down the Workflow

  • Triggers (on): The workflow runs when code is pushed or a pull request is created on the main branch.
  • Jobs:
    • build-test job:
    • Checks out the repository.
    • Sets up Node.js.
    • Installs dependencies.
    • Runs tests.
    • deploy job:
    • Runs only if build-test succeeds.
    • Deploys the application (replace this with actual deployment steps).

Step 4: Push the Workflow to GitHub

After adding the workflow, commit and push the changes:

git add .github/workflows/ci-cd-pipeline.yml
git commit -m "Add GitHub Actions workflow"
git push origin main
Enter fullscreen mode Exit fullscreen mode

Step 5: Monitor Workflow Execution

Once you push the changes, GitHub Actions automatically triggers the pipeline.

  1. Go to Your GitHub Repository.
  2. Click on the "Actions" tab.
  3. You will see your workflow running.
  4. Click on the workflow run to see detailed logs.

Deploying the Application to a Cloud Platform

To deploy the application to Heroku, modify the deploy step:

- name: Deploy to Heroku
  env:
    HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
  run: |
    heroku git:remote -a your-heroku-app
    git push heroku main
Enter fullscreen mode Exit fullscreen mode

Setting Up GitHub Secrets

  1. Go to Your GitHub RepositorySettingsSecretsNew Repository Secret.
  2. Add a secret with the name HEROKU_API_KEY and paste your Heroku API key.
  3. Now, when the workflow runs, it deploys the application to Heroku.

Best Practices for CI/CD Pipelines with GitHub Actions

🔹 Automate Testing – Ensure unit, integration, and end-to-end tests are automated.

🔹 Use Caching – Speed up workflows by caching dependencies using the actions/cache action.

🔹 Secure Secrets – Use GitHub Secrets for API keys and credentials.

🔹 Fail Fast – Detect failures early by running tests at the beginning of the pipeline.

🔹 Keep Workflows Simple – Avoid overly complex pipelines. Start small and expand as needed.

Conclusion

GitHub Actions is a powerful, flexible, and easy-to-use CI/CD tool for automating software development workflows. In this guide, we covered:
✔ Setting up a GitHub repository for CI/CD.

✔ Writing a GitHub Actions workflow for building, testing, and deploying a Node.js application.

✔ Deploying applications to Heroku using GitHub Secrets.

✔ Best practices for maintaining efficient CI/CD pipelines.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more