mirror of
https://github.com/rlespinasse/shortify-git-revision.git
synced 2026-05-18 12:42:20 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4936ef9a58 | ||
|
|
ef0d40cdf6 | ||
|
|
512ffd9023 |
109
.github/workflows/shortify-git-revision.yaml
vendored
109
.github/workflows/shortify-git-revision.yaml
vendored
@@ -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
|
||||
@@ -93,15 +90,101 @@ jobs:
|
||||
[[ -z "${{ env.WRONG_AND_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 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
|
||||
|
||||
42
README.md
42
README.md
@@ -4,7 +4,7 @@
|
||||
|
||||
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.
|
||||
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
|
||||
@@ -12,7 +12,8 @@ If a revision is a bad revision, this action will produce an error message and f
|
||||
- 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
|
||||
```
|
||||
@@ -24,7 +25,8 @@ If a revision is a bad revision, this action will produce an error message and f
|
||||
- Shortify an environment variable with prefix
|
||||
|
||||
```yaml
|
||||
- uses: rlespinasse/shortify-git-revision@v1.x
|
||||
- uses: actions/checkout@v3
|
||||
- uses: rlespinasse/shortify-git-revision@v1
|
||||
with:
|
||||
name: GITHUB_SHA
|
||||
prefix: CI_
|
||||
@@ -38,7 +40,8 @@ If a revision is a bad revision, this action will produce an error message and f
|
||||
- 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
|
||||
@@ -49,6 +52,22 @@ If a revision is a bad revision, this action will produce an error message and f
|
||||
- `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`
|
||||
@@ -68,8 +87,23 @@ 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_.
|
||||
|
||||
10
action.yml
10
action.yml
@@ -12,10 +12,18 @@ inputs:
|
||||
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"
|
||||
@@ -28,4 +36,6 @@ runs:
|
||||
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 }}
|
||||
|
||||
30
shortify.sh
30
shortify.sh
@@ -11,16 +11,38 @@ else
|
||||
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" 2>&1)" == "" ]; then
|
||||
if [ "$(git cat-file -e "${REVISION}" 2>&1)" == "" ]; then
|
||||
{
|
||||
echo "${PREFIX}${NAME}=${REVISION}"
|
||||
echo "${PREFIX}${NAME}_SHORT=$(git rev-parse --short "$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"
|
||||
echo "::error ::Invalid revision: ${REVISION} from ${NAME}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user