Setting up a smooth Continuous Integration and Continuous Deployment (CI/CD) pipeline is a game-changer for development teams. It ensures that code changes are automatically built, tested, and deployed, saving time and reducing errors. In this guide, we’ll walk you through creating a simple CI/CD pipeline using GitHub Actions and Docker. If you're using DevOps as a Service, this process becomes even easier with built-in tools and support.
What You’ll Need
A GitHub account
Docker installed on your machine
A simple application (like a Node.js or Python app)
Step 1: Write a Dockerfile
To get started, you’ll need a Dockerfile that defines how your application is packaged into a container. Here’s an example for a Node.js app:
# Use Node.js official image
FROM node:18
# Set working directory
WORKDIR /usr/src/app
# Install dependencies
COPY package*.json ./
RUN npm install
# Copy app files
COPY . .
# Expose port and run app
EXPOSE 3000
CMD [ "node", "app.js" ]
This Dockerfile builds your app using Node.js, installs dependencies, and runs it on port 3000.
Step 2: Push Your Code to GitHub
Create a repository on GitHub.
Clone it to your machine using git clone.
Add your code and Dockerfile to the repository.
Commit and push your changes using:
git add .
git commit -m "Initial commit"
git push origin main
Step 3: Set Up GitHub Actions
Next, create a GitHub Actions workflow. This will automate building, testing, and pushing your Docker image.
Folder Structure
.github/
└── workflows/
└── ci-cd-pipeline.yml
Example Workflow (ci-cd-pipeline.yml)
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@v4
- name: Set Up Docker
run: docker --version
- name: Build Docker Image
run: docker build -t my-app:latest .
- name: Run Tests
run: docker run my-app:latest npm test
This setup builds a Docker image and runs any tests defined in your application.
Step 4: Push Your Docker Image to DockerHub
To publish your Docker image, add your DockerHub credentials as GitHub secrets (DOCKER_USERNAME and DOCKER_PASSWORD) and use this step:
- name: Login to DockerHub
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
- name: Tag and Push Image
run: |
docker tag my-app:latest your-dockerhub-username/my-app:latest
docker push your-dockerhub-username/my-app:latest
Step 5: Deploy Your Docker Container
Once your image is pushed to DockerHub, you can deploy it using Docker CLI:
docker run -d -p 3000:3000 your-dockerhub-username/my-app:latest
Your app should now be accessible at http://localhost:3000.
You’ve just built a functional CI/CD pipeline using GitHub Actions and Docker. This setup not only automates builds and tests but also makes deploying applications easier. For added improvements, consider integrating:
Notifications: Get alerts on build or test failures.
Multi-Stage Builds: Optimize your Docker images.
Kubernetes Deployments: Manage containers at scale.
Alternatively, if managing a pipeline seems overwhelming, consider using a CI/CD pipeline as a service. These solutions offer pre-configured pipelines, reducing the complexity of maintaining your own infrastructure.
Top comments (0)