Skip to content

Commit 498de73

Browse files
committed
separate jdk logic into its own reusable action
1 parent a9687ed commit 498de73

File tree

3 files changed

+449
-114
lines changed

3 files changed

+449
-114
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
# Determine Build Matrix Action
2+
3+
A composite GitHub Action that determines branches and JDK versions for Spring Cloud project builds based on centralized configuration.
4+
5+
## Description
6+
7+
This action reads project configuration from `config/projects.json` in the spring-cloud-github-actions repository and generates a build matrix suitable for GitHub Actions matrix strategy. It supports:
8+
9+
- OSS and commercial repository variants
10+
- Per-project and default configurations
11+
- Scheduled builds (multiple branches) vs single-branch builds
12+
- JDK version mapping per branch
13+
- Detection of JDK 8 availability for deployment logic
14+
15+
## Requirements
16+
17+
- Runs on: `ubuntu-latest` (or any runner with `jq` available)
18+
- Requires: `jq` command-line JSON processor (pre-installed on GitHub-hosted runners)
19+
20+
## Inputs
21+
22+
| Input | Description | Required | Default |
23+
|-------|-------------|----------|---------|
24+
| `repository` | Repository name in format `org/repo-name` | Yes | - |
25+
| `event-name` | GitHub event name (schedule, push, workflow_dispatch, etc.) | Yes | - |
26+
| `ref-name` | Git ref name (branch name) | Yes | - |
27+
| `branch` | Optional branch override for single-branch builds | No | `''` |
28+
| `config-ref` | Git ref for spring-cloud-github-actions config | No | `main` |
29+
30+
## Outputs
31+
32+
| Output | Description | Example |
33+
|--------|-------------|---------|
34+
| `matrix` | JSON matrix for GitHub Actions matrix strategy | `[{"branch":"main","java-version":"17","has-jdk8":false}]` |
35+
| `branches` | Comma-separated list of branches being built | `main,3.3.x,3.2.x` |
36+
| `branch-jdk-mapping` | JSON object mapping branches to JDK versions | `{"main":["17","21","25"],"3.3.x":["17","21"]}` |
37+
38+
## Usage
39+
40+
### Basic Usage
41+
42+
```yaml
43+
jobs:
44+
setup:
45+
runs-on: ubuntu-latest
46+
outputs:
47+
matrix: ${{ steps.determine-matrix.outputs.matrix }}
48+
steps:
49+
- name: Checkout repository
50+
uses: actions/checkout@v4
51+
52+
- name: Determine build matrix
53+
id: determine-matrix
54+
uses: ./.github/actions/determine-matrix
55+
with:
56+
repository: ${{ github.repository }}
57+
event-name: ${{ github.event_name }}
58+
ref-name: ${{ github.ref_name }}
59+
branch: ${{ inputs.branch }}
60+
61+
build:
62+
needs: setup
63+
strategy:
64+
matrix:
65+
include: ${{ fromJson(needs.setup.outputs.matrix) }}
66+
runs-on: ubuntu-latest
67+
steps:
68+
- uses: actions/checkout@v4
69+
with:
70+
ref: ${{ matrix.branch }}
71+
- uses: actions/setup-java@v4
72+
with:
73+
java-version: ${{ matrix.java-version }}
74+
# ... build steps ...
75+
```
76+
77+
### Usage from Other Repositories
78+
79+
Other Spring Cloud repositories can reference this action:
80+
81+
```yaml
82+
- name: Determine build matrix
83+
id: determine-matrix
84+
uses: spring-cloud/spring-cloud-github-actions/.github/actions/determine-matrix@main
85+
with:
86+
repository: ${{ github.repository }}
87+
event-name: ${{ github.event_name }}
88+
ref-name: ${{ github.ref_name }}
89+
branch: ${{ inputs.branch }}
90+
```
91+
92+
### Using Additional Outputs
93+
94+
```yaml
95+
- name: Determine build matrix
96+
id: determine-matrix
97+
uses: ./.github/actions/determine-matrix
98+
with:
99+
repository: ${{ github.repository }}
100+
event-name: ${{ github.event_name }}
101+
ref-name: ${{ github.ref_name }}
102+
branch: ${{ inputs.branch }}
103+
104+
- name: Display build info
105+
run: |
106+
echo "Building branches: ${{ steps.determine-matrix.outputs.branches }}"
107+
echo "Branch-JDK mapping: ${{ steps.determine-matrix.outputs.branch-jdk-mapping }}"
108+
```
109+
110+
### Testing with Feature Branch Config
111+
112+
```yaml
113+
- name: Determine build matrix
114+
id: determine-matrix
115+
uses: ./.github/actions/determine-matrix
116+
with:
117+
repository: ${{ github.repository }}
118+
event-name: ${{ github.event_name }}
119+
ref-name: ${{ github.ref_name }}
120+
branch: ${{ inputs.branch }}
121+
config-ref: 'feature/new-config-structure' # Use feature branch config
122+
```
123+
124+
## Configuration
125+
126+
The action reads configuration from `config/projects.json` in the spring-cloud-github-actions repository. The configuration structure is:
127+
128+
```json
129+
{
130+
"project-name": {
131+
"oss": {
132+
"branches": {
133+
"scheduled": ["main", "3.3.x"],
134+
"default": ["main"]
135+
},
136+
"jdkVersions": {
137+
"main": ["17", "21", "25"],
138+
"3.3.x": ["17", "21"],
139+
"default": ["17", "21", "25"]
140+
}
141+
},
142+
"commercial": {
143+
"branches": {
144+
"scheduled": ["3.1.x"],
145+
"default": ["3.1.x"]
146+
},
147+
"jdkVersions": {
148+
"3.1.x": ["8", "11", "17"],
149+
"default": ["17", "21"]
150+
}
151+
}
152+
},
153+
"defaults": {
154+
"oss": { /* fallback config */ },
155+
"commercial": { /* fallback config */ }
156+
}
157+
}
158+
```
159+
160+
## How It Works
161+
162+
1. **Repository Detection**: Extracts repository name and detects commercial vs OSS variant
163+
2. **Config Loading**: Checks out spring-cloud-github-actions repo and reads `config/projects.json`
164+
3. **Branch Determination**:
165+
- For scheduled runs: builds multiple branches from config
166+
- For other events: builds single branch (from input or ref-name)
167+
4. **JDK Mapping**: Maps each branch to its configured JDK versions
168+
5. **Matrix Generation**: Creates matrix entries for each branch + JDK combination
169+
6. **Additional Outputs**: Generates branches list and branch-jdk-mapping
170+
171+
## Matrix Entry Format
172+
173+
Each matrix entry contains:
174+
- `branch`: Branch name to build
175+
- `java-version`: JDK version to use
176+
- `has-jdk8`: Boolean indicating if this branch configuration includes JDK 8
177+
178+
The `has-jdk8` flag is used downstream to determine which builds should deploy artifacts and generate documentation (typically JDK 8 if available, otherwise JDK 17).
179+
180+
## Example Complete Workflow
181+
182+
```yaml
183+
name: Deploy
184+
185+
on:
186+
push:
187+
branches: [main, '*.x']
188+
schedule:
189+
- cron: '0 2 * * *'
190+
workflow_dispatch:
191+
inputs:
192+
branch:
193+
description: 'Branch to build'
194+
required: false
195+
196+
jobs:
197+
setup:
198+
runs-on: ubuntu-latest
199+
outputs:
200+
matrix: ${{ steps.determine-matrix.outputs.matrix }}
201+
branches: ${{ steps.determine-matrix.outputs.branches }}
202+
steps:
203+
- name: Checkout repository
204+
uses: actions/checkout@v4
205+
206+
- name: Determine build matrix
207+
id: determine-matrix
208+
uses: spring-cloud/spring-cloud-github-actions/.github/actions/determine-matrix@main
209+
with:
210+
repository: ${{ github.repository }}
211+
event-name: ${{ github.event_name }}
212+
ref-name: ${{ github.ref_name }}
213+
branch: ${{ inputs.branch }}
214+
215+
build:
216+
name: Build ${{ matrix.branch }} (JDK ${{ matrix.java-version }})
217+
needs: setup
218+
strategy:
219+
fail-fast: false
220+
matrix:
221+
include: ${{ fromJson(needs.setup.outputs.matrix) }}
222+
runs-on: ubuntu-latest
223+
steps:
224+
- uses: actions/checkout@v4
225+
with:
226+
ref: ${{ matrix.branch }}
227+
228+
- uses: actions/setup-java@v4
229+
with:
230+
java-version: ${{ matrix.java-version }}
231+
distribution: 'temurin'
232+
233+
- name: Build
234+
run: ./mvnw clean install -B
235+
236+
- name: Deploy (only on specific JDK)
237+
if: ${{ env.SHOULD_DEPLOY == 'true' }}
238+
run: ./mvnw deploy -B
239+
```
240+
241+
## License
242+
243+
Apache License 2.0

0 commit comments

Comments
 (0)