|
| 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 |
0 commit comments