Skip to content
Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Commit Hash

A GitHub Action for using the long and short hash of a commit — with an optional prefix.

prompt/actions-commit-hash@v4

📦 Automatic Release Packaging is used by this action, please reference by tag or commit hash in your Workflows.

Inputs

All inputs are optional.

IDDescriptionDefaultExamples
commitFull, 40-character SHA-1 hash of the commit${{github.sha}}734713bc047d87bf7eac9674765ae793478c50d3
prefixOptional string prepended to short and longcommitsha-

Outputs

IDDescriptionExamples
shortShort, 7-character hash of the commit with optional prefix734713bsha-734713b
longFull, 40-character SHA-1 hash of the commit with optional prefix734713bc047d87bf7eac9674765ae793478c50d3sha-734713bc047d87bf7eac9674765ae793478c50d3
hashOriginal, 40-character SHA-1 hash of the commit without prefix734713bc047d87bf7eac9674765ae793478c50d3

Examples

Build image with commit tag

docker/build-push-action builds Docker images with tags, here we tag the image we're building with the short commit hash.

jobs:
tag-image:
runs-on: ubuntu-lateststeps:
steps:
- id: commituses: prompt/actions-commit-hash@v4
- uses: docker/setup-buildx-action@v4
- name: Build image for commituses: docker/build-push-action@v7with:
push: truetags: ${{ steps.commit.outputs.short }}

Prefix commit hash for workflow_run event

workflow_run events are handled by Workflows within the context of the main branch, therefore the github.sha context value does not represent the commit that triggered the Workflow and we must use the head_sha value on the event instead.

on:
workflow_run:
workflows:
- Buildtypes:
- completedjobs:
tag-image:
runs-on: ubuntu-lateststeps:
steps:
- id: commituses: prompt/actions-commit-hash@v4with:
commit: "${{ github.event.workflow_run.head_sha }}"prefix: "sha-"
- uses: docker/setup-buildx-action@v4
- name: Build image for commituses: docker/build-push-action@v7with:
push: truetags: ${{ steps.commit.outputs.short }}

Automatic Release Packaging

Packaging (creation of dist) happens automatically when a new tag is created. Any reference to this Action in a Workflow must use a tag (mutable) or the commit hash of a tag (immutable).

✅ uses: prompt/actions-commit-hash@v4✅ uses: prompt/actions-commit-hash@v4.0.0✅ uses: prompt/actions-commit-hash@01d19a83c242e1851c9aa6cf9625092ecd095d09❌ uses: prompt/actions-commit-hash@main

The blog post Package GitHub Actions automatically with GitHub Actions describes how this is achieved.

About

:octocat: Output commit hash (short and long) with an optional prefix

Topics

Resources

Stars

15 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages