September 8, 2024

Md. Saad

GitHub Actions is a platform that automates software development workflows, including builds, tests, and deployments.
To deploy a Next.js application using GitHub Actions, you'll need to set up a workflow that automates the build and deployment process. Here’s a step-by-step guide. Let’s learn How to Deploy Next.js application with GitHub Action.
Before setting up the GitHub Actions workflow, the first and foremost step is to create a Next.js App. Use the following command to create a next.js app.
npx create-next-app@latest app-name
As it is not an article on creating the next.js app, we will not discuss it in detail. If you want to read it in detail follow the link.
git initgit add --all
git commit -m "initial commit"git remote add origin https://git.abc.com/your-app.gitgit push origin main mkdir -p .github/workflowstouch .github/workflows/preview.ymlname: Vercel Preview Deployment
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
on:
push:
branches-ignore:
- main
jobs:
Deploy-Preview:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Vercel CLI
run: npm install --global vercel@latest
- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
- name: Build Project Artifacts
run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
- name: Deploy Project Artifacts to Vercel
run: vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }} touch .github/workflows/production.ymlname: Vercel Production Deployment
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
on:
push:
branches:
- main
jobs:
Deploy-Production:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Vercel CLI
run: npm install --global vercel@latest
- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
- name: Build Project Artifacts
run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}
- name: Deploy Project Artifacts to Vercel
run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }}npm i -g vercelvercel loginvercel linkVERCEL_TOKEN: Your Vercel deployment token.
VERCEL_ORG_ID: Your Vercel organization ID.
VERCEL_PROJECT_ID: Your Vercel project ID.
Once you've set up the workflow file and added your secrets, you can try out the workflow. All you need to do is to create a new pull request to your GitHub repository. GitHub Actions will identify the update and develop your application using the Vercel CLI. T hus it will make a Preview Deployment. A Production build will also be created and deployed once the pull request is merged. A Preview Deployment will be included in every pull request by default. Finally, Vercel will initiate a fresh Production build to return to the previous git state if the pull request needs to be rolled back. To do this, simply revert and merge the PR.
Surely you are well known on How to Deploy Next.js applications with GitHub Action. In conclusion, deploying a Next.js application using GitHub Actions is an efficient and automated way to manage your deployment process. By setting up a workflow that includes code checkout, environment setup, and application build, followed by deployment to Vercel, you can streamline the process of pushing updates to production. The integration of secrets within GitHub ensures that sensitive information like tokens and IDs are securely managed. Once configured, this setup allows you to automatically deploy your application with every push to your main branch, making continuous integration and delivery seamless.
I hope you gathered some insightful information from this article. Feel free to get in touch with StaticMania if you have any queries or comments. Enjoy your coding!