Next.js Deployment with GitHub Actions: A Step-by-Step Guide

Next JS Deployment with GitHub Actions
In the realm of modern web development, efficiency and automation are paramount. Next.js, a powerful React framework, empowers developers to build server-rendered applications with ease. When it comes to deployment, GitHub Actions emerges as a potent tool, seamlessly integrating CI/CD workflows into your development pipeline.

Let’s delve into the process of deploying a Next.js application using GitHub Actions, complete with code examples.
Next JS Deployment with GitHub Actions
1. Setting up your Next.js project

Begin by initializing a Next.js project if you haven't already:
npx create-next-app@latest my-next-app
cd my-next-app  
  
2. Creating a GitHub repository

Navigate to GitHub and create a new repository for your Next.js project.

3. Configuring GitHub Actions

In your project directory, create a new directory named `.github/workflows`. Inside this directory, create a YAML file (e.g., `deploy.yml`) to define your GitHub Actions workflow:
name: Deploy Next.js App

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Install dependencies
        run: npm install

      - name: Build
        run: npm run build

      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@releases/v3
        with:
          ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
          BRANCH: gh-pages
          FOLDER: out  
  
This workflow triggers on every push to the main branch, installing dependencies, building the Next.js app, and deploying it to GitHub Pages using the `github-pages-deploy-action`.

4. Generating a GitHub Personal Access Token

To allow GitHub Actions to deploy to GitHub Pages, you need to create a personal access token:
  • Go to your GitHub Settings.
  • Navigate to Developer settings > Personal access tokens.
  • Click on Generate new token.
  • Select the `repo` scope.
  • Copy the generated token.
5. Adding the Access Token to your repository secrets

In your GitHub repository, go to Settings > Secrets, and click on New repository secret. Add a secret named `ACCESS_TOKEN` and paste the token you generated earlier.

6. Committing and pushing changes

Finally, commit your changes and push them to your GitHub repository:
git add .
git commit -m "Add GitHub Actions workflow for deployment"
git push origin main  
  
7. Accessing your deployed Next.js app

After pushing your changes, GitHub Actions will automatically trigger the workflow, building and deploying your Next.js app. You can access your deployed application at `https://<username>.github.io/<repository-name>`.

With GitHub Actions automating the deployment process, you can focus on building remarkable Next.js applications without worrying about manual deployment hassles. Streamline your development workflow today and unleash the full potential of Next.js with GitHub Actions. Happy coding!

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.