Logo

dev-resources.site

for different kinds of informations.

Automate Vercel Preview Deployments with GitHub actions: A Step-by-Step Guide

Published at
10/28/2024
Categories
ci
githubactions
github
automation
Author
dantonelli
Author
10 person written this
dantonelli
open
Automate Vercel Preview Deployments with GitHub actions: A Step-by-Step Guide

Introduction

Vercel Preview Deployments allow you to stage changes in a live environment before merging them to a production branch. This tutorial will guide you through configuring GitHub Actions to deploy pull requests (PRs) on Vercel automatically. Once deployed successfully, the Vercel bot will post the preview URL as a comment in the PR.

Workflow diagram

Vercel Bot comment

Prerequisites

  1. Basic familiarity with Continuous Integration using GitHub Actions

Step-by-Step Guide

1. Create the GitHub Action Workflow

Letโ€™s start by creating workflow file: .github/workflows/preview.yaml

name: GitHub Actions Vercel Preview Deployment
env:
  VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
  VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}

on:
  pull_request:

jobs:
  Deploy-Preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: amondnet/[email protected]
        with:
         vercel-token: ${{ secrets.VERCEL_TOKEN }}
         vercel-org-id: ${{ secrets.VERCEL_ORG_ID}}
         vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID}}
         github-comment: 'true'
         github-token: ${{ secrets.GITHUB_TOKEN }}
         scope: ${{ secrets.VERCEL_ORG_ID}}
Enter fullscreen mode Exit fullscreen mode

2. Set Up Vercel CLI and Login

If you donโ€™t have a Vercel account yet, you can create new one here.

npm i -g vercel
vercel login
Enter fullscreen mode Exit fullscreen mode

3. Create new Vercel project

In your project directory, run vercel link to create a new project on Vercel. Answer the prompts based on your settings. If you're linking the project for the first time, your answer may be no to Link to existing project prompt:

? Set up โ€œ~/repos/demo-vercel-ciโ€? yes
? Which scope should contain your project? my projects
? Link to existing project? no
? Whatโ€™s your projectโ€™s name? demo-vercel-ci
? In which directory is your code located? ./
Enter fullscreen mode Exit fullscreen mode

After linking, a .vercel folder will be automatically added to your project's root directory and included in your .gitignore file.

project-root/
โ”œโ”€โ”€ .github/
โ”‚   โ””โ”€โ”€ workflows/
โ”‚       โ””โ”€โ”€ preview.yaml
โ”œโ”€โ”€ .vercel/
โ”‚   โ”œโ”€โ”€ project.json
โ”‚   โ””โ”€โ”€ README.txt
โ”œโ”€โ”€ .gitignore
Enter fullscreen mode Exit fullscreen mode

3. Configure GitHub Secrets

Next, we will configure the secrets required for the workflow. Inside the .vercel folder save the save theย projectId andย orgId from theย project.json.

VERCEL_TOKEN=
VERCEL_ORG_ID=
VERCEL_PROJECT_ID=
Enter fullscreen mode Exit fullscreen mode

To generate a Vercel token, navigate to the Account Settings page under your personal account dropdown on Vercel.

Vercel account settings screenshot

Create a new token, and save it as shown only once.

Generate Vercel token screenshot

Letโ€™s add repository secrets to GitHub. Go to Settings > Secrets and Variables > Actions > Repository Secrets and add secrets as shown below:

Github repo secrets screenshot

4. Set Workflow Permissions

To allow the Vercel bot to comment on PRs, go to https://github.com/OWNER/REPO/settings/actions. Under Workflow Permissions, select Read and Write permissions.

GitHub Set Workflow Permissions screenshot

5. Push your changes

Commit and push your changes to open a pull request. GitHub Actions will then automatically deploy the preview to Vercel.

Source code

You can download the source code here

Resources

ci Article's
30 articles in total
Favicon
CI/CD Tools for Startups: Empowering IT Professionals to Scale Smarter
Favicon
CI/CD pipeline
Favicon
Top 10 Best CI/CD Tools For DevOps and Programmers
Favicon
Tester c'est tricher, compiler c'est douter
Favicon
Docker in DevOps Workflows: Enhancing CI/CD Pipelines
Favicon
Streamlining CI/CD Pipelines with Docker: A Complete Guide
Favicon
Docker with CI/CD: Automating the Software Lifecycle
Favicon
What is Continuous Integration? - A Comprehensive Guide
Favicon
Setting up Continuous Integration (CI) for dev-mate-cli
Favicon
Implementing a CI Pipeline for VShell with GitHub Actions
Favicon
Building CI/CD Pipelines with Jenkins and GitLab - Part1
Favicon
Top 5 Continuous Integration Tools: Optimizing Your Development Workflow
Favicon
How We Started Continuous OSS License Monitoring with License Finder
Favicon
Mastering CI/CD Pipelines: Automate Testing, Deployment, and Delivery Like a Pro
Favicon
Building a CI Tool for GitHub Runners
Favicon
GitLab CI: Needs vs Dependencies โ€” A Practical Guide
Favicon
Adding CI Workflows with GitHub Actions
Favicon
Managing Software Project Complexity with Development Containers and Continuous Integration
Favicon
GitHub Workflows: The First Line of Defense
Favicon
Automate Vercel Preview Deployments with GitHub actions: A Step-by-Step Guide
Favicon
github action services: mysql, redis and elasticsearch
Favicon
The Must-Have GitHub Actions Dashboard - CICube
Favicon
Effective CI: A Comprehensive Guide
Favicon
Day007 - Random posts under TIL
Favicon
Django project - Part 3 Continuous Integration
Favicon
Fast multi-arch Docker build for Rust projects
Favicon
Eslint Code Insights from Bitbucket pipelines
Favicon
Unlock Your Developer Potential: How to Apply Atomic Habits to Your Software Development Journey
Favicon
How to Copy Files from Pod to Local with kubectl cp
Favicon
DevOps Unleashed Navigating the Future of Continuous Integration and Delivery

Featured ones: