Deploy a Next.js App on Vercel with Github Actions
Deploying a Next.js app on Vercel with GitHub Actions is a powerful way to streamline your development workflow. This guide will walk you through the process, ensuring your app is automatically deployed whenever changes are pushed to your GitHub repository.
Prerequisites
- A Next.js App
- A GitHub repository containing your Next.js App
- A Vercel account
Step 1: Set up your Next.js App on Github
Ensure your Next.js app is pushed to a GitHub repository. If you haven’t done this yet, follow these steps:
- Open a terminal or command prompt.
- Navigate to the directory where your Next.js app is located.
- Initialize a new Git repository:
Shell
git init - Add all files to the repository:
Shell
git add . - Commit your changes with a descriptive message:
Shell
git commit -m "Initial commit" - Create a GitHub repository and connect it to your local repository:
Shell
git remote add origin https://github.com/your-username/your-repo-name.git - Push your changes to GitHub:
Shell
git push -u origin main
Step 2: Connect your repository to Vercel
Next, we’ll link your GitHub repository to Vercel.
- Log in to your Vercel account.
- Click on the “New Project” button.
- Select your Github repository
- Configure your project settings (Vercel should automatically detect that it’s a Next.js app)
- Click on the “Deploy” button.
Step 3: Set up GitHub Actions
Now, let’s set up Github Actions to automate the deployment process.
- In your Github repository, go to the
Actionstab. - Click on the
New Workflow - Choose
Set up a workflow yourself - Name your workflow file (eg.
vercel-deploy.tml) - Replace the content with following YAML:
- Or create
.github/workflows/vercel-deploy.ymlfile in your project folder
name: 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 }}Step 4: Configure Vercel Token
To allow Github Actions to deploy to Vercel, you need to set up a Vercel token:
- Go to your Vercel account settings
- Navigate to
Tokens - Create a new token and copy it
- In your Github repository, go to
Settings>Secrets and variables>Actions - Create a new repository secret name
VERCEL_TOKENand paste your Vercel token as the value
{"orgId":"team_xyz12a12asd","projectId":"prj_xxxxxxxxxxxxxxx"}Step 5: Push Changes and Watch it Deploy
Now, whenever you push changes to the main branch of your GitHub repository, GitHub Actions will automatically trigger a deployment to Vercel.
By following these steps, you’ve successfully set up automated deployments for your Next.js app using Vercel and GitHub Actions. This workflow will save you time and ensure that your production environment is always up to date with your latest code changes. Remember to test your application thoroughly before pushing to the main branch, as changes will be automatically deployed to production.
Happy coding!