Skip to content

Commit

Permalink
feat(short_sha): add a shortened sha commit id
Browse files Browse the repository at this point in the history
  • Loading branch information
Ameausoone authored and rlespinasse committed Nov 11, 2019
1 parent 3876a4c commit d77acd4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ This action slug and expose some github variables.
- remove leading and trailing `-` caracter,
- limit the string size to 63 caracters.

Others `Slug`-ish commands are available:
- `Short SHA` a variable will limit the string size to 8 caracters.

## Environment Variables

| GitHub environment variable | Slug variable |
Expand All @@ -17,6 +20,10 @@ This action slug and expose some github variables.
| GITHUB_HEAD_REF | GITHUB_HEAD_REF_SLUG |
| GITHUB_BASE_REF | GITHUB_BASE_REF_SLUG |

| GitHub environment variable | Short variable |
| - | - |
| GITHUB_SHA | GITHUB_SHA_SHORT |

## Example usage

```yaml
Expand All @@ -26,4 +33,5 @@ This action slug and expose some github variables.
echo ${{ env.GITHUB_REF_SLUG }}
echo ${{ env.GITHUB_HEAD_REF_SLUG }}
echo ${{ env.GITHUB_BASE_REF_SLUG }}
echo ${{ env.GITHUB_SHA_SHORT }}
```
6 changes: 6 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ slug_ref() {
| cut -c1-63
}

short_sha(){
echo "$1" \
| cut -c1-8
}

echo ::set-env name=GITHUB_REF_SLUG::"$(slug_ref "$GITHUB_REF")"
echo ::set-env name=GITHUB_HEAD_REF_SLUG::"$(slug_ref "$GITHUB_HEAD_REF")"
echo ::set-env name=GITHUB_BASE_REF_SLUG::"$(slug_ref "$GITHUB_BASE_REF")"
echo ::set-env name=GITHUB_SHA_SHORT::"$(short_sha "$GITHUB_SHA")"
19 changes: 19 additions & 0 deletions tests/short_sha.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bats

@test "Short long hash" {
test_short_sha \
"a35a1a486a260cfd99c5b6f8c6034a2929ba9b3f" \
"a35a1a48"
}

# Load sluf_ref function
source entrypoint.sh > /dev/null 2>&1

test_short_sha(){
given="${1}"
expected="${2}"

actual="$(short_sha ${given})"
echo "expected : [${expected}], actual : [${actual}]"
[ "${actual}" == "${expected}" ]
}

0 comments on commit d77acd4

Please sign in to comment.