setting-up-cicd-pipelines-with-github-actions-and-docker-for-nodejs-apps.html

Setting Up CI/CD Pipelines with GitHub Actions and Docker for Node.js Apps

In today's fast-paced development landscape, Continuous Integration (CI) and Continuous Deployment (CD) are essential for delivering high-quality software. Combining GitHub Actions with Docker can streamline your workflow, especially for Node.js applications. This article will guide you through setting up a CI/CD pipeline, complete with code snippets and step-by-step instructions.

What is CI/CD?

Continuous Integration (CI) is the practice of automatically testing and merging code changes into a shared repository. It allows developers to detect errors quickly, ensuring that the codebase remains healthy.

Continuous Deployment (CD) is the process of automatically deploying the application to production after passing the CI pipeline. This means that every change that passes the tests is immediately available to users.

Why Use GitHub Actions and Docker?

  • GitHub Actions: A powerful automation tool that allows you to create workflows directly in your GitHub repository. It simplifies the process of building, testing, and deploying applications.

  • Docker: A platform that enables developers to create, deploy, and run applications in containers. This ensures consistency across different environments.

Using these tools together can enhance your workflow, reduce deployment errors, and speed up the development process.

Prerequisites

Before diving into the setup, ensure you have:

  • A Node.js application ready to be deployed.
  • A GitHub account.
  • Docker installed on your machine.

Step 1: Create a Dockerfile

To containerize your Node.js application, start by creating a Dockerfile. This file contains instructions on how to build your Docker image.

# Use the official Node.js image.
FROM node:14

# Set the working directory.
WORKDIR /usr/src/app

# Copy package.json and package-lock.json.
COPY package*.json ./

# Install dependencies.
RUN npm install

# Copy the application code.
COPY . .

# Expose the port the app runs on.
EXPOSE 3000

# Command to run your app.
CMD ["node", "app.js"]

Explanation of the Dockerfile

  • FROM: Specifies the base image to use (Node.js 14 in this case).
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files from your local machine into the Docker image.
  • RUN: Executes commands to install dependencies.
  • EXPOSE: Documents which port the container listens on.
  • CMD: Specifies the command to run your Node.js app.

Step 2: Build and Run Your Docker Container Locally

You can build and test your Docker container locally before integrating it with GitHub Actions.

  1. Open your terminal and navigate to your project directory.
  2. Build the Docker image:

bash docker build -t my-node-app .

  1. Run the container:

bash docker run -p 3000:3000 my-node-app

Visit http://localhost:3000 to ensure your application is running as expected.

Step 3: Set Up GitHub Actions

Now, let’s create a CI/CD pipeline using GitHub Actions. In your GitHub repository, create a directory called .github/workflows and add a YAML file, for example, ci-cd.yml.

name: CI/CD Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Build Docker image
        run: |
          docker build -t my-node-app .

      - name: Run tests
        run: |
          docker run my-node-app npm test

      - name: Push to Docker Hub
        run: |
          echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
          docker tag my-node-app myusername/my-node-app:latest
          docker push myusername/my-node-app:latest

Breakdown of the GitHub Actions Workflow

  • name: The name of your workflow.
  • on: Specifies the event that triggers the workflow (in this case, pushes to the main branch).
  • jobs: Defines a set of steps to execute.
  • steps: A sequence of actions:
  • Checkout code: Retrieves your repository code.
  • Set up Docker Buildx: Prepares Docker for building images.
  • Build Docker image: Builds your Docker image.
  • Run tests: Runs unit tests inside the container.
  • Push to Docker Hub: Authenticates and pushes the Docker image to your Docker Hub repository.

Adding Secrets

To securely store your Docker Hub credentials, go to your GitHub repository settings, navigate to Secrets, and add DOCKER_USERNAME and DOCKER_PASSWORD.

Step 4: Test the CI/CD Pipeline

Commit your changes and push them to the main branch. GitHub Actions will automatically trigger the workflow. You can monitor the process in the "Actions" tab of your repository.

Conclusion

By setting up a CI/CD pipeline with GitHub Actions and Docker, you can enhance your Node.js application's deployment process. This automation not only saves time but also promotes code quality and reliability.

Key Takeaways

  • CI/CD helps maintain a healthy codebase and ensures seamless deployments.
  • Docker provides a consistent environment for running applications.
  • GitHub Actions allows for easy automation of workflows.

Embrace these tools to streamline your development process and deliver excellent software efficiently. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.