From d77acd4f478b6971e0f7b2c9d1d4e721032bc5ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20M=C3=A9ausoone?= Date: Mon, 11 Nov 2019 15:53:51 +0100 Subject: [PATCH] feat(short_sha): add a shortened sha commit id --- README.md | 8 ++++++++ entrypoint.sh | 6 ++++++ tests/short_sha.bats | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 tests/short_sha.bats diff --git a/README.md b/README.md index c38667f..db2b5ec 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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 @@ -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 }} ``` diff --git a/entrypoint.sh b/entrypoint.sh index f33431b..0083292 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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")" diff --git a/tests/short_sha.bats b/tests/short_sha.bats new file mode 100644 index 0000000..97ef617 --- /dev/null +++ b/tests/short_sha.bats @@ -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}" ] +} \ No newline at end of file