Skip to content

Commit 7d219e5

Browse files
authored
Add java-package parameter to action, support jre, jdk, and jdk+fx (#1)
* Add java-package parameter to action, support jre, jdk, and jdk+fx (#1) * Update tests to use 'jdk', 'jre', and 'jdk+fx' javaPackage parameters * Match extension only at end of line * Update README.md * Update workflow to use 'node-version' instead of deprecated 'version'
1 parent 204b974 commit 7d219e5

File tree

8 files changed

+112
-42
lines changed

8 files changed

+112
-42
lines changed

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Set Node.js 10.x
1515
uses: actions/setup-node@master
1616
with:
17-
version: 10.x
17+
node-version: 10.x
1818

1919
- name: npm install
2020
run: npm install

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
This action sets up a java environment for use in actions by:
88

9-
- optionally downloading and caching a version of java by version and adding to PATH. Downloads from [Azul's Zulu distribution](http://static.azul.com/zulu/bin/).
9+
- optionally downloading and caching a requested version of java by version and adding to PATH. Default downloads are populated from the [Zulu Community distribution of OpenJDK](http://static.azul.com/zulu/bin/)
1010
- registering problem matchers for error output
1111

1212
# Usage
@@ -19,7 +19,8 @@ steps:
1919
- uses: actions/checkout@latest
2020
- uses: actions/setup-java@v1
2121
with:
22-
java-version: '9.0.4' // The JDK version to make available on the path. Takes a whole or semver Jdk version, or 1.x syntax (e.g. 1.8 => Jdk 8.x)
22+
java-version: '11.0.4' // The Java version to make available on the path. Takes a whole or semver Java version, or 1.x syntax (e.g. 1.8 => Jdk 8.x)
23+
java-package: jdk // (jre, jdk, or jdk+fx) - defaults to jdk
2324
architecture: x64 // (x64 or x86) - defaults to x64
2425
- run: java -cp java HelloWorldApp
2526
```
@@ -32,7 +33,7 @@ steps:
3233
with:
3334
java-version: '4.0.0'
3435
architecture: x64
35-
jdkFile: <path to jdkFile> # Optional - jdkFile to install java from. Useful for versions not supported by Azul
36+
jdkFile: <path to jdkFile> # Optional - jdkFile to install java from. Useful for versions not found on Zulu Community CDN
3637
- run: java -cp java HelloWorldApp
3738
```
3839
@@ -43,15 +44,15 @@ jobs:
4344
runs-on: ubuntu-16.04
4445
strategy:
4546
matrix:
46-
java: [ '1.6', '9.0.x', '12.0.2' ]
47+
# test against latest update of each major Java version, as well as specific updates of LTS versions:
48+
java: [ 1.6, 6.0.83, 7, 7.0.181, 8, 8.0.192, 9.0,x, 10, 11.0.x, 11.0.3, 12, 13 ]
4749
name: Java ${{ matrix.java }} sample
4850
steps:
4951
- uses: actions/checkout@master
5052
- name: Setup java
5153
uses: actions/setup-java@v1
5254
with:
5355
java-version: ${{ matrix.java }}
54-
architecture: x64
5556
- run: java -cp java HelloWorldApp
5657
```
5758

__tests__/installer.test.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ describe('installer tests', () => {
5252
}, 100000);
5353

5454
it('Installs version of Java from jdkFile if no matching version is installed', async () => {
55-
await installer.getJava('12', 'x64', javaFilePath);
56-
const JavaDir = path.join(toolDir, 'Java', '12.0.0', 'x64');
55+
await installer.getJava('12', 'x64', javaFilePath, 'jdk');
56+
const JavaDir = path.join(toolDir, 'jdk', '12.0.0', 'x64');
5757

5858
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
5959
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
@@ -62,67 +62,94 @@ describe('installer tests', () => {
6262
it('Throws if invalid directory to jdk', async () => {
6363
let thrown = false;
6464
try {
65-
await installer.getJava('1000', 'x64', 'bad path');
65+
await installer.getJava('1000', 'x64', 'bad path', 'jdk');
6666
} catch {
6767
thrown = true;
6868
}
6969
expect(thrown).toBe(true);
7070
});
7171

7272
it('Downloads java if no file given', async () => {
73-
await installer.getJava('8.0.102', 'x64', '');
74-
const JavaDir = path.join(toolDir, 'Java', '8.0.102', 'x64');
73+
await installer.getJava('8.0.102', 'x64', '', 'jdk');
74+
const JavaDir = path.join(toolDir, 'jdk', '8.0.102', 'x64');
7575

7676
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
7777
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
7878
}, 100000);
7979

8080
it('Downloads java with 1.x syntax', async () => {
81-
await installer.getJava('1.10', 'x64', '');
82-
const JavaDir = path.join(toolDir, 'Java', '10.0.2', 'x64');
81+
await installer.getJava('1.10', 'x64', '', 'jdk');
82+
const JavaDir = path.join(toolDir, 'jdk', '10.0.2', 'x64');
8383

8484
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
8585
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
8686
}, 100000);
8787

8888
it('Downloads java with normal semver syntax', async () => {
89-
await installer.getJava('9.0.x', 'x64', '');
90-
const JavaDir = path.join(toolDir, 'Java', '9.0.7', 'x64');
89+
await installer.getJava('9.0.x', 'x64', '', 'jdk');
90+
const JavaDir = path.join(toolDir, 'jdk', '9.0.7', 'x64');
9191

9292
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
9393
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
9494
}, 100000);
9595

96+
it('Downloads java if package is jre', async () => {
97+
await installer.getJava('8.0.222', 'x64', '', 'jre');
98+
const JavaDir = path.join(toolDir, 'jre', '8.0.222', 'x64');
99+
100+
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
101+
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
102+
}, 100000);
103+
104+
it('Downloads java if package is jdk+fx', async () => {
105+
await installer.getJava('8.0.222', 'x64', '', 'jdk+fx');
106+
const JavaDir = path.join(toolDir, 'jdk+fx', '8.0.222', 'x64');
107+
108+
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
109+
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
110+
}, 100000);
111+
112+
it('Throws if invalid java package is specified', async () => {
113+
let thrown = false;
114+
try {
115+
await installer.getJava('8.0.222', 'x64', '', 'bad jdk');
116+
} catch {
117+
thrown = true;
118+
}
119+
expect(thrown).toBe(true);
120+
});
121+
96122
it('Throws if invalid directory to jdk', async () => {
97123
let thrown = false;
98124
try {
99-
await installer.getJava('1000', 'x64', 'bad path');
125+
await installer.getJava('1000', 'x64', 'bad path', 'jdk');
100126
} catch {
101127
thrown = true;
102128
}
103129
expect(thrown).toBe(true);
104130
});
105131

106132
it('Uses version of Java installed in cache', async () => {
107-
const JavaDir: string = path.join(toolDir, 'Java', '250.0.0', 'x64');
133+
const JavaDir: string = path.join(toolDir, 'jdk', '250.0.0', 'x64');
108134
await io.mkdirP(JavaDir);
109135
fs.writeFileSync(`${JavaDir}.complete`, 'hello');
110136
// This will throw if it doesn't find it in the cache (because no such version exists)
111137
await installer.getJava(
112138
'250',
113139
'x64',
114-
'path shouldnt matter, found in cache'
140+
'path shouldnt matter, found in cache',
141+
'jdk'
115142
);
116143
return;
117144
});
118145

119146
it('Doesnt use version of Java that was only partially installed in cache', async () => {
120-
const JavaDir: string = path.join(toolDir, 'Java', '251.0.0', 'x64');
147+
const JavaDir: string = path.join(toolDir, 'jdk', '251.0.0', 'x64');
121148
await io.mkdirP(JavaDir);
122149
let thrown = false;
123150
try {
124151
// This will throw if it doesn't find it in the cache (because no such version exists)
125-
await installer.getJava('251', 'x64', 'bad path');
152+
await installer.getJava('251', 'x64', 'bad path', 'jdk');
126153
} catch {
127154
thrown = true;
128155
}

action.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ description: 'Setup your runner with Java'
33
author: 'GitHub'
44
inputs:
55
java-version:
6-
description: 'The JDK version to make available on the path. Takes a whole or semver Jdk version, or 1.x syntax (e.g. 1.8 => Jdk 8.x)'
6+
description: 'The Java version to make available on the path. Takes a whole or semver Java version, or 1.x syntax (e.g. 1.8 => Jdk 8.x)'
77
required: true
8+
java-package:
9+
description: 'The package type (jre, jdk, jdk+fx)'
10+
required: true
11+
default: 'jdk'
812
architecture:
9-
description: 'The architecture (x86, x64) of the JDK.'
13+
description: 'The architecture (x86, x64) of the package.'
1014
required: true
1115
default: 'x64'
1216
jdkFile:

lib/installer.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ if (!tempDirectory) {
4141
}
4242
tempDirectory = path.join(baseLocation, 'actions', 'temp');
4343
}
44-
function getJava(version, arch, jdkFile) {
44+
function getJava(version, arch, jdkFile, javaPackage) {
4545
return __awaiter(this, void 0, void 0, function* () {
46-
let toolPath = tc.find('Java', version);
46+
let toolPath = tc.find(javaPackage, version);
4747
if (toolPath) {
4848
core.debug(`Tool found in cache ${toolPath}`);
4949
}
@@ -54,7 +54,7 @@ function getJava(version, arch, jdkFile) {
5454
let http = new httpm.HttpClient('setup-java');
5555
let contents = yield (yield http.get('https://static.azul.com/zulu/bin/')).readBody();
5656
let refs = contents.match(/<a href.*\">/gi) || [];
57-
const downloadInfo = getDownloadInfo(refs, version);
57+
const downloadInfo = getDownloadInfo(refs, version, javaPackage);
5858
jdkFile = yield tc.downloadTool(downloadInfo.url);
5959
version = downloadInfo.version;
6060
compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz';
@@ -66,7 +66,7 @@ function getJava(version, arch, jdkFile) {
6666
let tempDir = path.join(tempDirectory, 'temp_' + Math.floor(Math.random() * 2000000000));
6767
const jdkDir = yield unzipJavaDownload(jdkFile, compressedFileExtension, tempDir);
6868
core.debug(`jdk extracted to ${jdkDir}`);
69-
toolPath = yield tc.cacheDir(jdkDir, 'Java', getCacheVersionString(version), arch);
69+
toolPath = yield tc.cacheDir(jdkDir, javaPackage, getCacheVersionString(version), arch);
7070
}
7171
let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch;
7272
core.exportVariable('JAVA_HOME', toolPath);
@@ -162,7 +162,7 @@ function unzipJavaDownload(repoRoot, fileEnding, destinationFolder, extension) {
162162
}
163163
});
164164
}
165-
function getDownloadInfo(refs, version) {
165+
function getDownloadInfo(refs, version, javaPackage) {
166166
version = normalizeVersion(version);
167167
let extension = '';
168168
if (IS_WINDOWS) {
@@ -176,22 +176,39 @@ function getDownloadInfo(refs, version) {
176176
extension = `-linux_x64.tar.gz`;
177177
}
178178
}
179+
let pkgRegexp = new RegExp('');
180+
let pkgTypeLength = 0;
181+
if (javaPackage === 'jdk') {
182+
pkgRegexp = /jdk.*-/gi;
183+
pkgTypeLength = 'jdk'.length;
184+
}
185+
else if (javaPackage == 'jre') {
186+
pkgRegexp = /jre.*-/gi;
187+
pkgTypeLength = 'jre'.length;
188+
}
189+
else if (javaPackage == 'jdk+fx') {
190+
pkgRegexp = /fx-jdk.*-/gi;
191+
pkgTypeLength = 'fx-jdk'.length;
192+
}
193+
else {
194+
throw new Error(`package argument ${javaPackage} is not in [jdk | jre | jdk+fx]`);
195+
}
179196
// Maps version to url
180197
let versionMap = new Map();
181198
// Filter by platform
182199
refs.forEach(ref => {
183-
if (ref.indexOf(extension) < 0) {
200+
if (!ref.endsWith(extension + '">')) {
184201
return;
185202
}
186203
// If we haven't returned, means we're looking at the correct platform
187-
let versions = ref.match(/jdk.*-/gi) || [];
204+
let versions = ref.match(pkgRegexp) || [];
188205
if (versions.length > 1) {
189206
throw new Error(`Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}`);
190207
}
191208
if (versions.length == 0) {
192209
return;
193210
}
194-
const refVersion = versions[0].slice('jdk'.length, versions[0].length - 1);
211+
const refVersion = versions[0].slice(pkgTypeLength, versions[0].length - 1);
195212
if (semver.satisfies(refVersion, version)) {
196213
versionMap.set(refVersion, 'https://static.azul.com/zulu/bin/' +
197214
ref.slice('<a href="'.length, ref.length - '">'.length));
@@ -209,7 +226,7 @@ function getDownloadInfo(refs, version) {
209226
}
210227
}
211228
if (curUrl == '') {
212-
throw new Error(`No valid download found for version ${version}. Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument`);
229+
throw new Error(`No valid download found for version ${version} and package ${javaPackage}. Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument`);
213230
}
214231
return { version: curVersion, url: curUrl };
215232
}

lib/setup-java.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ function run() {
2626
version = core.getInput('java-version', { required: true });
2727
}
2828
const arch = core.getInput('architecture', { required: true });
29+
const javaPackage = core.getInput('java-package', { required: true });
2930
const jdkFile = core.getInput('jdkFile', { required: false }) || '';
30-
yield installer.getJava(version, arch, jdkFile);
31+
yield installer.getJava(version, arch, jdkFile, javaPackage);
3132
const matchersPath = path.join(__dirname, '..', '.github');
3233
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
3334
}

src/installer.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ if (!tempDirectory) {
2929
export async function getJava(
3030
version: string,
3131
arch: string,
32-
jdkFile: string
32+
jdkFile: string,
33+
javaPackage: string
3334
): Promise<void> {
34-
let toolPath = tc.find('Java', version);
35+
let toolPath = tc.find(javaPackage, version);
3536

3637
if (toolPath) {
3738
core.debug(`Tool found in cache ${toolPath}`);
@@ -45,7 +46,7 @@ export async function getJava(
4546
)).readBody();
4647
let refs = contents.match(/<a href.*\">/gi) || [];
4748

48-
const downloadInfo = getDownloadInfo(refs, version);
49+
const downloadInfo = getDownloadInfo(refs, version, javaPackage);
4950

5051
jdkFile = await tc.downloadTool(downloadInfo.url);
5152
version = downloadInfo.version;
@@ -66,7 +67,7 @@ export async function getJava(
6667
core.debug(`jdk extracted to ${jdkDir}`);
6768
toolPath = await tc.cacheDir(
6869
jdkDir,
69-
'Java',
70+
javaPackage,
7071
getCacheVersionString(version),
7172
arch
7273
);
@@ -173,7 +174,8 @@ async function unzipJavaDownload(
173174

174175
function getDownloadInfo(
175176
refs: string[],
176-
version: string
177+
version: string,
178+
javaPackage: string
177179
): {version: string; url: string} {
178180
version = normalizeVersion(version);
179181
let extension = '';
@@ -187,17 +189,34 @@ function getDownloadInfo(
187189
}
188190
}
189191

192+
let pkgRegexp = new RegExp('');
193+
let pkgTypeLength = 0;
194+
if (javaPackage === 'jdk') {
195+
pkgRegexp = /jdk.*-/gi;
196+
pkgTypeLength = 'jdk'.length;
197+
} else if (javaPackage == 'jre') {
198+
pkgRegexp = /jre.*-/gi;
199+
pkgTypeLength = 'jre'.length;
200+
} else if (javaPackage == 'jdk+fx') {
201+
pkgRegexp = /fx-jdk.*-/gi;
202+
pkgTypeLength = 'fx-jdk'.length;
203+
} else {
204+
throw new Error(
205+
`package argument ${javaPackage} is not in [jdk | jre | jdk+fx]`
206+
);
207+
}
208+
190209
// Maps version to url
191210
let versionMap = new Map();
192211

193212
// Filter by platform
194213
refs.forEach(ref => {
195-
if (ref.indexOf(extension) < 0) {
214+
if (!ref.endsWith(extension + '">')) {
196215
return;
197216
}
198217

199218
// If we haven't returned, means we're looking at the correct platform
200-
let versions = ref.match(/jdk.*-/gi) || [];
219+
let versions = ref.match(pkgRegexp) || [];
201220
if (versions.length > 1) {
202221
throw new Error(
203222
`Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}`
@@ -206,7 +225,7 @@ function getDownloadInfo(
206225
if (versions.length == 0) {
207226
return;
208227
}
209-
const refVersion = versions[0].slice('jdk'.length, versions[0].length - 1);
228+
const refVersion = versions[0].slice(pkgTypeLength, versions[0].length - 1);
210229

211230
if (semver.satisfies(refVersion, version)) {
212231
versionMap.set(
@@ -231,7 +250,7 @@ function getDownloadInfo(
231250

232251
if (curUrl == '') {
233252
throw new Error(
234-
`No valid download found for version ${version}. Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument`
253+
`No valid download found for version ${version} and package ${javaPackage}. Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument`
235254
);
236255
}
237256

src/setup-java.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ async function run() {
99
version = core.getInput('java-version', {required: true});
1010
}
1111
const arch = core.getInput('architecture', {required: true});
12+
const javaPackage = core.getInput('java-package', {required: true});
1213
const jdkFile = core.getInput('jdkFile', {required: false}) || '';
1314

14-
await installer.getJava(version, arch, jdkFile);
15+
await installer.getJava(version, arch, jdkFile, javaPackage);
1516

1617
const matchersPath = path.join(__dirname, '..', '.github');
1718
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);

0 commit comments

Comments
 (0)