Improve Vercel deployments with auto commits

Mohd HaiderMohd Haider
4 min read

Have you ever faced issues with Vercel not automatically deploying changes when a team member pushes code to the main branch? If so, you’re not alone. Vercel, by default, only triggers deployments when the repository owner pushes changes. This can be frustrating for teams where multiple members contribute to the codebase.

In this blog post, I’ll show you how to hack Vercel’s deployment process by automating dummy commits using GitHub Actions. Whenever someone other than you pushes to the main branch, the workflow will create a dummy commit, triggering Vercel to deploy the changes automatically.

Why Automate Dummy Commits?

The Problem with Vercel

Vercel’s default behavior is to trigger deployments only when the repository owner pushes changes. If a team member pushes code, Vercel won’t automatically deploy it. This can slow down development and create unnecessary manual steps.

The Solution: Dummy Commits

By automating dummy commits, we can trick Vercel into thinking that the repository owner has made a change, thereby triggering an automatic deployment. Here’s how it works:

  1. A team member pushes code to the main branch.

  2. A GitHub Actions workflow detects the push and checks if the pusher is not the repository owner.

  3. If the pusher is not the owner, the workflow creates an empty commit (a commit with no file changes) using the owner’s account.

  4. Vercel detects the dummy commit and triggers a deployment.

This approach ensures that every push to the main branch, regardless of who made it, results in an automatic deployment.

Prerequisites

Before we begin, make sure you have:

  1. A GitHub repository.

  2. Write access to the repository.

  3. A basic understanding of Git and GitHub Actions.


Step 1: Set Up Repository Permissions

To ensure the GitHub Actions workflow has the necessary permissions to push changes to the repository, follow these steps:

  1. Go to your repository’s Settings.

  2. Navigate to Actions > General.

  3. Under Actions permissions, make sure Allow all actions and reusable workflows is selected.

  4. Under Workflow permissions, ensure Read and write permissions is selected.

This ensures that the workflow can push changes to the repository.


Step 2: Create the GitHub Actions Workflow

GitHub Actions uses YAML files to define workflows. Here’s how to create a workflow that automates dummy commits:

  1. In your repository, create a .github/workflows directory if it doesn’t already exist.

  2. Inside the workflows directory, create a new file called dummy-commit.yml.

  3. Add the following content to the file:

     name: Dummy Commit Workflow
    
     on:
       push:
         branches:
           - main
    
     jobs:
       dummy-commit:
         runs-on: ubuntu-latest
    
         steps:
           - name: Checkout repository
             uses: actions/checkout@v3
    
           - name: Check if pusher is not you
             id: check-pusher
             run: |
               if [ "${{ github.actor }}" != "YOUR_GITHUB_USERNAME" ]; then
                 echo "::set-output name=should_commit::true"
               else
                 echo "::set-output name=should_commit::false"
               fi
    
           - name: Create dummy commit
             if: steps.check-pusher.outputs.should_commit == 'true'
             run: |
               git config --global user.name "YOUR_GITHUB_USERNAME"
               git config --global user.email "YOUR_GITHUB_EMAIL"
               git commit --allow-empty -m "Automated dummy commit"
               git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git HEAD:main
    

Explanation of the Workflow:

  • Trigger: The workflow runs whenever there’s a push to the main branch.

  • Check Pusher: It checks if the pusher is not you (YOUR_GITHUB_USERNAME). If the pusher is someone else, it proceeds to create a dummy commit.

  • Create Dummy Commit: It creates an empty commit using git commit --allow-empty and pushes it to the main branch.

Step 3: Push the Workflow to Your Repository

  1. Commit and push the dummy-commit.yml file to your repository.

  2. The workflow will now be active and will trigger whenever someone pushes to the main branch.


Step 4: Test the Workflow

To test the workflow:

  1. Push a change to the main branch from an account other than yours.

  2. Check the repository’s commit history to verify that an empty commit was created.

  3. Verify that Vercel triggers a deployment for the dummy commit.


Why Use Empty Commits?

Using empty commits is a clean and efficient way to achieve your goal because:

  • It doesn’t modify any files in the repository.

  • It keeps the commit history clean and easy to understand.

  • It avoids unnecessary changes to your codebase.


Conclusion

By automating dummy commits with GitHub Actions, you can hack Vercel’s deployment process and ensure that every push to the main branch triggers an automatic deployment, regardless of who made the change. This approach is a clever workaround for Vercel’s limitation and can save your team time and effort.

If you have any questions or run into issues, feel free to leave a comment below. Happy coding!

0
Subscribe to my newsletter

Read articles from Mohd Haider directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohd Haider
Mohd Haider

Full-Stack developer with a passion for problem-solving and process optimization. Currently exploring new tools and technologies to enhance software solutions. Always eager to share knowledge and contribute to meaningful projects.