mcclosa
mcclosa

Reputation: 1455

Prevent Vercel Deployment on branch push

I have my GitHub Repository linked to my vercel project. Required for deploying changes on a subdomain that's linked to a branch on my git repository. dev.<website>.<tld> is linked to the dev branch. <website>.<tld> is linked to the main branch. Couldn't figure out another way to do this, other than linking to my git repo.

I have a GitHub action that runs tests amongst other things that I want to ensure pass before deployment. But every time I push changes to my production branch it kicks of a vercel deployment, that I want to avoid.

I know there is a Ignored Build Step section in the git settings on vercel, but I'm not sure what to add in the command input in this section?

I've added

[ exit 1 ]

But not sure if this is correct.

Here is my github actions workflow yml file.

name: Deploy Web Application

on:
  push:
    branches: ["master", "development"]
    paths:
      - "web/**"
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./web

    strategy:
      matrix:
        node-version: [16.13.0]

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Node Version ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "yarn"
          cache-dependency-path: "./web/yarn.lock"

      - name: Install Dependencies
        run: yarn

      - name: Build Project
        run: yarn build

      - name: Run Linting
        run: yarn lint

      - name: Run Typecheck
        run: yarn typecheck

      - name: Start Deployment
        uses: bobheadxi/[email protected]
        id: deployment
        with:
          step: start
          token: ${{ secrets.GH_TOKEN }}
          env: prod

      - name: Deploy to Vercel
        uses: amondnet/vercel-action@v20
        id: vercel-action
        with:
          github-token: ${{ secrets.GH_TOKEN }}
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
          # if main branch go to --prod environment otherwise nothing for preview environment
          vercel-args: "${{ github.ref == 'refs/heads/master' && '--prod' || '' }}"
          working-directory: ./web

      - name: Update Deployment Status
        uses: bobheadxi/[email protected]
        if: always()
        with:
          step: finish
          token: ${{ secrets.GH_TOKEN }}
          status: ${{ job.status }}
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          env_url: ${{ steps.vercel-action.outputs.preview-url }}

Upvotes: 5

Views: 6400

Answers (1)

Eskandar Abedini
Eskandar Abedini

Reputation: 2154

You can enable the "Ignored Build Step" field by referring to the documentation of this feature. If the command returns "0", the build will be skipped. If, however, a code "1" or greater is returned, then a new deployment will be built.

With a Script

#!/bin/bash

echo "VERCEL_ENV: $VERCEL_ENV"

if [[ "$VERCEL_ENV" == "production" ]] ; then
  # Proceed with the build
  echo "✅ - Build can proceed"
  exit 1;

else
  # Don't build
  echo "🛑 - Build cancelled"
  exit 0;
fi

With Environment Variables

#!/bin/bash

echo "VERCEL_GIT_COMMIT_REF: $VERCEL_GIT_COMMIT_REF"

if [[ "$VERCEL_GIT_COMMIT_REF" == "staging" || "$VERCEL_GIT_COMMIT_REF" == "main"  ]] ; then
  # Proceed with the build
    echo "✅ - Build can proceed"
  exit 1;

else
  # Don't build
  echo "🛑 - Build cancelled"
  exit 0;
fi

With Folders and Workspaces

To build a new deployment considering only a certain folder, you can use the following command:

git diff HEAD^ HEAD --quiet ./packages/frontend/

The above was copied from here (includes more details):

How do I use the "Ignored Build Step" field on Vercel?

Video tutorial:

How to Ignore Build Step on Vercel

Another guide:

Vercel – ignore GitHub branches

Upvotes: 4

Related Questions