logo
CI/CD Pipeline for MERN with GitHub Actions and Docker (2025 Guide)
Kashan Iqbal

Kashan Iqbal

Posted 6 days ago

CI/CD Pipeline for MERN Using GitHub Actions + Docker (2025 DevOps Guide)

In 2025, if you're building full-stack MERN apps, you're expected to go beyond just npm start. Teams and clients want automation, reliability, and fast delivery.

That’s where CI/CD pipelines come in — and GitHub Actions + Docker is a powerful combo.


⚙️ What Is CI/CD?

  • CI (Continuous Integration): Automatically test & build your code when changes are pushed.
  • CD (Continuous Deployment): Automatically deploy your app after a successful build.

Together, CI/CD brings:

  • 🧪 Fewer bugs
  • 🚀 Faster releases
  • 🔄 Automated workflow

🚧 Our Stack for This Pipeline

  • GitHub Actions: Automation engine triggered by repo events
  • Docker: Containerized deployment of MERN app
  • MERN App: Node.js backend, React frontend
  • Render or Vercel: For final hosting (optional)

🗂️ Project Structure Overview

mern-ci-cd/
├── client/            # React app
├── server/            # Node.js API
├── .github/
│   └── workflows/
│       └── ci-cd.yml  # GitHub Actions config
├── Dockerfile
├── docker-compose.yml
└── README.md

🐳 Dockerfile (for Node + React)

# Build React FROM node:18 as build-frontend WORKDIR /app/client COPY client/ . RUN npm install && npm run build # Build backend FROM node:18 WORKDIR /app COPY server/ ./server COPY --from=build-frontend /app/client/build ./client/build COPY package*.json ./ RUN npm install EXPOSE 5000 CMD ["node", "server/index.js"]

🐳 docker-compose.yml

version: "3" services: app: build: . ports: - "5000:5000" environment: - NODE_ENV=production - MONGO_URI=${MONGO_URI}

🤖 GitHub Actions Workflow (.github/workflows/ci-cd.yml)

name: MERN CI/CD Pipeline on: push: branches: - main jobs: build-deploy: 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: Build client run: | cd client npm install npm run build - name: Build Docker Image run: docker build -t mern-app . - name: Run Tests (optional) run: echo "Add test runner here" - name: Deploy (e.g. to Render) run: echo "Add deploy script or use Render GitHub integration"

raw


🔐 Secrets You’ll Need (GitHub Repo Settings)

  • MONGO_URI
  • DOCKER_USERNAME, DOCKER_PASSWORD (optional for Docker Hub)
  • RENDER_API_KEY or VERCEL_TOKEN (if using deployment automation)

✅ Best Practices

  • Use different environments for dev/staging/prod
  • Run lint/tests during CI phase
  • Tag Docker images using commit SHA or version

🧠 Final Thoughts

With this CI/CD pipeline, every push to main:

  1. Installs dependencies
  2. Builds frontend + backend
  3. Builds Docker image
  4. Optionally deploys to production

Say goodbye to FTP uploads and manual builds — automate everything like a modern dev!


Published on: 2025-06-18

Related Articles

No related blogs found.