Skip to content

Commit 83aed64

Browse files
authored
Merge pull request #7 from cytopia/release-0.1.6
Add output flag: has_refs
2 parents 37bb5bb + 29fb2c7 commit 83aed64

File tree

4 files changed

+91
-13
lines changed

4 files changed

+91
-13
lines changed

.github/workflows/test.yml .github/workflows/test-with-refs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
name: test
2+
name: test-with
33
on:
44
pull_request:
55

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
name: test-without
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
get-matrix:
8+
runs-on: ubuntu-latest
9+
name: get-matrix
10+
outputs:
11+
matrix: ${{ steps.set-matrix.outputs.matrix }}
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v3
15+
with:
16+
fetch-depth: 0
17+
- name: Fetch matrix
18+
id: set-matrix
19+
uses: ./
20+
with:
21+
repository_default_branch: master
22+
branches: ""
23+
tags: ""
24+
num_latest_tags: 0
25+
26+
info:
27+
needs: [get-matrix]
28+
runs-on: ubuntu-latest
29+
name: info
30+
steps:
31+
- name: Show outputs (raw)
32+
run: |
33+
echo ${{ needs.get-matrix.outputs.matrix }}
34+
- name: Show outputs (json)
35+
run: |
36+
echo ${{ fromJson(needs.get-matrix.outputs.matrix) }}
37+
38+
build:
39+
needs: [get-matrix]
40+
runs-on: ubuntu-latest
41+
name: build
42+
strategy:
43+
fail-fast: false
44+
matrix:
45+
refs:
46+
- ${{ fromJson(needs.get-matrix.outputs.matrix) }}
47+
steps:
48+
- name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})"
49+
uses: actions/checkout@v3
50+
with:
51+
fetch-depth: 0
52+
ref: ${{ matrix.refs }}
53+
- name: Show git
54+
run: |
55+
git log | head -20

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
[![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)
44
[![GitHub marketplace](https://img.shields.io/badge/marketplace-git--ref--matrix--action-blue?logo=github)](https://github.com/marketplace/actions/git-ref-matrix-action)
55
[![](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")
6-
[![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)
6+
[![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)
7+
[![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)
78

89
This composite action creates a stringified JSON list of git refs to be used as a build matrix.
910

@@ -22,9 +23,10 @@ The following inputs can be used to alter the Docker tag name determination:
2223

2324
## :arrow_backward: Outputs
2425

25-
| Output | Description |
26-
|--------------|---------------------------------------------------------------|
27-
| `matrix` | Stringified JSON build matrix. Example: `["master","v0.1.0"]` |
26+
| Output | Description |
27+
|--------------|-----------------------------------------------------------------------------|
28+
| `matrix` | Stringified JSON build matrix. Example: `["master","v0.1.0"]` |
29+
| `has_refs` | Flag (0 or 1) that tells if we have matrix refs or not. Example: `0` or `1` |
2830

2931

3032
## :computer: Usage

action.yml

+29-8
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ inputs:
2727

2828
outputs:
2929
matrix:
30-
description: "Stringified JSON build matrix for defined git refs."
30+
description: "Stringified JSON build matrix for defined git refs. (list of strings)"
3131
value: ${{ steps.set-matrix.outputs.matrix }}
32+
has_refs:
33+
description: "Flag (0 or 1) that tells if we have matrix (list not empty) refs or not (list empty)."
34+
value: ${{ steps.set-matrix.outputs.has_refs }}
3235

3336
runs:
3437
using: "composite"
@@ -46,18 +49,21 @@ runs:
4649
id: set-matrix
4750
shell: bash
4851
run: |
49-
5052
###
5153
### Convert comma separated branches and tags to newline separated
5254
###
5355
BRANCHES="$( echo "${{ inputs.branches }}" | sed 's/,/\n/g' )"
5456
TAGS="$( echo "${{ inputs.tags }}" | sed 's/,/\n/g' )"
5557
5658
echo "BRANCHES:"
59+
echo "-------------------------"
5760
echo "${BRANCHES}"
61+
echo
5862
5963
echo "TAGS:"
64+
echo "-------------------------"
6065
echo "${TAGS}"
66+
echo
6167
6268
###
6369
### Get x number of latest tags of this repository (newline separated)
@@ -70,37 +76,52 @@ runs:
7076
fi
7177
7278
echo "LATEST_TAGS:"
79+
echo "-------------------------"
7380
echo "${LATEST_TAGS}"
74-
81+
echo
7582
7683
###
7784
### All newline separated refs (and make unique in case of duplicated tags)
7885
###
79-
REFS="$( printf "%s\n%s\n%s\n" "${BRANCHES}" "${TAGS}" "${LATEST_TAGS}" | grep -Ev '^$' | sort -u )"
86+
REFS="$( printf "%s\n%s\n%s\n" "${BRANCHES}" "${TAGS}" "${LATEST_TAGS}" | grep -Ev '^$' || true | sort -u )"
8087
8188
echo "REFS:"
89+
echo "-------------------------"
8290
echo "${REFS}"
91+
echo
8392
8493
###
8594
### Create element double-quoted and comma separated string (has leading comma)
8695
###
8796
JSON=''
8897
while IFS= read -r line; do
89-
JSON="${JSON},$( printf '"%s"' "${line}" )"
98+
if [ -n "${line}" ]; then
99+
JSON="${JSON},$( printf '"%s"' "${line}" )"
100+
fi
90101
done <<< "${REFS}"
91102
92103
###
93104
### Remove leading comma and encapsulate in square brackets
94105
###
95-
JSON="$( printf '[%s]\n' "${JSON#,}" )"
106+
JSON="$( printf '[%s]' "${JSON#,}" )"
96107
97108
###
98109
### Set final output for 'matrix'
99110
###
100111
echo "::set-output name=matrix::${JSON}"
101112
113+
###
114+
### Set 'has_refs'
115+
###
116+
if [ "${JSON}" = "[]" ]; then
117+
HAS_REFS="0"
118+
else
119+
HAS_REFS="1"
120+
fi
121+
echo "::set-output name=has_refs::${HAS_REFS}"
122+
102123
###
103124
### Output matrix
104125
###
105-
echo "JSON=${JSON}"
106-
echo JSON=${JSON}
126+
echo "matrix=${JSON}"
127+
echo "has_refs=${HAS_REFS}"

0 commit comments

Comments
 (0)