6 Commits

Author SHA1 Message Date
Romain Lespinasse
4936ef9a58 ci: remove concurrency when not vital 2022-03-19 10:50:14 +01:00
rlespinasse
ef0d40cdf6 feat: add short-on-error input 2022-03-19 07:48:08 +01:00
rlespinasse
512ffd9023 feat: enable short sha with specific length 2021-12-09 22:56:45 +01:00
Romain Lespinasse
928e740f90 feat: support prepend text to variable names 2021-12-08 21:37:22 +01:00
rlespinasse
3549754992 feat: support action failure on bad revision 2021-12-05 12:32:41 +01:00
rlespinasse
353f0c732e docs: correct the usage on env var 2021-12-04 23:54:50 +01:00
4 changed files with 271 additions and 24 deletions

View File

@@ -1,18 +1,15 @@
name: Shortify git revision testing
name: Shortify Git Revision
on: [push]
jobs:
shortify-git-revision-on-os:
os-testing:
strategy:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
concurrency:
group: shortify-git-revision-on-os-${{ github.ref }}
cancel-in-progress: true
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
# Test 1
- name: Shortify an existing git revision
@@ -20,46 +17,174 @@ 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" ]]
shell: bash
# Test 2
- name: Shortify an existing git revision with prefix
uses: ./
with:
name: ROOT_COMMIT
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
prefix: CI_
- name: Validate // Shortify an existing git revision with prefix
run: |
[[ "${{ env.ROOT_COMMIT }}" == "${{ env.CI_ROOT_COMMIT }}" ]]
[[ "${{ env.ROOT_COMMIT_SHORT }}" == "${{ env.CI_ROOT_COMMIT_SHORT }}" ]]
shell: bash
# Test 3
- name: Shortify an existing git revision from an env variable
uses: ./
with:
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" ]]
shell: bash
# Test 3
# Test 4
- name: Shortify a missing git revision
uses: ./
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
shortify-git-revision-release:
runs-on: ubuntu-latest
concurrency:
group: shortify-git-revision-release-${{ github.ref }}
needs: shortify-git-revision-on-os
# Test 5
- 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 6
- 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
# Test 7
- name: Shortify a git revision with specific length
uses: ./
with:
name: SIZED_REVISION
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
length: 10
- name: Validate // Shortify a git revision with specific length
run: |
[[ "${{ env.SIZED_REVISION }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]]
[[ "${{ env.SIZED_REVISION_SHORT }}" == "88428f56bd" ]]
shell: bash
# Test 8
- id: test-shortify-git-revision-with-wrong-length
name: Shortify a git revision with wrong length
uses: ./
with:
name: WRONGFULLY_SIZED_REVISION
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
length: "not_a_number"
continue-on-error: true
- name: Validate // Shortify a git revision with wrong length
run: |
[[ -z "${{ env.WRONGFULLY_SIZED_REVISION }}" ]]
[[ -z "${{ env.WRONGFULLY_SIZED_REVISION_SHORT }}" ]]
[[ "${{ steps.test-shortify-git-revision-with-wrong-length.outcome }}" == "failure" ]]
[[ "${{ steps.test-shortify-git-revision-with-wrong-length.conclusion }}" == "success" ]]
shell: bash
# Test 9
- name: Shortify a git revision with wrong length without failing
uses: ./
with:
name: WRONGFULLY_SIZED_REVISION
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
length: "not_a_number"
continue-on-error: true
- name: Validate // Shortify a git revision with wrong length without failing
run: |
[[ "${{ env.WRONGFULLY_SIZED_REVISION }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]]
[[ "${{ env.WRONGFULLY_SIZED_REVISION_SHORT }}" == "88428f5" ]]
shell: bash
error-os-testing:
strategy:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
path: this-action
ref: ${{ github.ref }}
# Test 1
- name: Continue on error
uses: ./this-action
with:
name: ROOT_COMMIT
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
continue-on-error: true
- name: Validate // Continue on error
run: |
[[ "${{ env.ROOT_COMMIT }}" == "" ]]
[[ "${{ env.ROOT_COMMIT_SHORT }}" == "" ]]
shell: bash
# Test 2
- name: Short on error
uses: ./this-action
with:
name: ROOT_COMMIT
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
short-on-error: true
length: 7
- name: Validate // Short on error
run: |
[[ "${{ env.ROOT_COMMIT }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]]
[[ "${{ env.ROOT_COMMIT_SHORT }}" == "88428f5" ]]
shell: bash
release:
runs-on: ubuntu-latest
concurrency:
group: release-${{ github.ref }}
needs:
- os-testing
- error-os-testing
steps:
- name: Checkout
uses: actions/checkout@v3
# Release
- name: Release this GitHub Action
uses: rlespinasse/release-that@v1.x
uses: rlespinasse/release-that@v1

View File

@@ -4,25 +4,44 @@
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 (This behavior can be override with `short-on-error` input).
`<NAME>`, and `<NAME>_SHORT` environment variable will only be available if the revision is not empty and valid.
## Usage
- Shortify an environment variable
```yaml
- uses: rlespinasse/shortify-git-revision@v1.x
- uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with:
name: GITHUB_SHA
revision: value_to_slugify
```
Will make available
- `GITHUB_SHA_SHORT`
- Shortify an environment variable with prefix
```yaml
- uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with:
name: GITHUB_SHA
prefix: CI_
```
Will make available
- `CI_GITHUB_SHA`
- `CI_GITHUB_SHA_SHORT`
- Shortify any revision
```yaml
- uses: rlespinasse/slugify-value@v1.x
- uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with:
name: SOME_REVISION
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
@@ -32,3 +51,59 @@ Produce short revision environment variable based on the input one.
- `SOME_REVISION`
- `SOME_REVISION_SHORT`
- Shortify a revision with a specific length
```yaml
- uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with:
name: SIZED_REVISION
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
length: 10
```
Will make available
- `SIZED_REVISION`
- `SIZED_REVISION_SHORT` (with value `88428f56bd`)
## 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`.
### `short-on-error`
If the input is set to `true`, this action will short a bad revision
The default value is `false`.
> If this input is set to `true`, the input `continue-on-error` input will be ignored.
> If this input is set to `true`, the input `length` input is mandatory.
### `prefix`
The value will be prepend to each generated variable.
This input is _Optional_.
### `length`
the `short` sha produce will have the length defined by the input.
This input is _Optional_.

View File

@@ -8,6 +8,22 @@ 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
short-on-error:
description: "Still short it if the git revision isn't valid"
default: "false"
required: false
prefix:
description: "Value to prepend to each generated variable"
default: ""
required: false
length:
description: "Value to configure the length of the shorted sha"
default: ""
required: false
branding:
icon: "crop"
color: "gray-dark"
@@ -19,3 +35,7 @@ runs:
env:
INPUT_NAME: ${{ inputs.name }}
INPUT_REVISION: ${{ inputs.revision }}
INPUT_CONTINUE_ON_ERROR: ${{ inputs.continue-on-error }}
INPUT_SHORT_ON_ERROR: ${{ inputs.short-on-error }}
INPUT_PREFIX: ${{ inputs.prefix }}
INPUT_LENGTH: ${{ inputs.length }}

View File

@@ -4,18 +4,45 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
# On MacOS,
# bash don't support substitution, so we use 'tr'
NAME=$(echo "$INPUT_NAME" | tr '[:lower:]' '[:upper:]')
PREFIX=$(echo "$INPUT_PREFIX" | tr '[:lower:]' '[:upper:]')
else
NAME=${INPUT_NAME^^}
PREFIX=${INPUT_PREFIX^^}
fi
REVISION=${INPUT_REVISION:-${!NAME}}
if [ -z "$REVISION" ]; then
SHORT_LENGTH=""
if [ "${INPUT_LENGTH}" != "" ]; then
if [ "${INPUT_LENGTH}" -eq "${INPUT_LENGTH}" ] 2>/dev/null; then
SHORT_LENGTH="=${INPUT_LENGTH}"
elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then
echo "::error ::Invalid length: ${INPUT_LENGTH}, should be a number"
exit 1
else
echo "::warning ::Invalid length: ${INPUT_LENGTH}, the default length will be used."
fi
fi
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")"
echo "${PREFIX}${NAME}=${REVISION}"
echo "${PREFIX}${NAME}_SHORT=$(git rev-parse --short"${SHORT_LENGTH}" "${REVISION}")"
} >>"$GITHUB_ENV"
elif [ "${INPUT_SHORT_ON_ERROR}" == "true" ]; then
if [ -n "${INPUT_LENGTH}" ]; then
{
echo "${PREFIX}${NAME}=${REVISION}"
echo "${PREFIX}${NAME}_SHORT=$(cut -c1-"${INPUT_LENGTH}" <<<"${REVISION}")"
} >>"$GITHUB_ENV"
else
echo "::error ::The input 'lenght' is mandatory with 'short-on-error' set to 'true'"
exit 1
fi
elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then
echo "::error ::Invalid revision: ${REVISION} from ${NAME}"
exit 1
fi