Skip to main content

Mastering CI/CD with GitHub Actions and Docker

Published on
Published on
Blog Post views
... Views
Reading time
2 min read

Mastering CI/CD with GitHub Actions and Docker

In todayโ€™s fast-paced development world, automation is key.
Whether you're working solo or in a team, a strong CI/CD setup ensures reliability, efficiency, and peace of mind.
In this blog, Iโ€™ll walk you through how I implemented CI/CD in my own project using GitHub Actions and Dockerโ€”from pull request checks to Docker Hub deployments.


๐Ÿš€ What is CI/CD and Why It Matters

In software projects, CI/CD (Continuous Integration and Continuous Deployment) plays a crucial role in maintaining the quality and reliability of the codebase. Whether itโ€™s an open-source or private project, a robust CI/CD pipeline helps manage code changes, streamline development, and boost collaboration.

๐Ÿ”‘ Key Benefits

  1. Code Integration
    CI/CD pipelines automatically integrate code on every push, reducing conflicts and keeping branches aligned.

  2. Automated Testing
    Unit, integration, and E2E tests run automatically, helping you catch bugs early.

  3. Workflows
    Tools like GitHub Actions let you define automated workflows for builds, tests, deployments, and more.

  4. Code Quality Checks
    Linting, static analysis, and security scans help ensure high standards.

  5. Deployment Automation
    CD pipelines handle deployments reliably and consistently across environments.


โš™๏ธ Setting Up CI with GitHub Actions

Letโ€™s start by setting up a simple CI pipeline that ensures code builds successfully when a pull request is created.

๐Ÿ“ File Structure

Create a file at:

.github/workflows/build.yml


### ๐Ÿ› ๏ธ CI Workflow Code

```yaml
name: Build on PR

on:
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '20'
      - name: Install Dependencies
        run: npm install
      - name: Run Build
        run: npm run build

โœ… What It Does

  • Triggers on PRs to the main branch.
  • Uses an Ubuntu runner.
  • Checks out code, sets up Node.js v20.
  • Installs dependencies and runs the build command.

This ensures all PRs are validated for successful builds before merging.


๐Ÿณ Dockerizing the App for CD

Before we can deploy, letโ€™s containerize our app. Assume weโ€™re deploying the user-app from a monorepo.

๐Ÿ“„ Dockerfile (docker/Dockerfile.user)

FROM node:20.12.0-alpine3.19
 
WORKDIR /usr/src/app
 
COPY package.json package-lock.json turbo.json tsconfig.json ./
COPY apps ./apps
COPY packages ./packages
 
RUN npm install
RUN npm run generate-prisma
RUN npm run build --filter=user-app
 
CMD ["npm", "run", "start-user-app"]

๐Ÿ” Breakdown

  • Uses a lightweight Node.js Alpine image.
  • Sets up the working directory and copies configs and code.
  • Installs dependencies, generates Prisma client, builds the specific app.
  • Starts the user app.

๐Ÿšš Continuous Deployment to Docker Hub

Now, letโ€™s automate the deployment of this Docker image using GitHub Actions.

๐Ÿงช Prerequisites

  1. Create Docker Hub account

  2. Make a new repo on Docker Hub

  3. Add GitHub secrets:

    • DOCKER_USERNAME
    • DOCKER_PASSWORD (use access token)

๐Ÿ“ File Structure

Create:

.github/workflows/deploy.yml

๐Ÿงฉ Deployment Workflow Code

name: Build and Deploy to Docker Hub
 
on:
  push:
    branches:
      - main
 
jobs:
  build-and-push:
    runs-on: ubuntu-latest
    steps:
      - name: Check Out Repo
        uses: actions/checkout@v2
 
      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
 
      - name: Build and Push Docker image
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./docker/Dockerfile.user
          push: true
          tags: your-dockerhub-username/your-repo-name:latest
 
      - name: Verify Pushed Image
        run: docker pull your-dockerhub-username/your-repo-name:latest

๐Ÿ“Œ Replace

Replace:

  • your-dockerhub-username
  • your-repo-name

with your actual Docker Hub credentials.


๐Ÿง  Summary

Weโ€™ve covered:

  • Setting up a CI workflow to validate PR builds.
  • Dockerizing a monorepo-based app.
  • Automating Docker image builds and pushing to Docker Hub.

CI/CD pipelines like this one keep your development process efficient, collaborative, and production-ready.


๐Ÿ“‚ Source Code

You can explore the full implementation in my repository here: ๐Ÿ‘‰ GitHub Repo