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
- Vercel: Connect your repo directly to Vercel.
- Hostinger: Use FTP upload in the next step (check out our Hostinger deployment guide).
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 runnpm start
after building.
If you’re hosting on vercel, read this guide:
π How to Deploy Next.js App on vercel
Best Practices
- Use Environment Variables β Donβt hardcode secrets.
- Cache dependencies to speed up builds.
- Run automated tests before deployment.
- Keep build time low by cleaning unused packages.
- Use branching strategies like
main
,dev
, andfeature/
.
Troubleshooting Common Issues
Issue | Solution |
---|---|
npm run export fails | Ensure next.config.js has output: 'export' |
GitHub build timeout | Split workflow or increase build resources |
Missing out folder | Confirm that npm run export was run correctly |
Environment variable not working | Add 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