How to Make CI/CD Pipeline for the Next.js Application (GitHub Actions & GitLab CI) How to Make CI/CD Pipeline for the Next.js Application (GitHub Actions & GitLab CI)

How to Make CI/CD Pipeline for the Next.js Application (GitHub Actions & GitLab CI)

Introduction

The modern software development process thrives on automation, and that’s exactly where CI/CD (Continuous Integration and Continuous Deployment) pipelines come in. Whether you’re a frontend developer, a DevOps professional, or just starting out, knowing how to make CI/CD pipeline for the Next.js application is an essential skill that can supercharge your workflow.

In this guide, we’ll walk you through setting up CI/CD for your Next.js app using both GitHub Actions and GitLab CI/CD. By the end, your application will automatically build, test, and deploy with every commit.


What is CI/CD and Why It Matters

CI/CD refers to:

  • Continuous Integration (CI): Automatically test and build your code every time you push changes to your repo.
  • Continuous Deployment/Delivery (CD): Automatically deploy code to production (or staging) after successful builds.

Benefits:

  • Faster release cycles
  • Improved code quality
  • Automated testing
  • Fewer manual errors

Next.js, with its hybrid static & server rendering, works well with both static deployments (like Vercel or Hostinger) and dynamic ones (like Node.js servers).


Prerequisites

Before we start, ensure you have the following:

  • A Next.js application ready in your repository.
  • A GitHub or GitLab account.
  • Hosting setup (e.g., Hostinger, Vercel, or your own server).
  • Basic knowledge of Git and terminal commands.

CI/CD Pipeline with GitHub Actions

πŸ“ Workflow File Structure

GitHub Actions workflows are written in YAML and saved under:

bashCopyEdit<code>.github/workflows/your-workflow.yml
</code>

βœ… Sample GitHub Actions Workflow

Here’s a basic setup that installs dependencies, runs tests, builds the app, and uploads the output (e.g., for static hosting):

yamlCopyEdit<code>name: Next.js CI/CD

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'

      - name: Install Dependencies
        run: npm install

      - name: Run Tests
        run: npm run test

      - name: Build App
        run: npm run build

      - name: Export Static Files
        run: npm run export

      - name: Upload Build Artifacts
        uses: actions/upload-artifact@v3
        with:
          name: nextjs-export
          path: out
</code>

βœ… Deployment Options with GitHub Actions


CI/CD Pipeline with GitLab CI/CD

GitLab CI/CD uses a .gitlab-ci.yml file in the root of your repository.

πŸ“ Sample .gitlab-ci.yml for Next.js

yamlCopyEdit<code>stages:
  - install
  - test
  - build
  - deploy

variables:
  NODE_ENV: 'production'

install_dependencies:
  stage: install
  image: node:18
  script:
    - npm install

run_tests:
  stage: test
  image: node:18
  script:
    - npm run test

build_app:
  stage: build
  image: node:18
  script:
    - npm run build
    - npm run export
  artifacts:
    paths:
      - out/

deploy:
  stage: deploy
  script:
    - echo "Deploy your files using scp or FTP here"
  only:
    - main
</code>

βœ… GitLab Runner Options

If you’re self-hosting:

  • Install GitLab Runner
  • Register it with your repo
  • Use SSH/FTP scripts to deploy your out folder

Deploying Your Next.js App

Once the build is complete, you can deploy using:

  • Vercel (static): Plug & play.
  • Hostinger (static): Upload out/ folder via FTP.
  • Custom VPS: Copy .next folder and run npm start after building.

If you’re hosting on vercel, read this guide:
πŸ‘‰ How to Deploy Next.js App on vercel


Best Practices

  1. Use Environment Variables – Don’t hardcode secrets.
  2. Cache dependencies to speed up builds.
  3. Run automated tests before deployment.
  4. Keep build time low by cleaning unused packages.
  5. Use branching strategies like main, dev, and feature/.

Troubleshooting Common Issues

IssueSolution
npm run export failsEnsure next.config.js has output: 'export'
GitHub build timeoutSplit workflow or increase build resources
Missing out folderConfirm that npm run export was run correctly
Environment variable not workingAdd it in GitHub/GitLab Secrets

Conclusion

Setting up a CI/CD pipeline for your Next.js application using GitHub Actions or GitLab CI may sound daunting at first, but with the right steps, it becomes an efficient and powerful tool in your development process.

Whether you’re deploying to Vercel, Hostinger, or your own VPS, automation is the future β€” and you’re now equipped to embrace it confidently.

Looking to explore more development tutorials?
Check out this helpful article:
πŸ‘‰ Best Projects for Web Development Students


Related Resources

Leave a Reply

Your email address will not be published. Required fields are marked *