From 2ba39e06854c58d29186c2fd380df271dbae8333 Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Tue, 20 Jun 2023 08:52:13 +0200 Subject: [PATCH] Add support for Oracle GraalVM JDK --- .github/workflows/e2e-versions.yml | 12 ++ README.md | 2 + .../distributors/graalvm-installer.test.ts | 109 ++++++++++++++++ dist/setup/index.js | 123 ++++++++++++++++++ docs/advanced-usage.md | 16 +++ src/distributions/distribution-factory.ts | 6 +- src/distributions/graalvm/installer.ts | 111 ++++++++++++++++ src/distributions/graalvm/models.ts | 1 + 8 files changed, 379 insertions(+), 1 deletion(-) create mode 100644 __tests__/distributors/graalvm-installer.test.ts create mode 100644 src/distributions/graalvm/installer.ts create mode 100644 src/distributions/graalvm/models.ts diff --git a/.github/workflows/e2e-versions.yml b/.github/workflows/e2e-versions.yml index b243936..f94b589 100644 --- a/.github/workflows/e2e-versions.yml +++ b/.github/workflows/e2e-versions.yml @@ -45,6 +45,15 @@ jobs: - distribution: oracle os: ubuntu-latest version: 20 + - distribution: graalvm + os: macos-latest + version: 17 + - distribution: graalvm + os: windows-latest + version: 20 + - distribution: graalvm + os: ubuntu-latest + version: 20 steps: - name: Checkout uses: actions/checkout@v3 @@ -75,6 +84,9 @@ jobs: - distribution: oracle os: ubuntu-latest version: '20.0.1' + - distribution: graalvm + os: ubuntu-latest + version: '20.0.1' steps: - name: Checkout uses: actions/checkout@v3 diff --git a/README.md b/README.md index ced5928..406ff39 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ Currently, the following distributions are supported: | `corretto` | Amazon Corretto Build of OpenJDK | [Link](https://aws.amazon.com/corretto/) | [Link](https://aws.amazon.com/corretto/faqs/) | `semeru` | IBM Semeru Runtime Open Edition | [Link](https://developer.ibm.com/languages/java/semeru-runtimes/downloads/) | [Link](https://openjdk.java.net/legal/gplv2+ce.html) | | `oracle` | Oracle JDK | [Link](https://www.oracle.com/java/technologies/downloads/) | [Link](https://java.com/freeuselicense) +| `graalvm` | Oracle GraalVM | [Link](https://www.graalvm.org/) | [Link](https://www.oracle.com/downloads/licenses/graal-free-license.html) **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. @@ -227,6 +228,7 @@ In the example above multiple JDKs are installed for the same job. The result af - [Microsoft](docs/advanced-usage.md#Microsoft) - [Amazon Corretto](docs/advanced-usage.md#Amazon-Corretto) - [Oracle](docs/advanced-usage.md#Oracle) + - [GraalVM](docs/advanced-usage.md#GraalVM) - [Installing custom Java package type](docs/advanced-usage.md#Installing-custom-Java-package-type) - [Installing custom Java architecture](docs/advanced-usage.md#Installing-custom-Java-architecture) - [Installing custom Java distribution from local file](docs/advanced-usage.md#Installing-Java-from-local-file) diff --git a/__tests__/distributors/graalvm-installer.test.ts b/__tests__/distributors/graalvm-installer.test.ts new file mode 100644 index 0000000..6b2a6f5 --- /dev/null +++ b/__tests__/distributors/graalvm-installer.test.ts @@ -0,0 +1,109 @@ +import {GraalVMDistribution} from '../../src/distributions/graalvm/installer'; +import os from 'os'; +import * as core from '@actions/core'; +import {getDownloadArchiveExtension} from '../../src/util'; +import {HttpClient} from '@actions/http-client'; + +describe('findPackageForDownload', () => { + let distribution: GraalVMDistribution; + let spyDebug: jest.SpyInstance; + let spyHttpClient: jest.SpyInstance; + + beforeEach(() => { + distribution = new GraalVMDistribution({ + version: '', + architecture: 'x64', + packageType: 'jdk', + checkLatest: false + }); + + spyDebug = jest.spyOn(core, 'debug'); + spyDebug.mockImplementation(() => {}); + }); + + it.each([ + [ + '20', + '20', + 'https://download.oracle.com/graalvm/20/latest/graalvm-jdk-20_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' + ], + [ + '20.0.1', + '20.0.1', + 'https://download.oracle.com/graalvm/20/archive/graalvm-jdk-20.0.1_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' + ], + [ + '17', + '17', + 'https://download.oracle.com/graalvm/17/latest/graalvm-jdk-17_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' + ], + [ + '17.0.7', + '17.0.7', + 'https://download.oracle.com/graalvm/17/archive/graalvm-jdk-17.0.7_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' + ] + ])('version is %s -> %s', async (input, expectedVersion, expectedUrl) => { + /* Needed only for this particular test because /latest/ urls tend to change */ + spyHttpClient = jest.spyOn(HttpClient.prototype, 'head'); + spyHttpClient.mockReturnValue( + Promise.resolve({ + message: { + statusCode: 200 + } + }) + ); + + const result = await distribution['findPackageForDownload'](input); + + jest.restoreAllMocks(); + + expect(result.version).toBe(expectedVersion); + const osType = distribution.getPlatform(); + const archiveType = getDownloadArchiveExtension(); + const url = expectedUrl + .replace('{{OS_TYPE}}', osType) + .replace('{{ARCHIVE_TYPE}}', archiveType); + expect(result.url).toBe(url); + }); + + it.each([ + ['amd64', 'x64'], + ['arm64', 'aarch64'] + ])( + 'defaults to os.arch(): %s mapped to distro arch: %s', + async (osArch: string, distroArch: string) => { + jest.spyOn(os, 'arch').mockReturnValue(osArch); + jest.spyOn(os, 'platform').mockReturnValue('linux'); + + const version = '17'; + const distro = new GraalVMDistribution({ + version, + architecture: '', // to get default value + packageType: 'jdk', + checkLatest: false + }); + + const osType = distribution.getPlatform(); + if (osType === 'windows' && distroArch == 'aarch64') { + return; // skip, aarch64 is not available for Windows + } + const archiveType = getDownloadArchiveExtension(); + const result = await distro['findPackageForDownload'](version); + const expectedUrl = `https://download.oracle.com/graalvm/17/latest/graalvm-jdk-17_${osType}-${distroArch}_bin.${archiveType}`; + + expect(result.url).toBe(expectedUrl); + } + ); + + it('should throw an error', async () => { + await expect(distribution['findPackageForDownload']('8')).rejects.toThrow( + /GraalVM JDK is only supported for JDK 17 and later/ + ); + await expect(distribution['findPackageForDownload']('11')).rejects.toThrow( + /GraalVM JDK is only supported for JDK 17 and later/ + ); + await expect(distribution['findPackageForDownload']('18')).rejects.toThrow( + /Could not find GraalVM JDK for SemVer */ + ); + }); +}); diff --git a/dist/setup/index.js b/dist/setup/index.js index 66a0a3c..66d6811 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -102434,6 +102434,7 @@ const installer_6 = __nccwpck_require__(3613); const installer_7 = __nccwpck_require__(1121); const installer_8 = __nccwpck_require__(4750); const installer_9 = __nccwpck_require__(4298); +const installer_10 = __nccwpck_require__(5644); var JavaDistribution; (function (JavaDistribution) { JavaDistribution["Adopt"] = "adopt"; @@ -102447,6 +102448,7 @@ var JavaDistribution; JavaDistribution["Semeru"] = "semeru"; JavaDistribution["Corretto"] = "corretto"; JavaDistribution["Oracle"] = "oracle"; + JavaDistribution["GraalVM"] = "graalvm"; })(JavaDistribution || (JavaDistribution = {})); function getJavaDistribution(distributionName, installerOptions, jdkFile) { switch (distributionName) { @@ -102471,6 +102473,8 @@ function getJavaDistribution(distributionName, installerOptions, jdkFile) { return new installer_8.CorrettoDistribution(installerOptions); case JavaDistribution.Oracle: return new installer_9.OracleDistribution(installerOptions); + case JavaDistribution.GraalVM: + return new installer_10.GraalVMDistribution(installerOptions); default: return null; } @@ -102478,6 +102482,125 @@ function getJavaDistribution(distributionName, installerOptions, jdkFile) { exports.getJavaDistribution = getJavaDistribution; +/***/ }), + +/***/ 5644: +/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { + +"use strict"; + +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.GraalVMDistribution = void 0; +const core = __importStar(__nccwpck_require__(2186)); +const tc = __importStar(__nccwpck_require__(7784)); +const fs_1 = __importDefault(__nccwpck_require__(7147)); +const path_1 = __importDefault(__nccwpck_require__(1017)); +const base_installer_1 = __nccwpck_require__(9741); +const util_1 = __nccwpck_require__(2629); +const http_client_1 = __nccwpck_require__(9925); +const GRAALVM_DL_BASE = 'https://download.oracle.com/graalvm'; +class GraalVMDistribution extends base_installer_1.JavaBase { + constructor(installerOptions) { + super('GraalVM', installerOptions); + } + downloadTool(javaRelease) { + return __awaiter(this, void 0, void 0, function* () { + core.info(`Downloading Java ${javaRelease.version} (${this.distribution}) from ${javaRelease.url} ...`); + const javaArchivePath = yield tc.downloadTool(javaRelease.url); + core.info(`Extracting Java archive...`); + const extension = util_1.getDownloadArchiveExtension(); + const extractedJavaPath = yield util_1.extractJdkFile(javaArchivePath, extension); + const archiveName = fs_1.default.readdirSync(extractedJavaPath)[0]; + const archivePath = path_1.default.join(extractedJavaPath, archiveName); + const version = this.getToolcacheVersionName(javaRelease.version); + const javaPath = yield tc.cacheDir(archivePath, this.toolcacheFolderName, version, this.architecture); + return { version: javaRelease.version, path: javaPath }; + }); + } + findPackageForDownload(range) { + return __awaiter(this, void 0, void 0, function* () { + const arch = this.distributionArchitecture(); + if (arch !== 'x64' && arch !== 'aarch64') { + throw new Error(`Unsupported architecture: ${this.architecture}`); + } + if (!this.stable) { + throw new Error('Early access versions are not supported'); + } + if (this.packageType !== 'jdk') { + throw new Error('GraalVM JDK provides only the `jdk` package type'); + } + const platform = this.getPlatform(); + const extension = util_1.getDownloadArchiveExtension(); + let major; + let fileUrl; + if (range.includes('.')) { + major = range.split('.')[0]; + fileUrl = `${GRAALVM_DL_BASE}/${major}/archive/graalvm-jdk-${range}_${platform}-${arch}_bin.${extension}`; + } + else { + major = range; + fileUrl = `${GRAALVM_DL_BASE}/${range}/latest/graalvm-jdk-${range}_${platform}-${arch}_bin.${extension}`; + } + if (parseInt(major) < 17) { + throw new Error('GraalVM JDK is only supported for JDK 17 and later'); + } + const response = yield this.http.head(fileUrl); + if (response.message.statusCode === http_client_1.HttpCodes.NotFound) { + throw new Error(`Could not find GraalVM JDK for SemVer ${range}`); + } + if (response.message.statusCode !== http_client_1.HttpCodes.OK) { + throw new Error(`Http request for GraalVM JDK failed with status code: ${response.message.statusCode}`); + } + return { url: fileUrl, version: range }; + }); + } + getPlatform(platform = process.platform) { + switch (platform) { + case 'darwin': + return 'macos'; + case 'win32': + return 'windows'; + case 'linux': + return 'linux'; + default: + throw new Error(`Platform '${platform}' is not supported. Supported platforms: 'linux', 'macos', 'windows'`); + } + } +} +exports.GraalVMDistribution = GraalVMDistribution; + + /***/ }), /***/ 883: diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 7fbcc3a..92043b5 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -7,6 +7,7 @@ - [Microsoft](#Microsoft) - [Amazon Corretto](#Amazon-Corretto) - [Oracle](#Oracle) + - [GraalVM](#GraalVM) - [Installing custom Java package type](#Installing-custom-Java-package-type) - [Installing custom Java architecture](#Installing-custom-Java-architecture) - [Installing custom Java distribution from local file](#Installing-Java-from-local-file) @@ -124,6 +125,21 @@ steps: - run: java -cp java HelloWorldApp ``` +### GraalVM +**NOTE:** Oracle GraalVM JDK is only available for version 17 and later. + +```yaml +steps: +- uses: actions/checkout@v3 +- uses: actions/setup-java@v3 + with: + distribution: 'graalvm' + java-version: '17' +- run: + java -cp java HelloWorldApp + native-image -cp java HelloWorldApp +``` + ## Installing custom Java package type ```yaml steps: diff --git a/src/distributions/distribution-factory.ts b/src/distributions/distribution-factory.ts index 1dd5131..4c0a493 100644 --- a/src/distributions/distribution-factory.ts +++ b/src/distributions/distribution-factory.ts @@ -9,6 +9,7 @@ import {MicrosoftDistributions} from './microsoft/installer'; import {SemeruDistribution} from './semeru/installer'; import {CorrettoDistribution} from './corretto/installer'; import {OracleDistribution} from './oracle/installer'; +import {GraalVMDistribution} from './graalvm/installer'; enum JavaDistribution { Adopt = 'adopt', @@ -21,7 +22,8 @@ enum JavaDistribution { Microsoft = 'microsoft', Semeru = 'semeru', Corretto = 'corretto', - Oracle = 'oracle' + Oracle = 'oracle', + GraalVM = 'graalvm' } export function getJavaDistribution( @@ -60,6 +62,8 @@ export function getJavaDistribution( return new CorrettoDistribution(installerOptions); case JavaDistribution.Oracle: return new OracleDistribution(installerOptions); + case JavaDistribution.GraalVM: + return new GraalVMDistribution(installerOptions); default: return null; } diff --git a/src/distributions/graalvm/installer.ts b/src/distributions/graalvm/installer.ts new file mode 100644 index 0000000..d213642 --- /dev/null +++ b/src/distributions/graalvm/installer.ts @@ -0,0 +1,111 @@ +import * as core from '@actions/core'; +import * as tc from '@actions/tool-cache'; + +import fs from 'fs'; +import path from 'path'; + +import {JavaBase} from '../base-installer'; +import { + JavaDownloadRelease, + JavaInstallerOptions, + JavaInstallerResults +} from '../base-models'; +import {extractJdkFile, getDownloadArchiveExtension} from '../../util'; +import {HttpCodes} from '@actions/http-client'; + +const GRAALVM_DL_BASE = 'https://download.oracle.com/graalvm'; + +export class GraalVMDistribution extends JavaBase { + constructor(installerOptions: JavaInstallerOptions) { + super('GraalVM', installerOptions); + } + + protected async downloadTool( + javaRelease: JavaDownloadRelease + ): Promise { + core.info( + `Downloading Java ${javaRelease.version} (${this.distribution}) from ${javaRelease.url} ...` + ); + const javaArchivePath = await tc.downloadTool(javaRelease.url); + + core.info(`Extracting Java archive...`); + const extension = getDownloadArchiveExtension(); + + const extractedJavaPath = await extractJdkFile(javaArchivePath, extension); + + const archiveName = fs.readdirSync(extractedJavaPath)[0]; + const archivePath = path.join(extractedJavaPath, archiveName); + const version = this.getToolcacheVersionName(javaRelease.version); + + const javaPath = await tc.cacheDir( + archivePath, + this.toolcacheFolderName, + version, + this.architecture + ); + + return {version: javaRelease.version, path: javaPath}; + } + + protected async findPackageForDownload( + range: string + ): Promise { + const arch = this.distributionArchitecture(); + if (arch !== 'x64' && arch !== 'aarch64') { + throw new Error(`Unsupported architecture: ${this.architecture}`); + } + + if (!this.stable) { + throw new Error('Early access versions are not supported'); + } + + if (this.packageType !== 'jdk') { + throw new Error('GraalVM JDK provides only the `jdk` package type'); + } + + const platform = this.getPlatform(); + const extension = getDownloadArchiveExtension(); + let major; + let fileUrl; + if (range.includes('.')) { + major = range.split('.')[0]; + fileUrl = `${GRAALVM_DL_BASE}/${major}/archive/graalvm-jdk-${range}_${platform}-${arch}_bin.${extension}`; + } else { + major = range; + fileUrl = `${GRAALVM_DL_BASE}/${range}/latest/graalvm-jdk-${range}_${platform}-${arch}_bin.${extension}`; + } + + if (parseInt(major) < 17) { + throw new Error('GraalVM JDK is only supported for JDK 17 and later'); + } + + const response = await this.http.head(fileUrl); + + if (response.message.statusCode === HttpCodes.NotFound) { + throw new Error(`Could not find GraalVM JDK for SemVer ${range}`); + } + + if (response.message.statusCode !== HttpCodes.OK) { + throw new Error( + `Http request for GraalVM JDK failed with status code: ${response.message.statusCode}` + ); + } + + return {url: fileUrl, version: range}; + } + + public getPlatform(platform: NodeJS.Platform = process.platform): OsVersions { + switch (platform) { + case 'darwin': + return 'macos'; + case 'win32': + return 'windows'; + case 'linux': + return 'linux'; + default: + throw new Error( + `Platform '${platform}' is not supported. Supported platforms: 'linux', 'macos', 'windows'` + ); + } + } +} diff --git a/src/distributions/graalvm/models.ts b/src/distributions/graalvm/models.ts new file mode 100644 index 0000000..2838ead --- /dev/null +++ b/src/distributions/graalvm/models.ts @@ -0,0 +1 @@ +export type OsVersions = 'linux' | 'macos' | 'windows';