Skip to content

Commit 1f2faad

Browse files
authored
Instruction to download custom distribution JDK and install (#500)
1 parent 45058d7 commit 1f2faad

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/advanced-usage.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,31 @@ steps:
168168
- run: java -cp java HelloWorldApp
169169
```
170170

171+
If your use-case requires a custom distribution (in the example, alpine-linux is used) or a version that is not provided by setup-java and you want to always install the latest version during runtime, then you can use the following code to auto-download the latest JDK, determine the semver needed for setup-java, and setup-java will take care of the installation and caching on the VM:
172+
173+
```yaml
174+
steps:
175+
- name: fetch latest temurin JDK
176+
id: fetch_latest_jdk
177+
run: |
178+
major_version={{ env.JAVA_VERSION }} # Example 8 or 11 or 17
179+
cd $RUNNER_TEMP
180+
response=$(curl -s "https://api.github.com/repos/adoptium/temurin${major_version}-binaries/releases")
181+
latest_jdk_download_url=$(echo "$response" | jq -r '.[0].assets[] | select(.name | contains("jdk_x64_alpine-linux") and endswith(".tar.gz")) | .browser_download_url')
182+
curl -Ls "$latest_jdk_download_url" -o java_package.tar.gz
183+
latest_jdk_json_url=$(jdk_download_url "$response" | jq -r '.[0].assets[] | select(.name | contains("jdk_x64_alpine-linux") and endswith(".tar.gz.json")) | .browser_download_url')
184+
latest_semver_version=$(curl -sL $latest_jdk_json_url | jq -r 'version.semver')
185+
echo "java_version=$latest_semver_version" >> "$GITHUB_OUTPUT"
186+
187+
- uses: actions/setup-java@v3
188+
with:
189+
distribution: 'jdkfile'
190+
jdkFile: ${{ runner.temp }}/java_package.tar.gz
191+
java-version: {{ steps.fetch_latest_jdk.outputs.java_version }}
192+
architecture: x64
193+
- run: java -cp java HelloWorldApp
194+
```
195+
171196
## Testing against different Java distributions
172197
**NOTE:** The different distributors can provide discrepant list of available versions / supported configurations. Please refer to the official documentation to see the list of supported versions.
173198
```yaml

0 commit comments

Comments
 (0)