VERCEL_GIT_COMMIT_SHA variable is empty

const sourceCommit: string = process.env.VERCEL_GIT_COMMIT_SHA ?? “unknown”; it is empty, i expect the git commit hash

How do get a git commit hash variable ? Do I need to type _.env ?

I cannot understand the wiki :
System environment variables ← how do I run this command : VERCEL_GIT_COMMIT_SHA=fa1eade47b73733d6312d5abfad33ce9e4068081

app router “next”: “15.1.3” , using vercel project

THe above steps should do the ticket. Are you reading commit SHA in source code (runtime) or during build (build step)? You can replace buildCommand with echo $VERCEL_GIT_COMMIT_SHA && npm run build or similar to figure out if the env var indeed available or not

Yes, I can see variable $VERCEL_GIT_COMMIT_SHA when I echo it in the vercel build command.

How do I access the variable in my client side code(example Page.tsx )?

Environment variables are by default only available on the server side. To access Vercel’s system environment variables like VERCEL_GIT_COMMIT_SHA on the client side, you need to follow a specific approach.

Option 1: Expose it with NEXT_PUBLIC prefix

You’ll need to create a new environment variable that’s prefixed with NEXT_PUBLIC_ and assign the value of VERCEL_GIT_COMMIT_SHA to it. This can be done in your project settings or build configuration.

NEXT_PUBLIC_GIT_COMMIT_SHA=$VERCEL_GIT_COMMIT_SHA

Then you can access it in your client-side code:

console.log(process.env.NEXT_PUBLIC_GIT_COMMIT_SHA)

Option 2: Pass it through a server component or API

Since VERCEL_GIT_COMMIT_SHA is available at both build and runtime , you can:

Using a Server Component

// app/page.tsx (Server Component)
import ClientComponent from './client-component'

export default function Page() {
  return <ClientComponent commitSha={process.env.VERCEL_GIT_COMMIT_SHA} />
}

Using an API Route

// app/api/git-info/route.ts
export async function GET() {
  return Response.json({
    commitSha: process.env.VERCEL_GIT_COMMIT_SHA
  })
}

Then fetch it from your client component:

'use client'

import { useEffect, useState } from 'react'

export default function GitInfo() {
  const [commitSha, setCommitSha] = useState('')
  
  useEffect(() => {
    fetch('/api/git-info')
      .then(res => res.json())
      .then(data => setCommitSha(data.commitSha))
  }, [])
  
  return <div>Commit SHA: {commitSha}</div>
}
1 Like

hi Swarnava

I am trying to use Option 1: Expose it with NEXT_PUBLIC prefix
but my build command does not work

  • my env varaible NEXT_PUBLIC_GIT_COMMIT_SHA=ABC

  • my build command :
    npx convex deploy --cmd ’ echo $VERCEL_GIT_COMMIT_SHA && set NEXT_PUBLIC_GIT_COMMIT_SHA = $VERCEL_GIT_COMMIT_SHA && npm run build’

my typescript code

Version:{ process.env.NEXT_PUBLIC_GIT_COMMIT_SHA ? process.env.NEXT_PUBLIC_GIT_COMMIT_SHA : “missing build#”} <— shows ABC

Can you share some vercel build command examples ?
Thsnks, Peter

hi Swarnava, using process.env.VERCEL_GIT_COMMIT_SHA locally returns a hash that is not match last commit
a) git rev-parse origin/main 37d9d82e9f27ae2a7694cec9a83e8c3ac1afc6b9 vs
b) 112233445566

vercel cli - this does not populate the project env variable - not sure why ??
npx convex deploy --cmd ‘set NEXT_PUBLIC_GIT_COMMIT_SHA = $VERCEL_GIT_COMMIT_SHA && npm run build’

I will start a new thread then.

This expectected since your CLI doesn’t know about git commit details. You need to ensure that relevant metadata is passed when you deploy. Why are my branch specific variables and domains not linked to my CLI deployments?

Can you share us a repro?

hi Swarnava,

githubCommitRef=“feature/branch-name” <-What does feature mean ?

My repo is zork0 , and I have 2 branches : master and main
I tried : vercel --prod -m githubDeployment=“1” -m githubCommitRef=“zork0/main” but l do not work

Here is my vercel build command:
npx convex deploy --cmd ‘set NEXT_PUBLIC_GIT_COMMIT_SHA = $VERCEL_GIT_COMMIT_SHA && npm run build’

Thanks,Peter

That’s branch name.

it should be githubCommitRef=“main”

hi Swarnava,
Can you find my mistake ?

I deployed like : >vercel --prod -m githubDeployment=“1” -m githubCommitRef=“main”

NEXT_PUBLIC_GIT_COMMIT_SHA = “git commit hash” ← This is the environ. variable - but it never changed.

this is my vercel buld :

npx convex deploy --cmd ‘set NEXT_PUBLIC_GIT_COMMIT_SHA = $VERCEL_GIT_COMMIT_SHA && npm run build’

hi, I suspect the problem is my build command .

I added a echo statement, but did not see the echo in the build logs .Iis Vercel using Standard Microsoft DOS interpreter ?

npx convex deploy --cmd ‘set NEXT_PUBLIC_GIT_COMMIT_SHA = $VERCEL_GIT_COMMIT_SHA && echo “**sha” && npm run build’