https://github.com/prompt/actions-commit-hash
:octocat: Output commit hash (short and long) with an optional prefix
https://github.com/prompt/actions-commit-hash
actions git-metadata github-actions
Last synced: 3 months ago
JSON representation
:octocat: Output commit hash (short and long) with an optional prefix
- Host: GitHub
- URL: https://github.com/prompt/actions-commit-hash
- Owner: prompt
- License: mit
- Created: 2021-06-01T17:16:29.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-09-12T21:46:42.000Z (almost 3 years ago)
- Last Synced: 2026-03-28T09:34:13.835Z (3 months ago)
- Topics: actions, git-metadata, github-actions
- Language: TypeScript
- Homepage:
- Size: 285 KB
- Stars: 16
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Commit Hash
A GitHub Action for using the long and short [hash of a commit][git/commits]
— with an optional prefix.
```
prompt/actions-commit-hash@v4
```
:package: [Automatic Release Packaging](#automatic-release-packaging) is used by
this action, please reference by tag or commit hash in your Workflows.
## Inputs
All inputs are optional.
| ID | Description | Default | Examples |
| :------- | :---------------------------------------------- | :---------------- | :----------------------------------------- |
| `commit` | Full, 40-character SHA-1 hash of the commit | `${{github.sha}}` | `734713bc047d87bf7eac9674765ae793478c50d3` |
| `prefix` | Optional string prepended to `short` and `long` | | `commit` `sha-` |
## Outputs
| ID | Description | Examples |
| :------ | :--------------------------------------------------------------- | :---------------------------------------------------------------------------------------- |
| `short` | Short, 7-character hash of the commit with optional prefix | `734713b` `sha-734713b` |
| `long` | Full, 40-character SHA-1 hash of the commit with optional prefix | `734713bc047d87bf7eac9674765ae793478c50d3` `sha-734713bc047d87bf7eac9674765ae793478c50d3` |
| `hash` | Original, 40-character SHA-1 hash of the commit without prefix | `734713bc047d87bf7eac9674765ae793478c50d3` |
## 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.
```yaml
jobs:
tag-image:
runs-on: ubuntu-latest
steps:
steps:
- id: commit
uses: prompt/actions-commit-hash@v4
- uses: docker/setup-buildx-action@v4
- name: Build image for commit
uses: docker/build-push-action@v7
with:
push: true
tags: ${{ steps.commit.outputs.short }}
```
### Prefix commit hash for `workflow_run` event
[`workflow_run`][events/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.
```yaml
on:
workflow_run:
workflows:
- Build
types:
- completed
jobs:
tag-image:
runs-on: ubuntu-latest
steps:
steps:
- id: commit
uses: prompt/actions-commit-hash@v4
with:
commit: "${{ github.event.workflow_run.head_sha }}"
prefix: "sha-"
- uses: docker/setup-buildx-action@v4
- name: Build image for commit
uses: docker/build-push-action@v7
with:
push: true
tags: ${{ 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][tags] (mutable) or
the commit hash of a tag (immutable).
```yaml
✅ 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][blog/package-automatically]
describes how this is achieved.
[git/commits]: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
[docker/build-push-action]: https://github.com/docker/build-push-action
[events/workflow_run]: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_run
[blog/package-automatically]: https://medium.com/prompt/package-github-actions-automatically-with-github-actions-a70b9f7bae4
[tags]: https://github.com/prompt/actions-commit-hash/tags