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 output flag: has_refs #7

Merged
merged 1 commit 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: test
name: test-with
on:
pull_request:

Expand Down
55 changes: 55 additions & 0 deletions .github/workflows/test-without-refs.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down
37 changes: 29 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -46,18 +49,21 @@ runs:
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)
Expand All @@ -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}"