From 29fb2c765f0e9c2488948472e2c0eb2acce4ff95 Mon Sep 17 00:00:00 2001 From: cytopia Date: Tue, 29 Mar 2022 15:54:15 +0200 Subject: [PATCH] Add output flag: has_refs --- .../{test.yml => test-with-refs.yml} | 2 +- .github/workflows/test-without-refs.yml | 55 +++++++++++++++++++ README.md | 10 ++-- action.yml | 37 ++++++++++--- 4 files changed, 91 insertions(+), 13 deletions(-) rename .github/workflows/{test.yml => test-with-refs.yml} (98%) create mode 100644 .github/workflows/test-without-refs.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test-with-refs.yml similarity index 98% rename from .github/workflows/test.yml rename to .github/workflows/test-with-refs.yml index 6070323..0ab920b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test-with-refs.yml @@ -1,5 +1,5 @@ --- -name: test +name: test-with on: pull_request: diff --git a/.github/workflows/test-without-refs.yml b/.github/workflows/test-without-refs.yml new file mode 100644 index 0000000..05e2ff4 --- /dev/null +++ b/.github/workflows/test-without-refs.yml @@ -0,0 +1,55 @@ +--- +name: test-without +on: + pull_request: + +jobs: + get-matrix: + runs-on: ubuntu-latest + name: get-matrix + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Fetch matrix + id: set-matrix + uses: ./ + with: + repository_default_branch: master + branches: "" + tags: "" + num_latest_tags: 0 + + info: + needs: [get-matrix] + runs-on: ubuntu-latest + name: info + steps: + - name: Show outputs (raw) + run: | + echo ${{ needs.get-matrix.outputs.matrix }} + - name: Show outputs (json) + run: | + echo ${{ fromJson(needs.get-matrix.outputs.matrix) }} + + build: + needs: [get-matrix] + runs-on: ubuntu-latest + name: build + strategy: + fail-fast: false + matrix: + refs: + - ${{ fromJson(needs.get-matrix.outputs.matrix) }} + steps: + - name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})" + uses: actions/checkout@v3 + with: + fetch-depth: 0 + ref: ${{ matrix.refs }} + - name: Show git + run: | + git log | head -20 diff --git a/README.md b/README.md index 5ee75ac..595f901 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ [![GitHub release](https://img.shields.io/github/release/cytopia/git-ref-matrix-action.svg?logo=github)](/~https://github.com/cytopia/git-ref-matrix-action/releases/latest) [![GitHub marketplace](https://img.shields.io/badge/marketplace-git--ref--matrix--action-blue?logo=github)](/~https://github.com/marketplace/actions/git-ref-matrix-action) [![](https://img.shields.io/badge/github-cytopia%2Fgit--ref--matrix--action-red.svg?logo=github)](/~https://github.com/cytopia/git-ref-matrix-action "github.com/cytopia/git-ref-matrix-action") -[![test](/~https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test.yml/badge.svg)](/~https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test.yml) +[![test-with](/~https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test.yml/badge.svg)](/~https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test-with.yml) +[![test-without](/~https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test.yml/badge.svg)](/~https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test-without.yml) This composite action creates a stringified JSON list of git refs to be used as a build matrix. @@ -22,9 +23,10 @@ The following inputs can be used to alter the Docker tag name determination: ## :arrow_backward: Outputs -| Output | Description | -|--------------|---------------------------------------------------------------| -| `matrix` | Stringified JSON build matrix. Example: `["master","v0.1.0"]` | +| Output | Description | +|--------------|-----------------------------------------------------------------------------| +| `matrix` | Stringified JSON build matrix. Example: `["master","v0.1.0"]` | +| `has_refs` | Flag (0 or 1) that tells if we have matrix refs or not. Example: `0` or `1` | ## :computer: Usage diff --git a/action.yml b/action.yml index 8af4e75..ace9bc5 100644 --- a/action.yml +++ b/action.yml @@ -27,8 +27,11 @@ inputs: outputs: matrix: - description: "Stringified JSON build matrix for defined git refs." + description: "Stringified JSON build matrix for defined git refs. (list of strings)" value: ${{ steps.set-matrix.outputs.matrix }} + has_refs: + description: "Flag (0 or 1) that tells if we have matrix (list not empty) refs or not (list empty)." + value: ${{ steps.set-matrix.outputs.has_refs }} runs: using: "composite" @@ -46,7 +49,6 @@ runs: id: set-matrix shell: bash run: | - ### ### Convert comma separated branches and tags to newline separated ### @@ -54,10 +56,14 @@ runs: TAGS="$( echo "${{ inputs.tags }}" | sed 's/,/\n/g' )" echo "BRANCHES:" + echo "-------------------------" echo "${BRANCHES}" + echo echo "TAGS:" + echo "-------------------------" echo "${TAGS}" + echo ### ### Get x number of latest tags of this repository (newline separated) @@ -70,37 +76,52 @@ runs: fi echo "LATEST_TAGS:" + echo "-------------------------" echo "${LATEST_TAGS}" - + echo ### ### All newline separated refs (and make unique in case of duplicated tags) ### - REFS="$( printf "%s\n%s\n%s\n" "${BRANCHES}" "${TAGS}" "${LATEST_TAGS}" | grep -Ev '^$' | sort -u )" + REFS="$( printf "%s\n%s\n%s\n" "${BRANCHES}" "${TAGS}" "${LATEST_TAGS}" | grep -Ev '^$' || true | sort -u )" echo "REFS:" + echo "-------------------------" echo "${REFS}" + echo ### ### Create element double-quoted and comma separated string (has leading comma) ### JSON='' while IFS= read -r line; do - JSON="${JSON},$( printf '"%s"' "${line}" )" + if [ -n "${line}" ]; then + JSON="${JSON},$( printf '"%s"' "${line}" )" + fi done <<< "${REFS}" ### ### Remove leading comma and encapsulate in square brackets ### - JSON="$( printf '[%s]\n' "${JSON#,}" )" + JSON="$( printf '[%s]' "${JSON#,}" )" ### ### Set final output for 'matrix' ### echo "::set-output name=matrix::${JSON}" + ### + ### Set 'has_refs' + ### + if [ "${JSON}" = "[]" ]; then + HAS_REFS="0" + else + HAS_REFS="1" + fi + echo "::set-output name=has_refs::${HAS_REFS}" + ### ### Output matrix ### - echo "JSON=${JSON}" - echo JSON=${JSON} + echo "matrix=${JSON}" + echo "has_refs=${HAS_REFS}"