Skip to content

Commit b396eba

Browse files
CodeGatatteggiani
andauthored
Add get-git-ref-info action, update READMEs (#39)
* Add get-git-ref-info action, update READMEs * Apply suggestions from code review Co-authored-by: Davide Marchegiani <davide.marchegiani@anu.edu.au> * Check if the ref is both a branch and a tag first, and error out if so --------- Co-authored-by: Davide Marchegiani <davide.marchegiani@anu.edu.au>
1 parent 03d17ee commit b396eba

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Get git ref info Action
2+
3+
This action is a shorthand way to determine whether a given git ref is a branch, tag or commit.
4+
5+
If the specified ref doesn't exist, this action will fail with an error.
6+
7+
## Inputs
8+
9+
| Name | Description | Required | Default | Example |
10+
| ---- | ----------- | -------- | ------- | ------- |
11+
| `repository` | The repository to get the git ref info from, in the format `owner/repo` | `true` | N/A | `"ACCESS-NRI/spack-packages"` |
12+
| `ref` | The simple git reference (branch, tag, or commit SHA) to retrieve information about | `true` | N/A | `some-feature-branch` (branch), `v2.1` (tag), `8d29u23` (short SHA), `03d17eebfda5b873c576bad1dd77acdb8` (full SHA) |
13+
| `token` | A GitHub Token/PAT that allows cloning of the repository | `false` | `github.token` | `gha_pat_abcds...` |
14+
15+
## Outputs
16+
17+
| Name | Description | Example |
18+
| ---- | ----------- | ------- |
19+
| `ref-type` | Type of the given ref, if it's valid | One of `branch`, `tag`, `commit` |
20+
| `sha` | Full SHA of the given ref, if it's valid | `03d17eebfda5b873c576bad1dd77acdb8` |
21+
22+
## Examples
23+
24+
```yaml
25+
# ...
26+
jobs:
27+
get-ref:
28+
runs-on: ubuntu-latest
29+
env:
30+
ref: main
31+
steps:
32+
- id: resolve-ref
33+
uses: access-nri/actions/.github/actions/get-git-ref-info@main
34+
with:
35+
repository: ACCESS-NRI/actions@main
36+
ref: ${{ env.ref }}
37+
38+
- run: echo "Ref ${{ env.ref }} is a ${{ steps.resolve-ref.outputs.ref-type }} with SHA ${{ steps.resolve-ref.outputs.sha }}"
39+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Get git ref info
2+
description: Action that retrieves information about a git reference
3+
inputs:
4+
repository:
5+
required: true
6+
description: The repository to get the git ref info from, in the format 'owner/repo'.
7+
ref:
8+
required: true
9+
description: The git reference (branch, tag, or commit SHA) to retrieve information about
10+
token:
11+
required: false
12+
description: |
13+
The GitHub token to use for authentication when accessing the repository.
14+
Defaults to github.token.
15+
outputs:
16+
ref-type:
17+
description: The type of the git reference (branch, tag, or commit)
18+
value: ${{ steps.ref-info.outputs.ref-type }}
19+
sha:
20+
description: The commit SHA that the git reference points to
21+
value: ${{ steps.ref-info.outputs.sha }}
22+
runs:
23+
using: composite
24+
steps:
25+
- name: Checkout ${{ inputs.repository }}
26+
uses: actions/checkout@v5
27+
with:
28+
repository: ${{ inputs.repository }}
29+
fetch-depth: 0
30+
fetch-tags: true
31+
token: ${{ inputs.token || github.token }}
32+
33+
- name: Get Git Ref Info
34+
id: ref-info
35+
shell: bash
36+
# Determine if the ref is a branch, tag, or commit SHA, and get the SHA for the given ref
37+
run: |
38+
# Check if the ref exists as both a branch and a tag, in which case we error out
39+
if git show-ref --verify --quiet 'refs/heads/${{ inputs.ref }}' && git show-ref --verify --quiet 'refs/tags/${{ inputs.ref }}'; then
40+
echo "::error::The specified ref '${{ inputs.ref }}' exists as both a branch and a tag in the repository '${{ inputs.repository }}'. Please specify a unique ref."
41+
exit 1
42+
fi
43+
44+
# Check if the ref is a branch, tag, or commit
45+
if git show-ref --verify --quiet 'refs/heads/${{ inputs.ref }}'; then
46+
echo "ref-type=branch" >> $GITHUB_OUTPUT
47+
echo "sha=$(git rev-parse "refs/heads/${{ inputs.ref }}")" >> $GITHUB_OUTPUT
48+
49+
elif git show-ref --verify --quiet 'refs/tags/${{ inputs.ref }}'; then
50+
echo "ref-type=tag" >> $GITHUB_OUTPUT
51+
echo "sha=$(git rev-parse 'refs/tags/${{ inputs.ref }}')" >> $GITHUB_OUTPUT
52+
53+
elif git cat-file -e '${{ inputs.ref }}^{commit}'; then
54+
echo "ref-type=commit" >> $GITHUB_OUTPUT
55+
echo "sha=$(git rev-parse '${{ inputs.ref }}')" >> $GITHUB_OUTPUT
56+
57+
else
58+
echo "::error::The specified ref '${{ inputs.ref }}' does not exist in the repository '${{ inputs.repository }}'."
59+
exit 1
60+
fi

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Custom GitHub Actions and Workflows for use across the ACCESS-NRI.
1212
* [commenter-permissions README.md](.github/actions/commenter-permission-check/README.md): Determine whether a commenter on a PR has write permissions on the repository.
1313
* [comment](.github/actions/comment/README.md): Add, modify or delete a comment on a given issue or PR.
1414
* [cache-svn-auth](.github/actions/cache-svn-auth/README.md): Cache Subversion (SVN)'s authentication password.
15+
* [get-git-ref-info](.github/actions/get-git-ref-info/README.md): Determine if a given git ref is a branch, tag or SHA, if it exists.
1516

1617
## Workflows
1718

0 commit comments

Comments
 (0)