Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input: disable_refs #8

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/test-disabled.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
name: test-disabled
on:
pull_request:

jobs:
get-matrix:
runs-on: ubuntu-latest
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
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
run: |
echo 'matrix=${{ needs.get-matrix.outputs.matrix }}'
echo 'has_refs=${{ needs.get-matrix.outputs.has_refs }}'
9 changes: 4 additions & 5 deletions .github/workflows/test-with-refs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
28 changes: 4 additions & 24 deletions .github/workflows/test-without-refs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}'
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +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-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.

Expand All @@ -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
Expand Down
160 changes: 87 additions & 73 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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