Setting Up CI/CD Pipelines with GitHub Actions and Docker for Node.js Apps
In today's fast-paced software development landscape, Continuous Integration and Continuous Deployment (CI/CD) have become essential practices. They automate the process of integrating code changes, running tests, and deploying applications, which ultimately leads to faster, more reliable releases. In this article, we will guide you through setting up CI/CD pipelines using GitHub Actions and Docker specifically for Node.js applications.
What is CI/CD?
CI/CD is a methodology that allows developers to frequently deliver code changes by automating the integration and deployment processes.
- Continuous Integration (CI): This involves automatically testing code changes to ensure they integrate well with the existing codebase.
- Continuous Deployment (CD): This automates the deployment of applications to production after passing tests, making updates swift and efficient.
Why Use GitHub Actions and Docker?
GitHub Actions
GitHub Actions is a powerful CI/CD tool integrated within GitHub, allowing you to automate workflows directly from your repository. Its key features include:
- Event-driven: Automatically triggers workflows based on events (e.g., push, pull request).
- Customizable: Supports a wide range of actions and can be tailored to fit any workflow.
Docker
Docker is a containerization platform that allows you to package applications and their dependencies in containers. This ensures consistency across development, testing, and production environments. Key benefits include:
- Portability: Run containers anywhere Docker is installed.
- Isolation: Each application runs in its own environment, reducing conflicts.
Use Case: Node.js Application
In this guide, we will set up a CI/CD pipeline for a simple Node.js application that uses Express.js. The pipeline will build a Docker image, run tests, and deploy the application to a cloud service.
Prerequisites
- Basic knowledge of Node.js and Express.js.
- A GitHub account.
- Docker installed on your local machine.
Step 1: Create a Sample Node.js Application
First, create a new Node.js application. Open your terminal and run the following commands:
mkdir my-node-app
cd my-node-app
npm init -y
npm install express --save
Next, create an index.js
file:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Step 2: Create a Dockerfile
To containerize your application, you need a Dockerfile
. Create a new file in the root of your project called Dockerfile
:
# Use the official Node.js image
FROM node:14
# Set the working directory
WORKDIR /usr/src/app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of your application code
COPY . .
# Expose the application port
EXPOSE 3000
# Start the application
CMD ["node", "index.js"]
Step 3: Set Up GitHub Actions
Now, let’s create a GitHub Actions workflow. Create a directory named .github/workflows
in your project root, and inside that, create a file named 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 Docker image 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
Step 4: Add Secrets for Docker Hub
To push your Docker image to Docker Hub, you must add your Docker credentials as secrets in your GitHub repository.
- Go to your GitHub repository.
- Click on Settings > Secrets and variables > Actions.
- Add the following secrets:
DOCKER_USERNAME
: Your Docker Hub username.DOCKER_PASSWORD
: Your Docker Hub password.
Step 5: Test Your CI/CD Pipeline
Now that everything is set up, commit your changes and push to the main
branch:
git add .
git commit -m "Set up CI/CD pipeline with GitHub Actions and Docker"
git push origin main
Visit the Actions tab in your GitHub repository to monitor the workflow. If everything is set up correctly, you should see the workflow executing and your Docker image being built and pushed to Docker Hub.
Conclusion
You have successfully set up a CI/CD pipeline for your Node.js application using GitHub Actions and Docker. This setup automates the process of building, testing, and deploying your app, leading to faster releases and higher quality software.
By embracing CI/CD practices, you not only streamline your development workflow but also enhance collaboration among team members. As your application grows, consider expanding your pipeline with additional testing stages, security checks, or deployment to cloud platforms like AWS or Heroku.
Happy coding!