How to Access the Commit SHA for Your Build

Last updated: February 10, 2026

When deploying applications on Porter, you may need to access the commit SHA that your build is based on. This can be useful for tracking versions, debugging, or other purposes. Here are two methods to obtain the commit SHA, depending on your build process and when you need the information.

Method 1: Using Build-time Environment Variables (Dockerfile)

If you're using a Dockerfile and need the commit SHA at build time, follow these steps:

  1. Obtain the commit SHA before your build process starts.

  2. Set the commit SHA as an environment variable in the env: block of the setup-porter action in your GH workflow file. It should begin with PORTER_.

  3. Insert the SHA into your Dockerfile as a build-time environment variable.

  4. Use this variable to set an environment variable in your final image.

Example Dockerfile:

ARG PORTER_COMMIT_SHA # you need to set this env var in the `env:` block of your `setup-porter` action in your GH workflow file
ENV APP_COMMIT_SHA=$PORTER_COMMIT_SHA

# Rest of your Dockerfile...

For more details on using build-time environment variables, refer to our documentation on builds.

Method 2: Setting Environment Variables at Runtime

If you need to set the commit SHA at runtime or are using Buildpacks, you can use the Porter CLI to set environment variables:

  1. In your CI/CD pipeline, after obtaining the commit SHA, use the following command:

porter env set COMMIT_SHA=<your-commit-sha> (-a <app-name>|-g <env-group-name>)

This command will set the COMMIT_SHA environment variable for your application, which you can then access at runtime.

For more information on setting environment variables using the Porter CLI, see our documentation on porter env set.

Choosing the Right Method

Choose Method 1 if you're using Dockerfiles and need the SHA at build time. Choose Method 2 if you're using Buildpacks or need the SHA at runtime. Both methods can be integrated into your CI/CD pipeline to automate the process of capturing and using the commit SHA.