Skip to content

Commit 65a01d1

Browse files
committed
feat(java-build): add reusable workflow
1 parent 298adeb commit 65a01d1

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed

.github/workflows/java-build.yml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
name: Build and Test Java
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
java-version:
7+
description: 'Java version to use'
8+
default: '17'
9+
type: string
10+
java-distribution:
11+
description: 'Java distribution to use (temurin, zulu, adopt, etc.)'
12+
default: 'temurin'
13+
type: string
14+
build-tool:
15+
description: 'Build tool to use (gradle or maven)'
16+
default: 'gradle'
17+
type: string
18+
gradle-version:
19+
description: 'Gradle version to use (only applicable if build-tool is gradle)'
20+
default: 'wrapper'
21+
type: string
22+
gradle-build-task:
23+
description: 'Gradle build task to run'
24+
default: 'build'
25+
type: string
26+
gradle-build-file:
27+
description: 'Gradle build file to use'
28+
default: ''
29+
type: string
30+
maven-version:
31+
description: 'Maven version to use (only applicable if build-tool is maven)'
32+
default: '3.9.5'
33+
type: string
34+
maven-goals:
35+
description: 'Maven goals to run'
36+
default: 'clean package'
37+
type: string
38+
maven-args:
39+
description: 'Additional Maven arguments'
40+
default: ''
41+
type: string
42+
working-directory:
43+
description: 'Working directory where the build commands will be run'
44+
default: '.'
45+
type: string
46+
upload-artifacts:
47+
description: 'Whether to upload build artifacts'
48+
default: true
49+
type: boolean
50+
artifacts-name:
51+
description: 'Name of the artifacts to upload'
52+
default: 'build-artifacts'
53+
type: string
54+
artifacts-path:
55+
description: 'Path to the artifacts to upload (relative to working-directory)'
56+
default: ''
57+
type: string
58+
59+
jobs:
60+
build:
61+
runs-on: ubuntu-latest
62+
steps:
63+
- name: Checkout Repository
64+
uses: actions/checkout@v4
65+
66+
- name: Set up JDK
67+
uses: actions/setup-java@v4
68+
with:
69+
java-version: ${{ inputs.java-version }}
70+
distribution: ${{ inputs.java-distribution }}
71+
architecture: x64
72+
73+
# Gradle build
74+
- name: Setup Gradle
75+
if: ${{ inputs.build-tool == 'gradle' }}
76+
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
77+
with:
78+
gradle-version: ${{ inputs.gradle-version }}
79+
80+
- name: Build with Gradle
81+
if: ${{ inputs.build-tool == 'gradle' }}
82+
working-directory: ${{ inputs.working-directory }}
83+
run: |
84+
if [ -n "${{ inputs.gradle-build-file }}" ]; then
85+
./gradlew -b ${{ inputs.gradle-build-file }} ${{ inputs.gradle-build-task }}
86+
else
87+
./gradlew ${{ inputs.gradle-build-task }}
88+
fi
89+
90+
# Maven build
91+
- name: Setup Maven
92+
if: ${{ inputs.build-tool == 'maven' }}
93+
uses: stCarolas/setup-maven@v5
94+
with:
95+
maven-version: ${{ inputs.maven-version }}
96+
97+
- name: Build with Maven
98+
if: ${{ inputs.build-tool == 'maven' }}
99+
working-directory: ${{ inputs.working-directory }}
100+
run: mvn ${{ inputs.maven-goals }} ${{ inputs.maven-args }}
101+
102+
# Determine artifacts path based on build tool if not explicitly provided
103+
- name: Set default artifacts path
104+
if: ${{ inputs.upload-artifacts && inputs.artifacts-path == '' }}
105+
id: set-artifacts-path
106+
run: |
107+
if [ "${{ inputs.build-tool }}" = "gradle" ]; then
108+
echo "path=build/libs" >> $GITHUB_OUTPUT
109+
else
110+
echo "path=target" >> $GITHUB_OUTPUT
111+
fi
112+
113+
# Upload artifacts
114+
- name: Upload build artifacts
115+
if: ${{ inputs.upload-artifacts }}
116+
uses: actions/upload-artifact@v4
117+
with:
118+
name: ${{ inputs.artifacts-name }}
119+
path: ${{ inputs.working-directory }}/${{ inputs.artifacts-path != '' && inputs.artifacts-path || steps.set-artifacts-path.outputs.path }}

java-build/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Java Build and Test
2+
3+
A reusable GitHub Actions workflow for building and testing Java applications with Gradle or Maven.
4+
5+
## Inputs
6+
7+
### `java-version`
8+
9+
**Optional** The Java version to use. Default: `'17'`.
10+
11+
### `java-distribution`
12+
13+
**Optional** The Java distribution to use (temurin, zulu, adopt, etc.). Default: `'temurin'`.
14+
15+
### `build-tool`
16+
17+
**Optional** The build tool to use (gradle or maven). Default: `'gradle'`.
18+
19+
### `gradle-version`
20+
21+
**Optional** The Gradle version to use (only applicable if build-tool is gradle). Default: `'wrapper'` (uses the Gradle Wrapper).
22+
23+
### `gradle-build-task`
24+
25+
**Optional** The Gradle build task to run. Default: `'build'`.
26+
27+
### `gradle-build-file`
28+
29+
**Optional** The Gradle build file to use. Default: `''` (uses the default build.gradle file).
30+
31+
### `maven-version`
32+
33+
**Optional** The Maven version to use (only applicable if build-tool is maven). Default: `'3.9.5'`.
34+
35+
### `maven-goals`
36+
37+
**Optional** The Maven goals to run. Default: `'clean package'`.
38+
39+
### `maven-args`
40+
41+
**Optional** Additional Maven arguments. Default: `''`.
42+
43+
### `working-directory`
44+
45+
**Optional** The working directory where the build commands will be run. Default: `'.'`.
46+
47+
### `upload-artifacts`
48+
49+
**Optional** Whether to upload build artifacts. Default: `true`.
50+
51+
### `artifacts-name`
52+
53+
**Optional** The name of the artifacts to upload. Default: `'build-artifacts'`.
54+
55+
### `artifacts-path`
56+
57+
**Optional** The path to the artifacts to upload (relative to working-directory). Default: `''` (automatically determined based on build tool).
58+
59+
## Example usage
60+
61+
### Basic usage with Gradle (default)
62+
63+
```yaml
64+
jobs:
65+
build:
66+
uses: iExecBlockchainComputing/github-actions-workflows/[email protected]
67+
```
68+
69+
### Using with Maven
70+
71+
```yaml
72+
jobs:
73+
build:
74+
uses: iExecBlockchainComputing/github-actions-workflows/[email protected]
75+
with:
76+
build-tool: 'maven'
77+
```
78+
79+
### Custom Java version and distribution
80+
81+
```yaml
82+
jobs:
83+
build:
84+
uses: iExecBlockchainComputing/github-actions-workflows/[email protected]
85+
with:
86+
java-version: '11'
87+
java-distribution: 'zulu'
88+
```
89+
90+
### Custom Gradle configuration
91+
92+
```yaml
93+
jobs:
94+
build:
95+
uses: iExecBlockchainComputing/github-actions-workflows/[email protected]
96+
with:
97+
gradle-build-task: 'test'
98+
gradle-build-file: 'custom.gradle'
99+
```
100+
101+
### Custom Maven configuration
102+
103+
```yaml
104+
jobs:
105+
build:
106+
uses: iExecBlockchainComputing/github-actions-workflows/[email protected]
107+
with:
108+
build-tool: 'maven'
109+
maven-goals: 'clean test'
110+
maven-args: '-DskipTests=false'
111+
```
112+
113+
### Custom artifacts configuration
114+
115+
```yaml
116+
jobs:
117+
build:
118+
uses: iExecBlockchainComputing/github-actions-workflows/[email protected]
119+
with:
120+
upload-artifacts: true
121+
artifacts-name: 'my-java-app'
122+
artifacts-path: 'custom/path/to/artifacts'
123+
```

0 commit comments

Comments
 (0)