feat: support action failure on bad revision

This commit is contained in:
rlespinasse
2021-12-05 12:20:48 +01:00
committed by Romain Lespinasse
parent 353f0c732e
commit 3549754992
4 changed files with 63 additions and 4 deletions

View File

@@ -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:

View File

@@ -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.
`<NAME>`, and `<NAME>_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 `<NAME>_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`.

View File

@@ -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 }}

View File

@@ -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