|Blogs| Automating Next.js Application Deployment with CI/CD Using GitHub Actions
ringring
DevOps
Automating Next.js Application Deployment with CI/CD Using GitHub Actions
Published by:Anuj Poudel
Published on:19 Feb, 2024
blogimage
Share

In our previous tutorials, we explored Dockerizing a Next.js application and deploying it to a remote VPS using docker-compose. While effective, this method required manual steps like building and pushing Docker images and managing deployments on the server. To streamline this process, we'll dive into the concept of Continuous Integration and Continuous Deployment (CI/CD) and automate it using GitHub Actions.

 

Understanding CI/CD

CI/CD stands for Continuous Integration and Continuous Deployment. It's a practice where developers frequently integrate code changes into a shared repository (Continuous Integration) and automatically deploy those changes to production environments (Continuous Deployment) through automated pipelines.

 

Setting Up CI/CD with GitHub Actions

 

Step 1: Prepare Remote Server

Log in to the remote server and create a directory in the home folder, e.g., next-js-cicd

mkdir next-js-cicd

Navigate into the directory and create a docker-compose.yml file with the necessary configurations. The detail of this configuration is mentioned in this tutorial.

version: '3.8'

services:
  app:
    image: dallotech/nextjs-blog:latest
    platform: linux/amd64
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    restart: always

 

Step 2: Create GitHub Repository

Create a repository for your Next.js application on GitHub.

 

Step 3: Configure Repository Secrets

Navigate to Settings -> Secrets and Variables -> Actions in your GitHub repository. Create the following secrets:

  • DOCKER_HUB_TOKEN: Token for pushing Docker images to Docker Hub. Method of generation of this token is mentioned in this tutorial
  • DOCKER_HUB_USERNAME: Docker Hub username.
  • REMOTE_SSH_HOST: Host IP address of the remote server.
  • REMOTE_SSH_PW: Password for SSH login.
  • REMOTE_SSH_USER: Username for SSH login.
  • REMOTE_WORKING_DIR: Path of the directory on the remote server.
*Note: This tutorial is based on SSH password-based VPS login, for PEM files or SSH-based login like AWS ec2 will be covered in future blogs. 

After completion, it will look like this

 

Step 4: Define GitHub Action Workflow

In your project's root directory, create a deploy-docker.yml file inside .github/workflows/ the directory. The folder structure will look like

 Add the workflow configuration in the docker-deploy.yml file. This file is the command for github actions. 

name: vps-deploy

on:
  push:
    branches: [ "master" ]

env:
  REGISTRY: dockerhub.io
  IMAGE_NAME: dallotech/nextjs-blog:latest

jobs:
    publish:
        name: publish docker image
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3
        - name: Login to docker
          run: |
            echo ${{ secrets.DOCKER_HUB_TOKEN }} | docker login -u ${{secrets.DOCKER_HUB_USERNAME}} --password-stdin
        - name: Build and Publish in docker hub
          run: |
            docker-compose build
            docker-compose push


    deploy:
        needs: publish
        name: deploy image in remote vps
        runs-on: ubuntu-latest

        steps:
        - name: Login to vps and deploy
          uses: appleboy/ssh-action@v0.1.3
          with:
            host: ${{secrets.REMOTE_SSH_HOST}}
            username: ${{secrets.REMOTE_SSH_USER}}
            password: ${{ secrets.REMOTE_SSH_PW }}
            port: 22
            script: |
              cd ${{ secrets.REMOTE_WORK_DIR }}
              docker-compose pull
              docker-compose up -d

 

Let's go through each line of the docker-deploy.yml file

name: Publish and Deploy
  • This line sets the name of the workflow. It will appear in the GitHub Actions dashboard.
on:
  push:
    branches: [ "master" ]
  • This section defines the event trigger for the workflow. In this case, the workflow will be triggered whenever there is a push event to the master branch.

 

env:
  REGISTRY: dockerhub.io
  IMAGE_NAME: dallotech/nextjs-blog:latest
  • Here, environment variables are defined. These variables will be used later in the workflow steps to reference the Docker registry and image name.

 

jobs:
  publish:
    name: Publish Image
    runs-on: ubuntu-latest
  • This section defines a job named "Publish Image" that will run on an Ubuntu latest virtual machine. Jobs are the individual tasks that make up a workflow.

 

    steps:
      - uses: actions/checkout@v3
  • This step uses a pre-built GitHub Action (actions/checkout) to check out the repository code on the runner machine.

 

      - name: Login to Docker Hub
        run: echo ${{ secrets.DOCKER_HUB_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
  • This step logs in to Docker Hub using the provided credentials stored in GitHub secrets. The DOCKER_HUB_TOKEN is piped into docker login command to authenticate.

 

      - name: Build and Push Docker Image
        run: |
          docker-compose build
          docker-compose push
  • This step builds the Docker image for the Next.js application using docker-compose build and pushes it to the Docker registry using docker-compose push.

 

  deploy:
    name: Deploy Image
    needs: publish
    runs-on: ubuntu-latest
  • Similar to the previous job, this section defines another job named "Deploy Image" that will run on an Ubuntu latest virtual machine. It specifies a dependency on the "publish" job, meaning it will only run after the "publish" job is completed.

 

    steps:
      - name: Connect and Pull
        uses: appleboy/ssh-action@v0.1.3
        with:
          host: ${{ secrets.REMOTE_SSH_HOST }}
          username: ${{ secrets.REMOTE_SSH_USER }}
          password: ${{ secrets.REMOTE_SSH_PW }}
          port: 22
          script: |
            cd ${{ secrets.REMOTE_WORKING_DIR }}
            docker-compose pull
            docker-compose up -d
  • This step uses the appleboy/ssh-action GitHub Action to connect to the remote server via SSH and execute commands. It pulls the latest Docker image from the registry onto the server and then restarts the application using docker-compose up -d.

 

Step 5: Commit and Push Changes

Commit the changes to your project and push them to the master branch of your GitHub repository.

 

Step 6: Monitor Deployment

Navigate to your GitHub repository, then to the Actions tab to monitor the progress of the deployment.

Once the workflow execution is complete, your Next.js application will be deployed and accessible on port 3000 of the remote server.

By automating the CI/CD pipeline with GitHub Actions, you can streamline the deployment process, ensuring faster and more reliable deployments for your Next.js application. To learn more or get assistance, reach out to me at anuj@dallotech.com.

Other related blogs
Technology
Integrating Online Payment in NestJS using Factory Pattern: Project Setup, Service Layer and APIs (Part 1)

If you are integrating a payment features(eSewa, Khalti, ConnectIPS, Stripe, PayPal etc.) in NestJS or are interested in knowing how online payment features can be implemented, this blog is perfect for you.

Technology
Restore MSSQL Database file .bak on a Linux Server

If you need to restore a MSSQL database file (.bak) on a Linux server, this is the perfect blog for you. Additionally, anyone interested in web development, database management, or technology in general may also find this useful and interesting.

Technology
Hexagonal Architecture in NestJS with Sample Code

NestJS is javascript server side progressive framework combining Object Oriented Programming(OOP) and Functional Programming(FP). Since I got introduced to this framework, I have been loving it. Personal Opinion: At first, I became familiar with NestJS framework. After that, I wrote my first API , my first NestJS package and so on. But I was still exploring new things. This time, I wanted to learn about Hexagonal Architecture. Learning never stops !!! Rather than talking about theories, let’s directly dive into codes. I will be referencing code from the below mentioned repository. Github Link: Nestjs-Hexagonal-Architecture-Demo-Project

DevOps
How to Set Up CI/CD pipeline on Next.js project using GitLab on AWS EC2

Continuous Integration and Continuous Deployment (CI/CD) are essential for modern web development, ensuring fast, reliable, and automated software delivery. In this guide, we’ll walk you through setting up a CI/CD pipeline for a Next.js application using GitLab CI/CD. By automating the build and deployment process, you can eliminate manual tasks, reduce errors, and deploy updates seamlessly. Whether you're deploying to a VPS, cloud server, or a containerized environment, this tutorial will help you streamline the deployment of your Next.js app using Docker, GitLab CI/CD, and SSH. Let’s dive into automating your Next.js project for efficient, hassle-free deployment!

Discover Dallo Tech, a leading software development company offering expertise in Mobile and Web App Development, Data Analytics, Visualization, AI, and Blockchain solutions. Elevate your digital journey with our innovative technology services.

Open Hours

Sun - Fri:
10:00 am - 5:00 pm

Saturday:
CLOSED

Dallotech as tech partner
Business Team
Devops and Service
Design Team
Quality Assurance
Software Team
Mobile Team
Hire Expert Team
© 2018 - 2025 . DalloTech Pvt. Ltd. All Rights Reserved