Skip to content

Commit 75f0d44

Browse files
committed
Improve CMD task (matrix strategy, Gradle cache)
1 parent 6aef3f2 commit 75f0d44

File tree

2 files changed

+16
-69
lines changed

2 files changed

+16
-69
lines changed

.github/workflows/cmd.yml

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ on:
1515
type: string
1616
required: false
1717
default: ''
18-
scala:
19-
type: string
20-
required: false
21-
default: ""
22-
add-dimensions:
23-
type: string
24-
required: false
25-
default: "{}"
26-
include:
27-
type: string
28-
required: false
29-
default: "[]"
30-
exclude:
31-
type: string
32-
required: false
33-
default: "[]"
3418
cmd:
3519
type: string
3620
required: true
@@ -66,43 +50,15 @@ on:
6650
type: string
6751
required: false
6852
default: ""
53+
gradle-cache-read-only:
54+
type: boolean
55+
required: false
56+
default: false
6957

7058
jobs:
71-
prepare-matrix:
72-
name: Prepare Matrix
73-
if: >-
74-
github.event.repository.fork == false ||
75-
github.event_name != 'schedule' ||
76-
(github.event_name == 'schedule' && github.event.repository.fork == true && inputs.run-scheduled-in-forks == true)
77-
runs-on: ubuntu-24.04
78-
outputs:
79-
matrix: ${{ steps.prepare-matrix.outputs.matrix }}
80-
steps:
81-
- id: prepare-matrix
82-
run: |
83-
add_dimensions=$(echo -n '${{ inputs.add-dimensions }}' | sed 's/^.*{//;s/}.*$//') # Remove leading { and trailing }
84-
# input java/scala | replace whitespaces/commas/quotes by newline | drop empty | quotation | join by comma
85-
java=$(echo -n '${{inputs.java}}' | sed 's/[[:blank:],"]\+/\n/g' | awk NF | sed 's/^..*$/"&"/' | sed ':a; N; $!ba; s/\n/,/g')
86-
scala=$(echo -n '${{inputs.scala}}' | sed 's/[[:blank:],"]\+/\n/g' | awk NF | sed 's/^..*$/"&"/' | sed ':a; N; $!ba; s/\n/,/g')
87-
matrix="{"
88-
matrix+="\"java\": [$java],"
89-
[[ ! -z "$scala" ]] && matrix+="\"scala\": [$scala],"
90-
matrix+="$(echo ${add_dimensions:+$add_dimensions,})"
91-
matrix+="\"include\":$(echo -n '${{ inputs.include }}'),"
92-
matrix+="\"exclude\":$(echo -n '${{ inputs.exclude }}')"
93-
matrix+="}"
94-
# Cleanup JSON (no unnecessary whitespaces, etc.)
95-
matrix=$(jq -n -c "$matrix")
96-
echo $matrix
97-
echo "matrix=$matrix" >> $GITHUB_OUTPUT
9859
cmd:
99-
name: ${{ toJSON(matrix) }}
10060
if: ${{ github.event.repository.fork == false || github.event_name != 'schedule' || (github.event_name == 'schedule' && github.event.repository.fork == true && inputs.run-scheduled-in-forks == true) }}
101-
needs: prepare-matrix
10261
runs-on: ubuntu-24.04
103-
strategy:
104-
# WA: https://github.community/t/reusable-workflow-with-strategy-matrix/205676/6
105-
matrix: ${{fromJson(needs.prepare-matrix.outputs.matrix)}}
10662
steps:
10763
- name: Block deprecated repos in /etc/hosts
10864
run: |
@@ -129,32 +85,33 @@ jobs:
12985

13086
- name: Gradle Cache
13187
uses: burrunan/gradle-cache-action@v3
88+
if: ${{ hashFiles(format('{0}{1}{2}', inputs.gradle-build-root, inputs.gradle-build-root && '/' || '', 'gradle/wrapper/gradle-wrapper.properties')) != '' }}
13289
with:
13390
build-root-directory: ${{ inputs.gradle-build-root }}
13491
# Disable caching of ~/.gradle/caches/build-cache-*
13592
save-local-build-cache: false
13693
# Disable caching of ~/.m2/repository/
13794
save-maven-dependencies-cache: false
95+
read-only: ${{ inputs.gradle-cache-read-only }}
13896

13997
- name: Custom Cache
14098
uses: actions/cache@v4
14199
if: ${{ inputs.cache-key != '' && inputs.cache-path != '' }}
142100
with:
143-
key: ${{ format(inputs.cache-key, matrix.java) }}
101+
key: ${{ format(inputs.cache-key, inputs.java) }}
144102
path: ${{ inputs.cache-path }}
145103

146104
- name: Install Adoptium Temurin OpenJDK
147105
uses: coursier/setup-action@v1
148106
with:
149-
jvm: adoptium:${{ matrix.java }}
107+
jvm: adoptium:${{ inputs.java }}
150108
jvm-index: ${{ inputs.java-index }}
151109

152110
- name: Install sbt
153111
uses: sbt/setup-sbt@v1
154112

155113
- name: Print helpful configs and files and show environment
156114
run: |
157-
echo "Matrix: ${{ toJSON(matrix) }}"
158115
echo "$ cat /etc/sbt/jvmopts"
159116
cat /etc/sbt/jvmopts || true
160117
echo ""
@@ -168,10 +125,6 @@ jobs:
168125
echo "ls -alFhR ~/.cache/coursier | grep play | grep jar"
169126
ls -alFhR ~/.cache/coursier | grep play | grep jar || true
170127
171-
- name: Convert matrix elements to environment variables
172-
run: |
173-
jq -n -r '$in | to_entries|map("MATRIX_\(.key|ascii_upcase)=\(.value|tostring)")|.[]' --argjson in '${{ toJSON(matrix) }}' >> $GITHUB_ENV
174-
175128
- name: Run command
176129
run: ${{ inputs.cmd }}
177130
env:

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ This repository contains a few configurations of GitHub features. For example a
2828

2929
This workflow is used for running any CMD task on matrix of Java versions and other dimensions.
3030

31-
Every matrix dimension will be access by environment variable like `MATRIX_$(uppercase(dimension_name))` (for example `MATRIX_JAVA`).
32-
33-
3431
**Path**: [`.github/workflows/cmd.yml`](.github/workflows/cmd.yml)
3532

3633
**Image**: [Ubuntu 20.04](https://github.com/actions/runner-images/blob/main/images/linux/Ubuntu2004-Readme.md)
@@ -47,10 +44,6 @@ Every matrix dimension will be access by environment variable like `MATRIX_$(upp
4744
| cmd | 2.0.0 | :exclamation: | - | Running command |
4845
| java | 2.0.0 | :heavy_minus_sign: | 17 | _AdoptJDK_ version (space/comma delimited list) |
4946
| java-index | 3.3.1 | :heavy_minus_sign: | '' | URL to JVM index source file |
50-
| scala | 2.0.0 | :heavy_minus_sign: | '' | _Scala_ version (space/comma delimited list) |
51-
| add-dimensions | 2.0.0 | :heavy_minus_sign: | '' | Other matrix dimensions (json object) |
52-
| include | 2.0.0 | :heavy_minus_sign: | [] | Matrix include's (json object array) |
53-
| exclude | 2.0.0 | :heavy_minus_sign: | [] | Matrix exclude's (json object array) |
5447
| cache-key | 2.0.0 | :heavy_minus_sign: | '' | Key of custom cache |
5548
| cache-path | 2.0.0 | :heavy_minus_sign: | '' | Path of custom cache |
5649
| env | 2.0.0 | :heavy_minus_sign: | '' | Custom ENV vars |
@@ -64,16 +57,17 @@ Every matrix dimension will be access by environment variable like `MATRIX_$(upp
6457
**How to use**:
6558

6659
```yaml
60+
strategy:
61+
matrix:
62+
java: [ 17, 21 ]
63+
scala: [ 2.13.x, 3.x ]
64+
color: [ "red", "green"]
6765
uses: playframework/.github/.github/workflows/cmd.yml@v3
6866
with:
69-
java: 17, 21
67+
java: ${{ matrix.java }}
68+
scala: ${{ matrix.scala }}
7069
java-index: https://url/of/your/index.json
71-
scala: 2.12.19, 2.13.13, 3.3.1
72-
add-dimensions: >-
73-
{
74-
"color": [ "red", "green"]
75-
}
76-
cmd: sbt "-Dcustom_var=$CUSTOM_VAR" "-Dcolor=$MATRIX_COLOR" "-Djava=$MATRIX_JAVA" ++$MATRIX_SCALA test
70+
cmd: sbt "-Dcustom_var=$CUSTOM_VAR" "-Dcolor=${{ matrix.color }}" "-Djava=${{ matrix.java }}" ++${{ matrix.scala }} test
7771
env: |
7872
CUSTOM_VAR=value
7973
```

0 commit comments

Comments
 (0)