From f7143f9a4b72fdde5cd6316d278fa05725bd9aa8 Mon Sep 17 00:00:00 2001 From: cytopia Date: Tue, 29 Mar 2022 16:30:03 +0200 Subject: [PATCH 1/3] Add input: disable_refs --- .github/workflows/test-disabled.yml | 56 ++++++++++ README.md | 2 + action.yml | 160 +++++++++++++++------------- 3 files changed, 145 insertions(+), 73 deletions(-) create mode 100644 .github/workflows/test-disabled.yml diff --git a/.github/workflows/test-disabled.yml b/.github/workflows/test-disabled.yml new file mode 100644 index 0000000..fc536e6 --- /dev/null +++ b/.github/workflows/test-disabled.yml @@ -0,0 +1,56 @@ +--- +name: test-disabled +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: master + tags: v0.1.0 + num_latest_tags: 1 + disable_refs: 1 + + 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 595f901..1518c1f 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [![](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-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) +[![test-disabled](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-disabled.yml) This composite action creates a stringified JSON list of git refs to be used as a build matrix. @@ -19,6 +20,7 @@ The following inputs can be used to alter the Docker tag name determination: | `branches` | No | `` | Optionally specify a comma separated list of branches to add to the build matrix. | | `tags` | No | `` | Optionally specify a comma separated list of tags to add to the build matrix. | | `num_latest_tags` | No | `0` | Optionally add x number of latest git tags to the build matrix (requires `repository_default_branch` to point to your repository's main branch. | +| `disable_refs` | No | `0` | Optionally force to disable all refs alltogether by returning an empty matrix and has_refs=0. | ## :arrow_backward: Outputs diff --git a/action.yml b/action.yml index ace9bc5..6fcad94 100644 --- a/action.yml +++ b/action.yml @@ -24,6 +24,10 @@ inputs: description: 'Number of latest tags to add to build matrix. Default: 0' required: false default: 0 + disable_refs: + description: 'A Flag to disable refs alltogether and return an empty matrix and has_refs=0.' + required: false + default: 0 outputs: matrix: @@ -43,85 +47,95 @@ runs: ref: ${{ inputs.repository_default_branch }} fetch-depth: 0 path: .git-ref-matrix-action - if: ${{ inputs.num_latest_tags > 0 }} + if: ${{ inputs.disable_refs == 0 && inputs.num_latest_tags > 0 }} - name: "[SETUP] Build and Export Matrix" id: set-matrix shell: bash run: | - ### - ### Convert comma separated branches and tags to newline separated - ### - BRANCHES="$( echo "${{ inputs.branches }}" | sed 's/,/\n/g' )" - 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) - ### - if [ "${{ inputs.num_latest_tags }}" != "0" ]; then - LATEST_TAGS="$( cd .git-ref-matrix-action && git tag --sort=creatordate | tail -${{ inputs.num_latest_tags }} )" - rm -r .git-ref-matrix-action + if [ "${{ inputs.disable_refs }}" = "1" ]; then + ### + ### Output matrix + ### + echo "::set-output name=matrix::[]" + echo "::set-output name=has_refs::0" + echo "matrix=[]" + echo "has_refs=0" else - LATEST_TAGS='' - fi + ### + ### Convert comma separated branches and tags to newline separated + ### + BRANCHES="$( echo "${{ inputs.branches }}" | sed 's/,/\n/g' )" + 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) + ### + if [ "${{ inputs.num_latest_tags }}" != "0" ]; then + LATEST_TAGS="$( cd .git-ref-matrix-action && git tag --sort=creatordate | tail -${{ inputs.num_latest_tags }} )" + rm -r .git-ref-matrix-action + else + LATEST_TAGS='' + 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 '^$' || 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 - if [ -n "${line}" ]; then - JSON="${JSON},$( printf '"%s"' "${line}" )" + 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 '^$' || 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 + if [ -n "${line}" ]; then + JSON="${JSON},$( printf '"%s"' "${line}" )" + fi + done <<< "${REFS}" + + ### + ### Remove leading comma and encapsulate in square brackets + ### + 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 - done <<< "${REFS}" - - ### - ### Remove leading comma and encapsulate in square brackets - ### - 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}" + echo "::set-output name=has_refs::${HAS_REFS}" - ### - ### Output matrix - ### - echo "matrix=${JSON}" - echo "has_refs=${HAS_REFS}" + ### + ### Output matrix + ### + echo "matrix=${JSON}" + echo "has_refs=${HAS_REFS}" + fi From a0db2afdf1cfb3749e0f676213bb2e43b379682a Mon Sep 17 00:00:00 2001 From: cytopia Date: Tue, 29 Mar 2022 16:36:14 +0200 Subject: [PATCH 2/3] Fix build badges --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1518c1f..ba4f05c 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ [![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-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) -[![test-disabled](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-disabled.yml) +[![test-with](https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test-with-refs.yml/badge.svg)](https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test-with-refs.yml) +[![test-disabled](https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test-disabled.yml/badge.svg)](https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test-disabled.yml) +[![test-without](https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test-without-refs.yml/badge.svg)](https://github.com/cytopia/git-ref-matrix-action/actions/workflows/test-without-refs.yml) This composite action creates a stringified JSON list of git refs to be used as a build matrix. From ae2a8fd56ed8cc38b17bfda5a5d5a33a90de8722 Mon Sep 17 00:00:00 2001 From: cytopia Date: Tue, 29 Mar 2022 16:36:26 +0200 Subject: [PATCH 3/3] Fix test cases --- .github/workflows/test-disabled.yml | 28 ++++--------------------- .github/workflows/test-with-refs.yml | 9 ++++---- .github/workflows/test-without-refs.yml | 28 ++++--------------------- 3 files changed, 12 insertions(+), 53 deletions(-) diff --git a/.github/workflows/test-disabled.yml b/.github/workflows/test-disabled.yml index fc536e6..e5ce263 100644 --- a/.github/workflows/test-disabled.yml +++ b/.github/workflows/test-disabled.yml @@ -9,6 +9,7 @@ jobs: name: get-matrix outputs: matrix: ${{ steps.set-matrix.outputs.matrix }} + has_refs: ${{ steps.set-matrix.outputs.has_refs }} steps: - name: Checkout repository uses: actions/checkout@v3 @@ -29,28 +30,7 @@ jobs: runs-on: ubuntu-latest name: info steps: - - name: Show outputs (raw) + - name: Show outputs 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 + echo 'matrix=${{ needs.get-matrix.outputs.matrix }}' + echo 'has_refs=${{ needs.get-matrix.outputs.has_refs }}' diff --git a/.github/workflows/test-with-refs.yml b/.github/workflows/test-with-refs.yml index 0ab920b..387126a 100644 --- a/.github/workflows/test-with-refs.yml +++ b/.github/workflows/test-with-refs.yml @@ -9,6 +9,7 @@ jobs: name: get-matrix outputs: matrix: ${{ steps.set-matrix.outputs.matrix }} + has_refs: ${{ steps.set-matrix.outputs.has_refs }} steps: - name: Checkout repository uses: actions/checkout@v3 @@ -28,12 +29,10 @@ jobs: runs-on: ubuntu-latest name: info steps: - - name: Show outputs (raw) + - name: Show outputs run: | - echo ${{ needs.get-matrix.outputs.matrix }} - - name: Show outputs (json) - run: | - echo ${{ fromJson(needs.get-matrix.outputs.matrix) }} + echo 'matrix=${{ needs.get-matrix.outputs.matrix }}' + echo 'has_refs=${{ needs.get-matrix.outputs.has_refs }}' build: needs: [get-matrix] diff --git a/.github/workflows/test-without-refs.yml b/.github/workflows/test-without-refs.yml index 05e2ff4..2d317fb 100644 --- a/.github/workflows/test-without-refs.yml +++ b/.github/workflows/test-without-refs.yml @@ -9,6 +9,7 @@ jobs: name: get-matrix outputs: matrix: ${{ steps.set-matrix.outputs.matrix }} + has_refs: ${{ steps.set-matrix.outputs.has_refs }} steps: - name: Checkout repository uses: actions/checkout@v3 @@ -28,28 +29,7 @@ jobs: runs-on: ubuntu-latest name: info steps: - - name: Show outputs (raw) + - name: Show outputs 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 + echo 'matrix=${{ needs.get-matrix.outputs.matrix }}' + echo 'has_refs=${{ needs.get-matrix.outputs.has_refs }}'