6 Commits

Author SHA1 Message Date
Romain Lespinasse
c90ba7007e feat: support GHES step output management 2022-10-31 19:45:56 +01:00
Romain Lespinasse
3b1b863e54 fix: use environment file to manage outputs 2022-10-15 21:26:27 +02:00
rlespinasse
4c6b226848 feat: expose short value as output 2022-04-17 21:03:27 +02:00
Romain Lespinasse
8198b7ea44 ci: enable build on fork pull request 2022-03-27 07:27:36 +02:00
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
4 changed files with 174 additions and 34 deletions

View File

@@ -1,21 +1,23 @@
name: Shortify git revision testing name: Shortify Git Revision
on: [push] on:
push:
branches:
- v1.x
pull_request:
jobs: jobs:
shortify-git-revision-on-os: os-testing:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-latest, ubuntu-latest, windows-latest] os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
concurrency:
group: shortify-git-revision-on-os-${{ github.ref }}
cancel-in-progress: true
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v3
# Test 1 # Test 1
- name: Shortify an existing git revision - name: Shortify an existing git revision
id: shortify-an-existing-git-revision
uses: ./ uses: ./
with: with:
name: ROOT_COMMIT name: ROOT_COMMIT
@@ -24,6 +26,8 @@ jobs:
run: | run: |
[[ "${{ env.ROOT_COMMIT }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]] [[ "${{ env.ROOT_COMMIT }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]]
[[ "${{ env.ROOT_COMMIT_SHORT }}" == "88428f5" ]] [[ "${{ env.ROOT_COMMIT_SHORT }}" == "88428f5" ]]
[[ "${{ env.ROOT_COMMIT }}" == "${{ steps.shortify-an-existing-git-revision.outputs.revision }}" ]]
[[ "${{ env.ROOT_COMMIT_SHORT }}" == "${{ steps.shortify-an-existing-git-revision.outputs.short }}" ]]
shell: bash shell: bash
# Test 2 # Test 2
@@ -137,15 +141,76 @@ jobs:
[[ "${{ env.WRONGFULLY_SIZED_REVISION_SHORT }}" == "88428f5" ]] [[ "${{ env.WRONGFULLY_SIZED_REVISION_SHORT }}" == "88428f5" ]]
shell: bash shell: bash
shortify-git-revision-release: # Test 10
runs-on: ubuntu-latest - name: Shortify an existing git revision without env publication
concurrency: id: shortify-an-existing-git-revision-without-env-publication
group: shortify-git-revision-release-${{ github.ref }} uses: ./
needs: shortify-git-revision-on-os with:
name: SHORT_WITHOUT_ENV_PUBLICATION
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
publish-env: false
- name: Validate // Shortify an existing git revision without env publication
run: |
[[ -z "${{ env.SHORT_WITHOUT_ENV_PUBLICATION }}" ]]
[[ -z "${{ env.SHORT_WITHOUT_ENV_PUBLICATION }}" ]]
[[ "${{ steps.shortify-an-existing-git-revision-without-env-publication.outputs.revision }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]]
[[ "${{ steps.shortify-an-existing-git-revision-without-env-publication.outputs.short }}" == "88428f5" ]]
shell: bash
error-os-testing:
strategy:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps: steps:
- name: Checkout - 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
id: 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" ]]
[[ "${{ env.ROOT_COMMIT }}" == "${{ steps.short-on-error.outputs.revision }}" ]]
[[ "${{ env.ROOT_COMMIT_SHORT }}" == "${{ steps.short-on-error.outputs.short }}" ]]
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 # Release
- name: Release this GitHub Action - name: Release this GitHub Action
uses: rlespinasse/release-that@v1.x uses: rlespinasse/release-that@v1

View File

@@ -4,7 +4,7 @@
Produce short revision environment variable based on the input one. 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. `<NAME>`, and `<NAME>_SHORT` environment variable will only be available if the revision is not empty and valid.
## Usage ## 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 - Shortify an environment variable
```yaml ```yaml
- uses: rlespinasse/shortify-git-revision@v1.x - uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with: with:
name: GITHUB_SHA 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 - Shortify an environment variable with prefix
```yaml ```yaml
- uses: rlespinasse/shortify-git-revision@v1.x - uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with: with:
name: GITHUB_SHA name: GITHUB_SHA
prefix: CI_ 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 - Shortify any revision
```yaml ```yaml
- uses: rlespinasse/shortify-git-revision@v1.x - uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with: with:
name: SOME_REVISION name: SOME_REVISION
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
@@ -52,7 +55,8 @@ If a revision is a bad revision, this action will produce an error message and f
- Shortify a revision with a specific length - Shortify a revision with a specific length
```yaml ```yaml
- uses: rlespinasse/shortify-git-revision@v1.x - uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with: with:
name: SIZED_REVISION name: SIZED_REVISION
revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 revision: 88428f56bd9d2751c47106bedfd148162dfa50b8
@@ -64,6 +68,19 @@ If a revision is a bad revision, this action will produce an error message and f
- `SIZED_REVISION` - `SIZED_REVISION`
- `SIZED_REVISION_SHORT` (with value `88428f56bd`) - `SIZED_REVISION_SHORT` (with value `88428f56bd`)
- Shortify without publishing the environment variables
```yaml
- uses: actions/checkout@v3
- uses: rlespinasse/shortify-git-revision@v1
with:
name: GITHUB_SHA
```
Will **not** make available
- `GITHUB_SHA_SHORT`
## Inputs ## Inputs
### `name` ### `name`
@@ -83,6 +100,15 @@ If the input is set to `true`, this action will not fail on a bad revision
The default value is `false`. 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` ### `prefix`
The value will be prepend to each generated variable. The value will be prepend to each generated variable.
@@ -94,3 +120,10 @@ This input is _Optional_.
the `short` sha produce will have the length defined by the input. the `short` sha produce will have the length defined by the input.
This input is _Optional_. This input is _Optional_.
## Outputs
| Output | Description |
| -------- | --------------------------- |
| revision | The revision to be shortify |
| short | Revision Short |

View File

@@ -1,6 +1,9 @@
name: "Shortify Git Revision" name: "Shortify Git Revision"
description: "Github Action to shortify a git revision" description: "Github Action to shortify a git revision"
author: "Romain Lespinasse" author: "Romain Lespinasse"
branding:
icon: "crop"
color: "gray-dark"
inputs: inputs:
name: name:
description: "Environment variable that will hold the value and serve as prefix to shortify value" description: "Environment variable that will hold the value and serve as prefix to shortify value"
@@ -12,6 +15,10 @@ inputs:
description: "Don't fail the action if the git revision isn't valid" description: "Don't fail the action if the git revision isn't valid"
default: "false" default: "false"
required: false required: false
short-on-error:
description: "Still short it if the git revision isn't valid"
default: "false"
required: false
prefix: prefix:
description: "Value to prepend to each generated variable" description: "Value to prepend to each generated variable"
default: "" default: ""
@@ -20,17 +27,28 @@ inputs:
description: "Value to configure the length of the shorted sha" description: "Value to configure the length of the shorted sha"
default: "" default: ""
required: false required: false
branding: publish-env:
icon: "crop" description: "Publish short as environment variable"
color: "gray-dark" default: "true"
required: false
outputs:
revision:
description: "The revision to be shortify"
value: ${{ steps.shortify.outputs.revision }}
short:
description: "Revision Short"
value: ${{ steps.shortify.outputs.short }}
runs: runs:
using: "composite" using: "composite"
steps: steps:
- run: $GITHUB_ACTION_PATH/shortify.sh - id: shortify
run: $GITHUB_ACTION_PATH/shortify.sh
shell: bash shell: bash
env: env:
INPUT_NAME: ${{ inputs.name }} INPUT_NAME: ${{ inputs.name }}
INPUT_REVISION: ${{ inputs.revision }} INPUT_REVISION: ${{ inputs.revision }}
INPUT_CONTINUE_ON_ERROR: ${{ inputs.continue-on-error }} INPUT_CONTINUE_ON_ERROR: ${{ inputs.continue-on-error }}
INPUT_SHORT_ON_ERROR: ${{ inputs.short-on-error }}
INPUT_PREFIX: ${{ inputs.prefix }} INPUT_PREFIX: ${{ inputs.prefix }}
INPUT_LENGTH: ${{ inputs.length }} INPUT_LENGTH: ${{ inputs.length }}
INPUT_PUBLISH_ENV: ${{ inputs.publish-env }}

View File

@@ -12,27 +12,51 @@ fi
REVISION=${INPUT_REVISION:-${!NAME}} REVISION=${INPUT_REVISION:-${!NAME}}
SHORT_LENGTH="" SHORT_LENGTH=""
if [ "$INPUT_LENGTH" != "" ]; then if [ "${INPUT_LENGTH}" != "" ]; then
if [ "$INPUT_LENGTH" -eq "$INPUT_LENGTH" ] 2>/dev/null; then if [ "${INPUT_LENGTH}" -eq "${INPUT_LENGTH}" ] 2>/dev/null; then
SHORT_LENGTH="=${INPUT_LENGTH}" SHORT_LENGTH="=${INPUT_LENGTH}"
elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then
echo "::error ::Invalid length: $INPUT_LENGTH, should be a number" echo "::error ::Invalid length: ${INPUT_LENGTH}, should be a number"
exit 1 exit 1
else else
echo "::warning ::Invalid length: $INPUT_LENGTH, the default length will be used." echo "::warning ::Invalid length: ${INPUT_LENGTH}, the default length will be used."
fi fi
fi fi
if [ -z "$REVISION" ]; then if [ -z "${REVISION}" ]; then
exit 0 exit 0
fi fi
if [ "$(git cat-file -e "$REVISION" 2>&1)" == "" ]; then SHORT_PUBLICATION=false
{ if [ "$(git cat-file -e "${REVISION}" 2>&1)" == "" ]; then
echo "${PREFIX}${NAME}=${REVISION}" SHORT_VALUE=$(git rev-parse --short"${SHORT_LENGTH}" "${REVISION}")
echo "${PREFIX}${NAME}_SHORT=$(git rev-parse --short"${SHORT_LENGTH}" "$REVISION")" SHORT_PUBLICATION="true"
} >>"$GITHUB_ENV" elif [ "${INPUT_SHORT_ON_ERROR}" == "true" ]; then
if [ -n "${INPUT_LENGTH}" ]; then
SHORT_VALUE=$(cut -c1-"${INPUT_LENGTH}" <<<"${REVISION}")
SHORT_PUBLICATION="true"
else
echo "::error ::The input 'length' is mandatory with 'short-on-error' set to 'true'"
exit 1
fi
elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then
echo "::error ::Invalid revision: $REVISION from $NAME" echo "::error ::Invalid revision: ${REVISION} from ${NAME}"
exit 1 exit 1
fi fi
if [ "${SHORT_PUBLICATION}" == "true" ]; then
if [ -f "$GITHUB_OUTPUT" ]; then
echo "revision=${REVISION}" >> "$GITHUB_OUTPUT"
echo "short=${SHORT_VALUE}" >> "$GITHUB_OUTPUT"
else
echo "::set-output name=revision::${REVISION}"
echo "::set-output name=short::${SHORT_VALUE}"
fi
if [ "${INPUT_PUBLISH_ENV}" == "true" ]; then
{
echo "${PREFIX}${NAME}=${REVISION}"
echo "${PREFIX}${NAME}_SHORT=${SHORT_VALUE}"
} >>"$GITHUB_ENV"
fi
fi