Continuous Integration and Continuous Deployment (CI/CD) are essential practices for modern software development. Automating these processes for your NestJS applications can significantly improve deployment speed and reliability. This guide provides a step-by-step approach to setting up CI/CD for NestJS projects using GitHub Actions.

Prerequisites

  • A GitHub account with access to your repository
  • Basic knowledge of NestJS framework
  • Docker installed locally (optional but recommended)
  • Understanding of GitHub Actions workflows

Step 1: Prepare Your NestJS Application

Ensure your NestJS project is ready for deployment. Confirm that your application can be built and run locally without issues. Create a Dockerfile at the root of your project to containerize your app.

Sample Dockerfile:

FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/main"]

Step 2: Create a GitHub Actions Workflow

In your repository, create a directory named .github/workflows if it doesn't exist. Inside, add a new file called ci-cd.yml.

This file defines the automation process for your CI/CD pipeline.

name: NestJS CI/CD

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build_and_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: '14'

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

      - name: Build project
        run: npm run build

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build Docker image
        run: docker build -t yourusername/nestjs-app:${{ github.sha }} .

      - name: Push Docker image
        run: docker push yourusername/nestjs-app:${{ github.sha }}

      - name: Deploy to server
        run: |
          ssh user@yourserver "docker pull yourusername/nestjs-app:${{ github.sha }} && docker run -d -p 80:3000 yourusername/nestjs-app:${{ github.sha }}"
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}

Step 3: Configure Secrets in GitHub

Navigate to your GitHub repository settings, then to the Secrets and variables > Actions section. Add the following secrets:

  • DOCKER_USERNAME: Your Docker Hub username
  • DOCKER_PASSWORD: Your Docker Hub password
  • SSH_PRIVATE_KEY: Your private SSH key for server access

Step 4: Push Changes and Verify

Push your code to the main branch. GitHub Actions will automatically trigger the workflow. Monitor the Actions tab for progress and logs.

Conclusion

By following these steps, you automate the build, test, containerization, and deployment of your NestJS application. This setup ensures faster, more reliable releases, enabling continuous delivery with minimal manual intervention.