1
0
Fork 0
mirror of https://github.com/actions/setup-java.git synced 2024-08-16 10:19:51 +02:00

Merge pull request #27 from foo-actions/master

Add java-package parameter to action, support jre, jdk, and jdk+fx
This commit is contained in:
Konrad Pabjan 2019-11-22 14:57:43 -05:00 committed by GitHub
commit ef3ee0a8c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 114 additions and 44 deletions

View file

@ -14,7 +14,7 @@ jobs:
- name: Set Node.js 10.x - name: Set Node.js 10.x
uses: actions/setup-node@master uses: actions/setup-node@master
with: with:
version: 10.x node-version: 10.x
- name: npm install - name: npm install
run: npm install run: npm install

View file

@ -6,7 +6,7 @@
This action sets up a java environment for use in actions by: This action sets up a java environment for use in actions by:
- 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/). - 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/)
- registering problem matchers for error output - registering problem matchers for error output
# Usage # Usage
@ -19,7 +19,8 @@ steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- uses: actions/setup-java@v1 - uses: actions/setup-java@v1
with: with:
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) 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)
java-package: jdk // (jre, jdk, or jdk+fx) - defaults to jdk
architecture: x64 // (x64 or x86) - defaults to x64 architecture: x64 // (x64 or x86) - defaults to x64
- run: java -cp java HelloWorldApp - run: java -cp java HelloWorldApp
``` ```
@ -32,7 +33,7 @@ steps:
with: with:
java-version: '4.0.0' java-version: '4.0.0'
architecture: x64 architecture: x64
jdkFile: <path to jdkFile> # Optional - jdkFile to install java from. Useful for versions not supported by Azul jdkFile: <path to jdkFile> # Optional - jdkFile to install java from. Useful for versions not found on Zulu Community CDN
- run: java -cp java HelloWorldApp - run: java -cp java HelloWorldApp
``` ```
@ -43,7 +44,8 @@ jobs:
runs-on: ubuntu-16.04 runs-on: ubuntu-16.04
strategy: strategy:
matrix: matrix:
java: [ '1.6', '9.0.x', '12.0.2' ] # test against latest update of each major Java version, as well as specific updates of LTS versions:
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 ]
name: Java ${{ matrix.java }} sample name: Java ${{ matrix.java }} sample
steps: steps:
- uses: actions/checkout@master - uses: actions/checkout@master
@ -51,7 +53,6 @@ jobs:
uses: actions/setup-java@v1 uses: actions/setup-java@v1
with: with:
java-version: ${{ matrix.java }} java-version: ${{ matrix.java }}
architecture: x64
- run: java -cp java HelloWorldApp - run: java -cp java HelloWorldApp
``` ```

View file

@ -52,8 +52,8 @@ describe('installer tests', () => {
}, 100000); }, 100000);
it('Installs version of Java from jdkFile if no matching version is installed', async () => { it('Installs version of Java from jdkFile if no matching version is installed', async () => {
await installer.getJava('12', 'x64', javaFilePath); await installer.getJava('12', 'x64', javaFilePath, 'jdk');
const JavaDir = path.join(toolDir, 'Java', '12.0.0', 'x64'); const JavaDir = path.join(toolDir, 'jdk', '12.0.0', 'x64');
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true); expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true); expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
@ -62,7 +62,7 @@ describe('installer tests', () => {
it('Throws if invalid directory to jdk', async () => { it('Throws if invalid directory to jdk', async () => {
let thrown = false; let thrown = false;
try { try {
await installer.getJava('1000', 'x64', 'bad path'); await installer.getJava('1000', 'x64', 'bad path', 'jdk');
} catch { } catch {
thrown = true; thrown = true;
} }
@ -70,33 +70,59 @@ describe('installer tests', () => {
}); });
it('Downloads java if no file given', async () => { it('Downloads java if no file given', async () => {
await installer.getJava('8.0.102', 'x64', ''); await installer.getJava('8.0.102', 'x64', '', 'jdk');
const JavaDir = path.join(toolDir, 'Java', '8.0.102', 'x64'); const JavaDir = path.join(toolDir, 'jdk', '8.0.102', 'x64');
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true); expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true); expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
}, 100000); }, 100000);
it('Downloads java with 1.x syntax', async () => { it('Downloads java with 1.x syntax', async () => {
await installer.getJava('1.10', 'x64', ''); await installer.getJava('1.10', 'x64', '', 'jdk');
const JavaDir = path.join(toolDir, 'Java', '10.0.2', 'x64'); const JavaDir = path.join(toolDir, 'jdk', '10.0.2', 'x64');
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true); expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true); expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
}, 100000); }, 100000);
it('Downloads java with normal semver syntax', async () => { it('Downloads java with normal semver syntax', async () => {
await installer.getJava('9.0.x', 'x64', ''); await installer.getJava('9.0.x', 'x64', '', 'jdk');
const JavaDir = path.join(toolDir, 'Java', '9.0.7', 'x64'); const JavaDir = path.join(toolDir, 'jdk', '9.0.7', 'x64');
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true); expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true); expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
}, 100000); }, 100000);
it('Downloads java if package is jre', async () => {
await installer.getJava('8.0.222', 'x64', '', 'jre');
const JavaDir = path.join(toolDir, 'jre', '8.0.222', 'x64');
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
}, 100000);
it('Downloads java if package is jdk+fx', async () => {
await installer.getJava('8.0.222', 'x64', '', 'jdk+fx');
const JavaDir = path.join(toolDir, 'jdk+fx', '8.0.222', 'x64');
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
}, 100000);
it('Throws if invalid java package is specified', async () => {
let thrown = false;
try {
await installer.getJava('8.0.222', 'x64', '', 'bad jdk');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});
it('Throws if invalid directory to jdk', async () => { it('Throws if invalid directory to jdk', async () => {
let thrown = false; let thrown = false;
try { try {
await installer.getJava('1000', 'x64', 'bad path'); await installer.getJava('1000', 'x64', 'bad path', 'jdk');
} catch { } catch {
thrown = true; thrown = true;
} }
@ -104,25 +130,26 @@ describe('installer tests', () => {
}); });
it('Uses version of Java installed in cache', async () => { it('Uses version of Java installed in cache', async () => {
const JavaDir: string = path.join(toolDir, 'Java', '250.0.0', 'x64'); const JavaDir: string = path.join(toolDir, 'jdk', '250.0.0', 'x64');
await io.mkdirP(JavaDir); await io.mkdirP(JavaDir);
fs.writeFileSync(`${JavaDir}.complete`, 'hello'); fs.writeFileSync(`${JavaDir}.complete`, 'hello');
// This will throw if it doesn't find it in the cache (because no such version exists) // This will throw if it doesn't find it in the cache (because no such version exists)
await installer.getJava( await installer.getJava(
'250', '250',
'x64', 'x64',
'path shouldnt matter, found in cache' 'path shouldnt matter, found in cache',
'jdk'
); );
return; return;
}); });
it('Doesnt use version of Java that was only partially installed in cache', async () => { it('Doesnt use version of Java that was only partially installed in cache', async () => {
const JavaDir: string = path.join(toolDir, 'Java', '251.0.0', 'x64'); const JavaDir: string = path.join(toolDir, 'jdk', '251.0.0', 'x64');
await io.mkdirP(JavaDir); await io.mkdirP(JavaDir);
let thrown = false; let thrown = false;
try { try {
// This will throw if it doesn't find it in the cache (because no such version exists) // This will throw if it doesn't find it in the cache (because no such version exists)
await installer.getJava('251', 'x64', 'bad path'); await installer.getJava('251', 'x64', 'bad path', 'jdk');
} catch { } catch {
thrown = true; thrown = true;
} }

View file

@ -3,11 +3,15 @@ description: 'Set up a specific version of the Java JDK and add the command-line
author: 'GitHub' author: 'GitHub'
inputs: inputs:
java-version: java-version:
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)' 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 => Java 8.x)'
required: true required: true
java-package:
description: 'The package type (jre, jdk, jdk+fx)'
required: false
default: 'jdk'
architecture: architecture:
description: 'The architecture (x86, x64) of the JDK.' description: 'The architecture (x86, x64) of the package.'
required: true required: false
default: 'x64' default: 'x64'
jdkFile: jdkFile:
description: 'Path to where the compressed JDK is located. The path could be in your source repository or a local path on the agent.' description: 'Path to where the compressed JDK is located. The path could be in your source repository or a local path on the agent.'

View file

@ -41,9 +41,9 @@ if (!tempDirectory) {
} }
tempDirectory = path.join(baseLocation, 'actions', 'temp'); tempDirectory = path.join(baseLocation, 'actions', 'temp');
} }
function getJava(version, arch, jdkFile) { function getJava(version, arch, jdkFile, javaPackage) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let toolPath = tc.find('Java', version); let toolPath = tc.find(javaPackage, version);
if (toolPath) { if (toolPath) {
core.debug(`Tool found in cache ${toolPath}`); core.debug(`Tool found in cache ${toolPath}`);
} }
@ -54,7 +54,7 @@ function getJava(version, arch, jdkFile) {
let http = new httpm.HttpClient('setup-java'); let http = new httpm.HttpClient('setup-java');
let contents = yield (yield http.get('https://static.azul.com/zulu/bin/')).readBody(); let contents = yield (yield http.get('https://static.azul.com/zulu/bin/')).readBody();
let refs = contents.match(/<a href.*\">/gi) || []; let refs = contents.match(/<a href.*\">/gi) || [];
const downloadInfo = getDownloadInfo(refs, version); const downloadInfo = getDownloadInfo(refs, version, javaPackage);
jdkFile = yield tc.downloadTool(downloadInfo.url); jdkFile = yield tc.downloadTool(downloadInfo.url);
version = downloadInfo.version; version = downloadInfo.version;
compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz'; compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz';
@ -66,7 +66,7 @@ function getJava(version, arch, jdkFile) {
let tempDir = path.join(tempDirectory, 'temp_' + Math.floor(Math.random() * 2000000000)); let tempDir = path.join(tempDirectory, 'temp_' + Math.floor(Math.random() * 2000000000));
const jdkDir = yield unzipJavaDownload(jdkFile, compressedFileExtension, tempDir); const jdkDir = yield unzipJavaDownload(jdkFile, compressedFileExtension, tempDir);
core.debug(`jdk extracted to ${jdkDir}`); core.debug(`jdk extracted to ${jdkDir}`);
toolPath = yield tc.cacheDir(jdkDir, 'Java', getCacheVersionString(version), arch); toolPath = yield tc.cacheDir(jdkDir, javaPackage, getCacheVersionString(version), arch);
} }
let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch; let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch;
core.exportVariable('JAVA_HOME', toolPath); core.exportVariable('JAVA_HOME', toolPath);
@ -162,7 +162,7 @@ function unzipJavaDownload(repoRoot, fileEnding, destinationFolder, extension) {
} }
}); });
} }
function getDownloadInfo(refs, version) { function getDownloadInfo(refs, version, javaPackage) {
version = normalizeVersion(version); version = normalizeVersion(version);
let extension = ''; let extension = '';
if (IS_WINDOWS) { if (IS_WINDOWS) {
@ -176,22 +176,39 @@ function getDownloadInfo(refs, version) {
extension = `-linux_x64.tar.gz`; extension = `-linux_x64.tar.gz`;
} }
} }
let pkgRegexp = new RegExp('');
let pkgTypeLength = 0;
if (javaPackage === 'jdk') {
pkgRegexp = /jdk.*-/gi;
pkgTypeLength = 'jdk'.length;
}
else if (javaPackage == 'jre') {
pkgRegexp = /jre.*-/gi;
pkgTypeLength = 'jre'.length;
}
else if (javaPackage == 'jdk+fx') {
pkgRegexp = /fx-jdk.*-/gi;
pkgTypeLength = 'fx-jdk'.length;
}
else {
throw new Error(`package argument ${javaPackage} is not in [jdk | jre | jdk+fx]`);
}
// Maps version to url // Maps version to url
let versionMap = new Map(); let versionMap = new Map();
// Filter by platform // Filter by platform
refs.forEach(ref => { refs.forEach(ref => {
if (ref.indexOf(extension) < 0) { if (!ref.endsWith(extension + '">')) {
return; return;
} }
// If we haven't returned, means we're looking at the correct platform // If we haven't returned, means we're looking at the correct platform
let versions = ref.match(/jdk.*-/gi) || []; let versions = ref.match(pkgRegexp) || [];
if (versions.length > 1) { if (versions.length > 1) {
throw new Error(`Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}`); throw new Error(`Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}`);
} }
if (versions.length == 0) { if (versions.length == 0) {
return; return;
} }
const refVersion = versions[0].slice('jdk'.length, versions[0].length - 1); const refVersion = versions[0].slice(pkgTypeLength, versions[0].length - 1);
if (semver.satisfies(refVersion, version)) { if (semver.satisfies(refVersion, version)) {
versionMap.set(refVersion, 'https://static.azul.com/zulu/bin/' + versionMap.set(refVersion, 'https://static.azul.com/zulu/bin/' +
ref.slice('<a href="'.length, ref.length - '">'.length)); ref.slice('<a href="'.length, ref.length - '">'.length));
@ -209,7 +226,7 @@ function getDownloadInfo(refs, version) {
} }
} }
if (curUrl == '') { if (curUrl == '') {
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`); 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`);
} }
return { version: curVersion, url: curUrl }; return { version: curVersion, url: curUrl };
} }

View file

@ -26,8 +26,9 @@ function run() {
version = core.getInput('java-version', { required: true }); version = core.getInput('java-version', { required: true });
} }
const arch = core.getInput('architecture', { required: true }); const arch = core.getInput('architecture', { required: true });
const javaPackage = core.getInput('java-package', { required: true });
const jdkFile = core.getInput('jdkFile', { required: false }) || ''; const jdkFile = core.getInput('jdkFile', { required: false }) || '';
yield installer.getJava(version, arch, jdkFile); yield installer.getJava(version, arch, jdkFile, javaPackage);
const matchersPath = path.join(__dirname, '..', '.github'); const matchersPath = path.join(__dirname, '..', '.github');
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`); console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
} }

View file

@ -29,9 +29,10 @@ if (!tempDirectory) {
export async function getJava( export async function getJava(
version: string, version: string,
arch: string, arch: string,
jdkFile: string jdkFile: string,
javaPackage: string
): Promise<void> { ): Promise<void> {
let toolPath = tc.find('Java', version); let toolPath = tc.find(javaPackage, version);
if (toolPath) { if (toolPath) {
core.debug(`Tool found in cache ${toolPath}`); core.debug(`Tool found in cache ${toolPath}`);
@ -45,7 +46,7 @@ export async function getJava(
)).readBody(); )).readBody();
let refs = contents.match(/<a href.*\">/gi) || []; let refs = contents.match(/<a href.*\">/gi) || [];
const downloadInfo = getDownloadInfo(refs, version); const downloadInfo = getDownloadInfo(refs, version, javaPackage);
jdkFile = await tc.downloadTool(downloadInfo.url); jdkFile = await tc.downloadTool(downloadInfo.url);
version = downloadInfo.version; version = downloadInfo.version;
@ -66,7 +67,7 @@ export async function getJava(
core.debug(`jdk extracted to ${jdkDir}`); core.debug(`jdk extracted to ${jdkDir}`);
toolPath = await tc.cacheDir( toolPath = await tc.cacheDir(
jdkDir, jdkDir,
'Java', javaPackage,
getCacheVersionString(version), getCacheVersionString(version),
arch arch
); );
@ -173,7 +174,8 @@ async function unzipJavaDownload(
function getDownloadInfo( function getDownloadInfo(
refs: string[], refs: string[],
version: string version: string,
javaPackage: string
): {version: string; url: string} { ): {version: string; url: string} {
version = normalizeVersion(version); version = normalizeVersion(version);
let extension = ''; let extension = '';
@ -187,17 +189,34 @@ function getDownloadInfo(
} }
} }
let pkgRegexp = new RegExp('');
let pkgTypeLength = 0;
if (javaPackage === 'jdk') {
pkgRegexp = /jdk.*-/gi;
pkgTypeLength = 'jdk'.length;
} else if (javaPackage == 'jre') {
pkgRegexp = /jre.*-/gi;
pkgTypeLength = 'jre'.length;
} else if (javaPackage == 'jdk+fx') {
pkgRegexp = /fx-jdk.*-/gi;
pkgTypeLength = 'fx-jdk'.length;
} else {
throw new Error(
`package argument ${javaPackage} is not in [jdk | jre | jdk+fx]`
);
}
// Maps version to url // Maps version to url
let versionMap = new Map(); let versionMap = new Map();
// Filter by platform // Filter by platform
refs.forEach(ref => { refs.forEach(ref => {
if (ref.indexOf(extension) < 0) { if (!ref.endsWith(extension + '">')) {
return; return;
} }
// If we haven't returned, means we're looking at the correct platform // If we haven't returned, means we're looking at the correct platform
let versions = ref.match(/jdk.*-/gi) || []; let versions = ref.match(pkgRegexp) || [];
if (versions.length > 1) { if (versions.length > 1) {
throw new Error( throw new Error(
`Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}` `Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}`
@ -206,7 +225,7 @@ function getDownloadInfo(
if (versions.length == 0) { if (versions.length == 0) {
return; return;
} }
const refVersion = versions[0].slice('jdk'.length, versions[0].length - 1); const refVersion = versions[0].slice(pkgTypeLength, versions[0].length - 1);
if (semver.satisfies(refVersion, version)) { if (semver.satisfies(refVersion, version)) {
versionMap.set( versionMap.set(
@ -231,7 +250,7 @@ function getDownloadInfo(
if (curUrl == '') { if (curUrl == '') {
throw new Error( 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` `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`
); );
} }

View file

@ -9,9 +9,10 @@ async function run() {
version = core.getInput('java-version', {required: true}); version = core.getInput('java-version', {required: true});
} }
const arch = core.getInput('architecture', {required: true}); const arch = core.getInput('architecture', {required: true});
const javaPackage = core.getInput('java-package', {required: true});
const jdkFile = core.getInput('jdkFile', {required: false}) || ''; const jdkFile = core.getInput('jdkFile', {required: false}) || '';
await installer.getJava(version, arch, jdkFile); await installer.getJava(version, arch, jdkFile, javaPackage);
const matchersPath = path.join(__dirname, '..', '.github'); const matchersPath = path.join(__dirname, '..', '.github');
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`); console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);