diff --git a/.github/workflows/shortify-git-revision.yaml b/.github/workflows/shortify-git-revision.yaml index 5f7e4ac..0e1669d 100644 --- a/.github/workflows/shortify-git-revision.yaml +++ b/.github/workflows/shortify-git-revision.yaml @@ -17,6 +17,7 @@ jobs: # Test 1 - name: Shortify an existing git revision + id: shortify-an-existing-git-revision uses: ./ with: name: ROOT_COMMIT @@ -25,6 +26,8 @@ jobs: run: | [[ "${{ env.ROOT_COMMIT }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]] [[ "${{ 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 # Test 2 @@ -138,6 +141,22 @@ jobs: [[ "${{ env.WRONGFULLY_SIZED_REVISION_SHORT }}" == "88428f5" ]] shell: bash + # Test 10 + - name: Shortify an existing git revision without env publication + id: shortify-an-existing-git-revision-without-env-publication + uses: ./ + 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 @@ -166,6 +185,7 @@ jobs: # Test 2 - name: Short on error + id: short-on-error uses: ./this-action with: name: ROOT_COMMIT @@ -176,6 +196,8 @@ jobs: 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: diff --git a/README.md b/README.md index 1ada543..dcd2020 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,19 @@ If a revision is a bad revision, this action will produce an error message and f - `SIZED_REVISION` - `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 ### `name` @@ -107,3 +120,10 @@ This input is _Optional_. the `short` sha produce will have the length defined by the input. This input is _Optional_. + +## Outputs + +| Output | Description | +| -------- | --------------------------- | +| revision | The revision to be shortify | +| short | Revision Short | diff --git a/action.yml b/action.yml index bff0fce..1bf9694 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,9 @@ name: "Shortify Git Revision" description: "Github Action to shortify a git revision" author: "Romain Lespinasse" +branding: + icon: "crop" + color: "gray-dark" inputs: name: description: "Environment variable that will hold the value and serve as prefix to shortify value" @@ -24,13 +27,22 @@ inputs: description: "Value to configure the length of the shorted sha" default: "" required: false -branding: - icon: "crop" - color: "gray-dark" + publish-env: + description: "Publish short as environment variable" + 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: using: "composite" steps: - - run: $GITHUB_ACTION_PATH/shortify.sh + - id: shortify + run: $GITHUB_ACTION_PATH/shortify.sh shell: bash env: INPUT_NAME: ${{ inputs.name }} @@ -39,3 +51,4 @@ runs: INPUT_SHORT_ON_ERROR: ${{ inputs.short-on-error }} INPUT_PREFIX: ${{ inputs.prefix }} INPUT_LENGTH: ${{ inputs.length }} + INPUT_PUBLISH_ENV: ${{ inputs.publish-env }} diff --git a/shortify.sh b/shortify.sh index 0f5a29a..a2c2874 100755 --- a/shortify.sh +++ b/shortify.sh @@ -27,22 +27,31 @@ if [ -z "${REVISION}" ]; then exit 0 fi +SHORT_PUBLICATION=false if [ "$(git cat-file -e "${REVISION}" 2>&1)" == "" ]; then - { - echo "${PREFIX}${NAME}=${REVISION}" - echo "${PREFIX}${NAME}_SHORT=$(git rev-parse --short"${SHORT_LENGTH}" "${REVISION}")" - } >>"$GITHUB_ENV" + SHORT_VALUE=$(git rev-parse --short"${SHORT_LENGTH}" "${REVISION}") + SHORT_PUBLICATION="true" 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" + SHORT_VALUE=$(cut -c1-"${INPUT_LENGTH}" <<<"${REVISION}") + SHORT_PUBLICATION="true" else - echo "::error ::The input 'lenght' is mandatory with 'short-on-error' set to 'true'" + echo "::error ::The input 'length' 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 + +if [ "${SHORT_PUBLICATION}" == "true" ]; then + echo "::set-output name=revision::${REVISION}" + echo "::set-output name=short::${SHORT_VALUE}" + + if [ "${INPUT_PUBLISH_ENV}" == "true" ]; then + { + echo "${PREFIX}${NAME}=${REVISION}" + echo "${PREFIX}${NAME}_SHORT=${SHORT_VALUE}" + } >>"$GITHUB_ENV" + fi +fi