From 3549754992c5049f34fe96e785c159543229edc8 Mon Sep 17 00:00:00 2001 From: rlespinasse Date: Sun, 5 Dec 2021 12:20:48 +0100 Subject: [PATCH] feat: support action failure on bad revision --- .github/workflows/shortify-git-revision.yaml | 35 ++++++++++++++++++-- README.md | 22 ++++++++++++ action.yml | 5 +++ shortify.sh | 5 ++- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/.github/workflows/shortify-git-revision.yaml b/.github/workflows/shortify-git-revision.yaml index c7c9707..600c21c 100644 --- a/.github/workflows/shortify-git-revision.yaml +++ b/.github/workflows/shortify-git-revision.yaml @@ -20,7 +20,7 @@ jobs: with: name: ROOT_COMMIT revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 - - name: Test result 1 + - name: Validate // Shortify an existing git revision run: | [[ "${{ env.ROOT_COMMIT }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]] [[ "${{ env.ROOT_COMMIT_SHORT }}" == "88428f5" ]] @@ -33,7 +33,7 @@ jobs: name: ENV_VAR_COMMIT env: ENV_VAR_COMMIT: 88428f56bd9d2751c47106bedfd148162dfa50b8 - - name: Test result 1 + - name: Validate // Shortify an existing git revision from an env variable run: | [[ "${{ env.ENV_VAR_COMMIT }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]] [[ "${{ env.ENV_VAR_COMMIT_SHORT }}" == "88428f5" ]] @@ -45,12 +45,41 @@ jobs: with: name: MISSING_REVISION revision: "" - - name: Test result 3 + - name: Validate // Shortify a missing git revision run: | [[ -z "${{ env.MISSING_REVISION }}" ]] [[ -z "${{ env.MISSING_REVISION_SHORT }}" ]] shell: bash + # Test 4 + - id: test-shortify-wrong-git-revision + name: Shortify a wrong git revision + uses: ./ + with: + name: WRONG_REVISION + revision: wrongwrongwrongwrongwrongwrongwrongwrong + continue-on-error: true + - name: Validate // Shortify a wrong git revision + run: | + [[ -z "${{ env.WRONG_REVISION }}" ]] + [[ -z "${{ env.WRONG_REVISION_SHORT }}" ]] + [[ "${{ steps.test-shortify-wrong-git-revision.outcome }}" == "failure" ]] + [[ "${{ steps.test-shortify-wrong-git-revision.conclusion }}" == "success" ]] + shell: bash + + # Test 5 + - name: Shortify a wrong git revision without failing + uses: ./ + with: + name: WRONG_AND_MISSING_REVISION + revision: wrongwrongwrongwrongwrongwrongwrongwrong + continue-on-error: true + - name: Validate // Shortify a wrong git revision without failing + run: | + [[ -z "${{ env.WRONG_AND_MISSING_REVISION }}" ]] + [[ -z "${{ env.WRONG_AND_MISSING_REVISION_SHORT }}" ]] + shell: bash + shortify-git-revision-release: runs-on: ubuntu-latest concurrency: diff --git a/README.md b/README.md index 44bb686..a7ab0b0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ Produce short revision environment variable based on the input one. +If a revision is a bad revision, this action will produce an error message and fail depending on `continue-on-error` input value. +``, and `_SHORT` environment variable will only be available if the revision is not empty and valid. + ## Usage - Shortify an environment variable @@ -31,3 +34,22 @@ Produce short revision environment variable based on the input one. - `SOME_REVISION` - `SOME_REVISION_SHORT` + +## Inputs + +### `name` + +If used with `revision` input, it's the name of the environment variable containing the revision to shortify. +Otherwise, the `name` input will be used (in upper case) to define a environment variable containing the `revision` input value. + +### `revision` + +The revision to shortify into an environment variable named `_SHORT`. + +This input is _Optional_. + +### `continue-on-error` + +If the input is set to `true`, this action will not fail on a bad revision + +The default value is `false`. diff --git a/action.yml b/action.yml index 936f490..a6d6686 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,10 @@ inputs: revision: description: "Revision to short" required: false + continue-on-error: + description: "Don't fail the action if the git revision isn't valid" + default: "false" + required: false branding: icon: "crop" color: "gray-dark" @@ -19,3 +23,4 @@ runs: env: INPUT_NAME: ${{ inputs.name }} INPUT_REVISION: ${{ inputs.revision }} + INPUT_CONTINUE_ON_ERROR: ${{ inputs.continue-on-error }} diff --git a/shortify.sh b/shortify.sh index 87568e9..f9b53f2 100755 --- a/shortify.sh +++ b/shortify.sh @@ -13,9 +13,12 @@ if [ -z "$REVISION" ]; then exit 0 fi -if [ "$(git cat-file -e "$REVISION")" == "" ]; then +if [ "$(git cat-file -e "$REVISION" 2>&1)" == "" ]; then { echo "${NAME}=${REVISION}" echo "${NAME}_SHORT=$(git rev-parse --short "$REVISION")" } >>"$GITHUB_ENV" +elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then + echo "::error ::Invalid revision: $REVISION from $NAME" + exit 1 fi