diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9874cf6..59a24a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,13 +20,49 @@ jobs: npm install - run: | npm run all + test: + name: GraalVM + runs-on: ${{ matrix.os }} + strategy: + matrix: + java-version: ['17', '20', 'dev'] + distribution: ['graalvm', 'graalvm-community'] + os: [macos-latest, windows-latest, ubuntu-latest] + steps: + - uses: actions/checkout@v3 + - name: Run setup-graalvm action + uses: ./ + with: + java-version: ${{ matrix.java-version }} + distribution: ${{ matrix.distribution }} + github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Check environment + run: | + echo "GRAALVM_HOME: $GRAALVM_HOME" + if [[ "${{ matrix.java-version }}" == "dev" ]]; then + [[ "$GRAALVM_HOME" == *"$RUNNER_TEMP"* ]] || exit 12 + else + [[ "$GRAALVM_HOME" == *"$RUNNER_TOOL_CACHE"* ]] || exit 23 + fi + echo "JAVA_HOME: $JAVA_HOME" + java --version + java --version | grep "GraalVM" || exit 34 + native-image --version + if: runner.os != 'Windows' + - name: Check Windows environment + run: | + echo "GRAALVM_HOME: $env:GRAALVM_HOME" + echo "JAVA_HOME: $env:JAVA_HOME" + java --version + native-image --version test-ce: # make sure the action works on a clean machine without building + needs: test name: CE ${{ matrix.version }} + JDK${{ matrix.java-version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: matrix: version: ['latest', 'dev'] - java-version: ['19', '20'] + java-version: ['17', '20'] components: ['native-image'] os: [macos-latest, windows-latest, ubuntu-latest] exclude: @@ -70,7 +106,7 @@ jobs: fi echo "JAVA_HOME: $JAVA_HOME" java --version - java --version | grep "GraalVM CE" || exit 34 + java --version | grep "GraalVM" || exit 34 native-image --version gu list if: runner.os != 'Windows' @@ -83,6 +119,7 @@ jobs: gu.cmd remove native-image if: runner.os == 'Windows' test-ee: + needs: test name: EE ${{ matrix.version }} + JDK${{ matrix.java-version }} on ${{ matrix.os }} if: github.event_name != 'pull_request' runs-on: ${{ matrix.os }} @@ -130,6 +167,7 @@ jobs: gu.cmd remove native-image if: runner.os == 'Windows' test-mandrel: + needs: test name: ${{ matrix.version }} + JDK${{ matrix.java-version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: @@ -175,9 +213,8 @@ jobs: - name: Run setup-graalvm action uses: ./ with: - version: 'dev' java-version: 'dev' - components: 'native-image' + distribution: 'graalvm-community' native-image-job-reports: 'true' native-image-pr-reports: 'true' github-token: ${{ secrets.GITHUB_TOKEN }} @@ -188,8 +225,8 @@ jobs: native-image HelloWorld ./helloworld test-native-image-windows-msvc: - name: native-image on windows-2019 - runs-on: windows-2019 + name: native-image on windows-2022 + runs-on: windows-2022 permissions: contents: read pull-requests: write # for `native-image-pr-reports` option @@ -198,9 +235,8 @@ jobs: - name: Run setup-graalvm action uses: ./ with: - version: '22.3.1' java-version: '17' - components: 'native-image' + distribution: 'graalvm' native-image-job-reports: 'true' native-image-pr-reports: 'true' github-token: ${{ secrets.GITHUB_TOKEN }} @@ -221,9 +257,8 @@ jobs: - name: Run setup-graalvm action uses: ./ with: - version: 'dev' java-version: 'dev' - components: 'native-image' + distribution: 'graalvm-community' native-image-musl: 'true' native-image-job-reports: 'true' native-image-pr-reports: 'true' @@ -245,9 +280,9 @@ jobs: - name: Run setup-graalvm action uses: ./ with: - version: 'latest' java-version: '17' - components: 'espresso,llvm-toolchain,native-image,nodejs,python,R,ruby,wasm' + distribution: 'graalvm' + components: 'espresso,llvm-toolchain,native-image,nodejs,python,ruby,wasm' set-java-home: 'false' native-image-job-reports: 'true' native-image-pr-reports: 'true' @@ -268,8 +303,6 @@ jobs: [[ $(which node) == *"graalvm"* ]] || exit 45 node --version graalpy --version - [[ $(which R) == *"graalvm"* ]] || exit 56 - R --version truffleruby --version wasm --version - name: Build HelloWorld.java with GraalVM Native Image @@ -290,4 +323,4 @@ jobs: bundle exec rake test popd > /dev/null - name: Remove components - run: gu remove espresso llvm-toolchain nodejs python R ruby wasm + run: gu remove espresso llvm-toolchain nodejs python ruby wasm diff --git a/README.md b/README.md index eed49e8..c1e776c 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,46 @@ # GitHub Action for GraalVM [![build-test](https://github.com/graalvm/setup-graalvm/actions/workflows/test.yml/badge.svg)](https://github.com/graalvm/setup-graalvm/actions/workflows/test.yml) -This GitHub action sets up GraalVM [Community Edition (CE)][repo] or [Enterprise Edition (EE)][graalvm-ee] as well as GraalVM components such as [Native Image][native-image] and [Truffle languages][truffle-languages]. +This GitHub action sets up [Oracle GraalVM][graalvm], GraalVM [Community Edition (CE)][repo], [Enterprise Edition (EE)][graalvm-ee], or [Mandrel][mandrel], as well as [Native Image][native-image] and GraalVM components such as [Truffle languages][truffle-languages]. ## Key Features This action: -- supports GraalVM Community Edition (CE) [releases], [dev builds][dev-builds], GraalVM Enterprise Edition (EE) [releases][graalvm-ee] (set [`gds-token`](#options)) 22.1.0 and later, and [Mandrel][mandrel] (see [options](#options)) -- has built-in support for GraalVM components and the [GraalVM Updater][gu] +- supports Oracle GraalVM [releases][graalvm-dl], GraalVM Community Edition (CE) [releases], [dev builds][dev-builds], GraalVM Enterprise Edition (EE) [releases][graalvm-ee] (set [`gds-token`](#options)) 22.1.0 and later, and [Mandrel][mandrel] (see [Options](#options)) - exports a `$GRAALVM_HOME` environment variable -- adds `$GRAALVM_HOME/bin` to the `$PATH` environment variable
(Truffle languages and tools can be invoked directly) -- sets `$JAVA_HOME` to `$GRAALVM_HOME` by default
(can be disabled via `set-java-home: 'false'`, see [options](#options)) -- supports `amd64` and `aarch64` (selected automatically, `aarch64` requires a [self-hosted runner][gha-self-hosted-runners]) -- sets up Windows environments with build tools using [vcvarsall.bat][vcvarsall] +- adds `$GRAALVM_HOME/bin` to the `$PATH` environment variable
(Native Image, Truffle languages, and tools can be invoked directly) +- sets `$JAVA_HOME` to `$GRAALVM_HOME` by default
(can be disabled via `set-java-home: 'false'`, see [Options](#options)) +- supports `x64` and `aarch64` (selected automatically, `aarch64` requires a [self-hosted runner][gha-self-hosted-runners]) - supports dependency caching for Apache Maven, Gradle, and sbt (see [`cache` option](#options)) +- sets up Windows environments with build tools using [vcvarsall.bat][vcvarsall] +- has built-in support for GraalVM components and the [GraalVM Updater][gu] + + +## Migrating from GraalVM 22.3 or earlier to the new GraalVM for JDK 17 and later + +The new [GraalVM release](https://medium.com/graalvm/a-new-graalvm-release-and-new-free-license-4aab483692f5) aligns the version scheme with OpenJDK. +As a result, this action no longer requires the `version` option that was used to select a specific GraalVM version. +At the same time, it introduces a new `distribution` option that can be used to select a specific GraalVM distribution (`graalvm`, `graalvm-community`, and `mandrel`). +Therefore, to migrate workflows to use the latest GraalVM release, replace the `version` with the `distribution` option in your workflow `yml` config, for example: + +```yml +# ... +- uses: graalvm/setup-graalvm@v1 + with: + java-version: '17' + version: '22.3.2' # Old 'version' option for the GraalVM version + # ... +``` + +can be turned into: + +```yml +# ... +- uses: graalvm/setup-graalvm@v1 + with: + java-version: '17.0.7' # for a specific JDK 17; or '17' for the latest JDK 17 + distribution: 'graalvm' # New 'distribution' option + # ... +``` ## Templates @@ -20,7 +48,7 @@ This action: ### Quickstart Template ```yml -name: GraalVM Community Edition build +name: GraalVM build on: [push, pull_request] jobs: build: @@ -29,17 +57,19 @@ jobs: - uses: actions/checkout@v3 - uses: graalvm/setup-graalvm@v1 with: - version: 'latest' - java-version: '17' - components: 'native-image' + java-version: '17.0.7' + distribution: 'graalvm' # See 'Options' for all available distributions github-token: ${{ secrets.GITHUB_TOKEN }} - name: Example step run: | echo "GRAALVM_HOME: $GRAALVM_HOME" echo "JAVA_HOME: $JAVA_HOME" java --version - gu --version native-image --version + - name: Example step using Maven plugin # https://graalvm.github.io/native-build-tools/latest/maven-plugin.html + run: mvn -Pnative package + - name: Example step using Gradle plugin # https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html + run: gradlew nativeCompile ``` ### Building a HelloWorld with GraalVM Native Image on Different Platforms @@ -59,9 +89,8 @@ jobs: - uses: graalvm/setup-graalvm@v1 with: - version: '22.3.0' - java-version: '17' - components: 'native-image' + java-version: '17.0.7' + distribution: 'graalvm' github-token: ${{ secrets.GITHUB_TOKEN }} native-image-job-reports: 'true' @@ -79,7 +108,29 @@ jobs: path: helloworld* ``` -### Basic GraalVM Enterprise Edition Template +
+

Template for older GraalVM releases

+ +```yml +name: GraalVM build +on: [push, pull_request] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: graalvm/setup-graalvm@v1 + with: + version: '22.3.2' # GraalVM version + java-version: '17' + components: 'native-image' + github-token: ${{ secrets.GITHUB_TOKEN }} +``` + +
+ +
+

Template for GraalVM Enterprise Edition

#### Prerequisites @@ -109,22 +160,24 @@ jobs: native-image --version ``` +
## Options | Name | Default | Description | |-----------------|:--------:|-------------| -| `version`
*(required)* | n/a | `X.Y.Z` (e.g., `22.3.0`) for a specific [GraalVM release][releases]
`latest` for [latest stable release][stable],
`dev` for [latest dev build][dev-build],
`mandrel-X.Y.Z` (e.g., `mandrel-21.3.0.0-Final`) for a specific [Mandrel release][mandrel-releases],
`mandrel-latest` for [latest Mandrel stable release][mandrel-stable]. | -| `gds-token` | `''` | Download token for the GraalVM Download Service. If a non-empty token is provided, the action will set up GraalVM Enterprise Edition (see [GraalVM EE template](#basic-graalvm-enterprise-edition-template)). | -| `java-version`
*(required)* | n/a | `'17'` or `'19'` for a specific Java version, `'dev'` for the highest Java version available (requires `version: 'dev'`).
(`'8'`, `'11'`, `'16'` are supported for older GraalVM releases.) | -| `components` | `''` | Comma-spearated list of GraalVM components (e.g., `native-image` or `ruby,nodejs`) that will be installed by the [GraalVM Updater][gu]. | -| `github-token` | `'${{ github.token }}'` | Token for communication with the GitHub API. Please set to `${{ secrets.GITHUB_TOKEN }}` (see [templates](#templates)) to allow the action to authenticate with the GitHub API, which helps to reduce rate limiting issues. | +| `java-version`
*(required)* | n/a | `'17.0.7'` or `'20.0.1'` for a specific Java version, `'dev'` for a dev build with the highest Java version available.
(`'8'`, `'11'`, `'16'`, `'19'` are supported for older GraalVM releases.) | +| `distribution` | `''` | GraalVM distribution (`graalvm` for Oracle GraalVM, `graalvm-community` for GraalVM Community Edition, `mandrel` for Mandrel). | +| `github-token` | `'${{ github.token }}'` | Token for communication with the GitHub API. Please set this to `${{ secrets.GITHUB_TOKEN }}` (see [templates](#templates)) to allow the action to authenticate with the GitHub API, which helps reduce rate-limiting issues. | | `set-java-home` | `'true'` | If set to `'true'`, instructs the action to set `$JAVA_HOME` to the path of the GraalVM installation. Overrides any previous action or command that sets `$JAVA_HOME`. | | `cache` | `''` | Name of the build platform to cache dependencies. It can be `'maven'`, `'gradle'`, or `'sbt'` and works the same way as described in [actions/setup-java][setup-java-caching]. | -| `check-for-updates` | `'true'` | [Annotate jobs][gha-annotations] with update notifications, for example, when a new GraalVM release is available. | +| `check-for-updates` | `'true'` | [Annotate jobs][gha-annotations] with update notifications, for example when a new GraalVM release is available. | | `native-image-musl` | `'false'` | If set to `'true'`, sets up [musl] to build [static binaries][native-image-static] with GraalVM Native Image *(Linux only)*. [Example usage][native-image-musl-build] (be sure to replace `uses: ./` with `uses: graalvm/setup-graalvm@v1`). | | `native-image-job-reports` *) | `'false'` | If set to `'true'`, post a job summary containing a Native Image build report. | | `native-image-pr-reports` *) | `'false'` | If set to `'true'`, post a comment containing a Native Image build report on pull requests. Requires `write` permissions for the [`pull-requests` scope][gha-permissions]. | +| `components` | `''` | Comma-separated list of GraalVM components (e.g., `native-image` or `ruby,nodejs`) that will be installed by the [GraalVM Updater][gu]. | +| `version` | n/a | `X.Y.Z` (e.g., `22.3.0`) for a specific [GraalVM release][releases] up to `22.3.2`
`mandrel-X.Y.Z` (e.g., `mandrel-21.3.0.0-Final`) for a specific [Mandrel release][mandrel-releases],
`mandrel-latest` for [latest Mandrel stable release][mandrel-stable]. | +| `gds-token` | `''` | Download token for the GraalVM Download Service. If a non-empty token is provided, the action will set up GraalVM Enterprise Edition (see [GraalVM EE template](#template-for-graalvm-enterprise-edition)). | **) Make sure that Native Image is used only once per build job. Otherwise, the report is only generated for the last Native Image build.* @@ -143,6 +196,8 @@ Only pull requests from committers that can be verified as having signed the OCA [gha-secrets]: https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository [gha-self-hosted-runners]: https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners [gu]: https://www.graalvm.org/reference-manual/graalvm-updater/ +[graalvm]: https://www.graalvm.org/ +[graalvm-dl]: https://www.oracle.com/java/technologies/downloads/ [graalvm-ee]: https://www.oracle.com/downloads/graalvm-downloads.html [mandrel]: https://github.com/graalvm/mandrel [mandrel-releases]: https://github.com/graalvm/mandrel/releases diff --git a/__tests__/gds.test.ts b/__tests__/gds.test.ts index 0dd12aa..b5b8911 100644 --- a/__tests__/gds.test.ts +++ b/__tests__/gds.test.ts @@ -1,5 +1,5 @@ import * as path from 'path' -import {downloadGraalVMEE, fetchArtifact} from '../src/gds' +import {downloadGraalVMEELegacy, fetchArtifact} from '../src/gds' import {expect, test} from '@jest/globals' const TEST_USER_AGENT = 'GraalVMGitHubActionTest/1.0.4' @@ -32,13 +32,15 @@ test('fetch artifacts', async () => { }) test('errors when downloading artifacts', async () => { - await expect(downloadGraalVMEE('invalid', '22.1.0', '11')).rejects.toThrow( + await expect( + downloadGraalVMEELegacy('invalid', '22.1.0', '11') + ).rejects.toThrow( 'The provided "gds-token" was rejected (reason: "Invalid download token", opc-request-id: /' ) - await expect(downloadGraalVMEE('invalid', '1.0.0', '11')).rejects.toThrow( - 'Unable to find JDK11-based GraalVM EE 1.0.0' - ) - await expect(downloadGraalVMEE('invalid', '22.1.0', '1')).rejects.toThrow( - 'Unable to find JDK1-based GraalVM EE 22.1.0' - ) + await expect( + downloadGraalVMEELegacy('invalid', '1.0.0', '11') + ).rejects.toThrow('Unable to find JDK11-based GraalVM EE 1.0.0') + await expect( + downloadGraalVMEELegacy('invalid', '22.1.0', '1') + ).rejects.toThrow('Unable to find JDK1-based GraalVM EE 22.1.0') }) diff --git a/__tests__/graalvm.test.ts b/__tests__/graalvm.test.ts index 61fd01e..aee238b 100644 --- a/__tests__/graalvm.test.ts +++ b/__tests__/graalvm.test.ts @@ -19,7 +19,7 @@ test('request invalid version/javaVersion', async () => { await graalvm.setUpGraalVMRelease('', combination[0], combination[1]) } catch (err) { if (!(err instanceof Error)) { - fail(`Unexpected non-Erro: ${err}`) + fail(`Unexpected non-Error: ${err}`) } error = err } @@ -31,6 +31,23 @@ test('request invalid version/javaVersion', async () => { }) test('find version/javaVersion', async () => { + // Make sure the action can find the latest Java version for known major versions + for (var majorJavaVersion of ['17', '20']) { + await graalvm.findLatestGraalVMJDKCEJavaVersion(majorJavaVersion) + } + + let error = new Error('unexpected') + try { + await graalvm.findLatestGraalVMJDKCEJavaVersion('11') + fail('Should not find Java version for 11') + } catch (err) { + if (!(err instanceof Error)) { + fail(`Unexpected non-Error: ${err}`) + } + error = err + } + expect(error.message).toContain('Unable to find the latest Java version for') + const latestRelease = await getTaggedRelease( GRAALVM_RELEASES_REPO, 'vm-22.3.1' @@ -40,7 +57,7 @@ test('find version/javaVersion', async () => { const latestJavaVersion = findHighestJavaVersion(latestRelease, latestVersion) expect(latestJavaVersion).not.toBe('') - let error = new Error('unexpected') + error = new Error('unexpected') try { const invalidRelease = {...latestRelease, tag_name: 'invalid'} findGraalVMVersion(invalidRelease) @@ -56,7 +73,7 @@ test('find version/javaVersion', async () => { findHighestJavaVersion(latestRelease, 'invalid') } catch (err) { if (!(err instanceof Error)) { - fail(`Unexpected non-Erro: ${err}`) + fail(`Unexpected non-Error: ${err}`) } error = err } diff --git a/action.yml b/action.yml index be98603..1eac87a 100644 --- a/action.yml +++ b/action.yml @@ -1,19 +1,17 @@ name: 'GitHub Action for GraalVM' -description: 'Set up a specific version of GraalVM Community Edition (CE) or Enterprise Edition (EE)' +description: 'Set up a specific version of the GraalVM JDK and add the command-line tools to the PATH' author: 'GraalVM Community' branding: icon: 'terminal' color: 'blue' inputs: - version: - required: true - description: 'GraalVM version (release, latest, dev).' - gds-token: - required: false - description: 'Download token for the GraalVM Download Service. If provided, the action will set up GraalVM Enterprise Edition.' java-version: required: true - description: 'Java version (11 or 17, 8 or 16 for older releases).' + description: 'Java version. See examples of supported syntax in the README file.' + distribution: + description: 'GraalVM distribution. See the list of available distributions in the README file.' + required: false + default: '' components: required: false description: 'Comma-separated list of GraalVM components to be installed.' @@ -45,6 +43,13 @@ inputs: required: false description: 'Post a comment containing a Native Image build report on pull requests.' default: 'false' + version: + required: false + description: 'GraalVM version (release, latest, dev).' + default: '' + gds-token: + required: false + description: 'Download token for the GraalVM Download Service. If provided, the action will set up GraalVM Enterprise Edition.' outputs: cache-hit: description: 'A boolean value to indicate an exact match was found for the primary key' diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index d3a90a8..6f01e1d 100644 Binary files a/dist/cleanup/index.js and b/dist/cleanup/index.js differ diff --git a/dist/main/index.js b/dist/main/index.js index 9214371..00b9347 100644 Binary files a/dist/main/index.js and b/dist/main/index.js differ diff --git a/src/constants.ts b/src/constants.ts index e757795..a021f0e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -3,6 +3,7 @@ import * as otypes from '@octokit/types' export const INPUT_VERSION = 'version' export const INPUT_GDS_TOKEN = 'gds-token' export const INPUT_JAVA_VERSION = 'java-version' +export const INPUT_DISTRIBUTION = 'distribution' export const INPUT_COMPONENTS = 'components' export const INPUT_GITHUB_TOKEN = 'github-token' export const INPUT_SET_JAVA_HOME = 'set-java-home' @@ -14,15 +15,22 @@ export const IS_LINUX = process.platform === 'linux' export const IS_MACOS = process.platform === 'darwin' export const IS_WINDOWS = process.platform === 'win32' +export const DISTRIBUTION_GRAALVM = 'graalvm' +export const DISTRIBUTION_GRAALVM_COMMUNITY = 'graalvm-community' +export const DISTRIBUTION_MANDREL = 'mandrel' + export const VERSION_DEV = 'dev' export const VERSION_LATEST = 'latest' +export const JDK_ARCH = determineJDKArchitecture() +export const JDK_PLATFORM = determineJDKPlatform() +export const JDK_HOME_SUFFIX = IS_MACOS ? '/Contents/Home' : '' + export const GRAALVM_ARCH = determineGraalVMArchitecture() export const GRAALVM_FILE_EXTENSION = IS_WINDOWS ? '.zip' : '.tar.gz' export const GRAALVM_GH_USER = 'graalvm' export const GRAALVM_PLATFORM = IS_WINDOWS ? 'windows' : process.platform export const GRAALVM_RELEASES_REPO = 'graalvm-ce-builds' -export const JDK_HOME_SUFFIX = IS_MACOS ? '/Contents/Home' : '' export const MANDREL_NAMESPACE = 'mandrel-' @@ -32,9 +40,46 @@ export const GDS_GRAALVM_PRODUCT_ID = 'D53FAE8052773FFAE0530F15000AA6C6' export const ENV_GITHUB_EVENT_NAME = 'GITHUB_EVENT_NAME' export const EVENT_NAME_PULL_REQUEST = 'pull_request' +export const ERROR_HINT = + 'If you think this is a mistake, please file an issue at: https://github.com/graalvm/setup-graalvm/issues.' + export type LatestReleaseResponse = otypes.Endpoints['GET /repos/{owner}/{repo}/releases/latest']['response'] +export type MatchingRefsResponse = + otypes.Endpoints['GET /repos/{owner}/{repo}/git/matching-refs/{ref}']['response'] + +function determineJDKArchitecture(): string { + switch (process.arch) { + case 'x64': { + return 'x64' + } + case 'arm64': { + return 'aarch64' + } + default: { + throw new Error(`Unsupported architecture: ${process.arch}`) + } + } +} + +function determineJDKPlatform(): string { + switch (process.platform) { + case 'linux': { + return 'linux' + } + case 'darwin': { + return 'macos' + } + case 'win32': { + return 'windows' + } + default: { + throw new Error(`Unsupported platform: ${process.platform}`) + } + } +} + function determineGraalVMArchitecture(): string { switch (process.arch) { case 'x64': { diff --git a/src/features/check-for-updates.ts b/src/features/check-for-updates.ts index a10395a..8dec9d4 100644 --- a/src/features/check-for-updates.ts +++ b/src/features/check-for-updates.ts @@ -8,6 +8,17 @@ export async function checkForUpdates( graalVMVersion: string, javaVersion: string ): Promise { + if ( + graalVMVersion.length > 0 && + (javaVersion === '17' || javaVersion === '19') + ) { + const recommendedJDK = javaVersion === '17' ? '17' : '20' + core.notice( + `A new GraalVM release is available! Please consider upgrading to GraalVM for JDK ${recommendedJDK}. Release notes: https://www.graalvm.org/release-notes/JDK_${recommendedJDK}/` + ) + return + } + if (graalVMVersion.startsWith('22.3.') && javaVersion === '11') { core.notice( 'Please consider upgrading your project to Java 17+. GraalVM 22.3.X releases are the last to support JDK11: https://github.com/oracle/graal/issues/5063' diff --git a/src/features/reports.ts b/src/features/reports.ts index 3533057..55b7932 100644 --- a/src/features/reports.ts +++ b/src/features/reports.ts @@ -88,6 +88,7 @@ export async function setUpNativeImageBuildReports( const isSupported = graalVMVersion === c.VERSION_LATEST || graalVMVersion === c.VERSION_DEV || + graalVMVersion.length === 0 || (!graalVMVersion.startsWith(c.MANDREL_NAMESPACE) && gte(toSemVer(graalVMVersion), '22.2.0')) if (!isSupported) { diff --git a/src/gds.ts b/src/gds.ts index 624fabf..a6eaf9b 100644 --- a/src/gds.ts +++ b/src/gds.ts @@ -27,7 +27,7 @@ interface GDSErrorResponse { readonly message: string } -export async function downloadGraalVMEE( +export async function downloadGraalVMEELegacy( gdsToken: string, version: string, javaVersion: string diff --git a/src/graalvm.ts b/src/graalvm.ts index df092df..ab904d2 100644 --- a/src/graalvm.ts +++ b/src/graalvm.ts @@ -3,78 +3,129 @@ import { downloadAndExtractJDK, downloadExtractAndCacheJDK, getLatestRelease, + getMatchingTags, getTaggedRelease } from './utils' -import {downloadGraalVMEE} from './gds' +import {downloadGraalVMEELegacy} from './gds' import {downloadTool} from '@actions/tool-cache' +import {basename} from 'path' +import {gt} from 'semver' +const GRAALVM_DL_BASE = 'https://download.oracle.com/graalvm' const GRAALVM_CE_DL_BASE = `https://github.com/graalvm/${c.GRAALVM_RELEASES_REPO}/releases/download` const GRAALVM_REPO_DEV_BUILDS = 'graalvm-ce-dev-builds' +const GRAALVM_JDK_TAG_PREFIX = 'jdk-' const GRAALVM_TAG_PREFIX = 'vm-' -export async function setUpGraalVMLatest( - gdsToken: string, - javaVersion: string -): Promise { - const lockedVersion = '22.3.1' - if (gdsToken.length > 0) { - return setUpGraalVMRelease(gdsToken, lockedVersion, javaVersion) - } - const latestRelease = await getTaggedRelease( - c.GRAALVM_RELEASES_REPO, - GRAALVM_TAG_PREFIX + lockedVersion - ) - const version = findGraalVMVersion(latestRelease) - return setUpGraalVMRelease(gdsToken, version, javaVersion) -} +// Support for GraalVM for JDK 17 and later -export function findGraalVMVersion(release: c.LatestReleaseResponse['data']) { - const tag_name = release.tag_name - if (!tag_name.startsWith(GRAALVM_TAG_PREFIX)) { - throw new Error(`Could not find latest GraalVM release: ${tag_name}`) - } - return tag_name.substring(GRAALVM_TAG_PREFIX.length, tag_name.length) -} - -export async function setUpGraalVMDevBuild( - gdsToken: string, - javaVersion: string +export async function setUpGraalVMJDK( + javaVersionOrDev: string ): Promise { - if (gdsToken.length > 0) { - throw new Error('Downloading GraalVM EE dev builds is not supported') + if (javaVersionOrDev === c.VERSION_DEV) { + return setUpGraalVMJDKDevBuild() } - const latestDevBuild = await getLatestRelease(GRAALVM_REPO_DEV_BUILDS) - let resolvedJavaVersion - if (javaVersion == c.VERSION_DEV) { - resolvedJavaVersion = findHighestJavaVersion(latestDevBuild, c.VERSION_DEV) + const javaVersion = javaVersionOrDev + const toolName = determineToolName(javaVersion, false) + let downloadUrl: string + if (javaVersion.includes('.')) { + const majorJavaVersion = javaVersion.split('.')[0] + downloadUrl = `${GRAALVM_DL_BASE}/${majorJavaVersion}/archive/${toolName}${c.GRAALVM_FILE_EXTENSION}` } else { - resolvedJavaVersion = javaVersion + downloadUrl = `${GRAALVM_DL_BASE}/${javaVersion}/latest/${toolName}${c.GRAALVM_FILE_EXTENSION}` } + const downloader = async () => downloadGraalVMJDK(downloadUrl, javaVersion) + return downloadExtractAndCacheJDK(downloader, toolName, javaVersion) +} + +export async function setUpGraalVMJDKCE( + javaVersionOrDev: string +): Promise { + if (javaVersionOrDev === c.VERSION_DEV) { + return setUpGraalVMJDKDevBuild() + } + let javaVersion = javaVersionOrDev + if (!javaVersion.includes('.')) { + javaVersion = await findLatestGraalVMJDKCEJavaVersion(javaVersion) + } + if (javaVersion.split('.').length != 3) { + throw new Error( + `java-version set to '${javaVersionOrDev}', which was resolved to '${javaVersion}'. Please make sure the java-version is set correctly. ${c.ERROR_HINT}` + ) + } + const toolName = determineToolName(javaVersion, true) + const downloadUrl = `${GRAALVM_CE_DL_BASE}/jdk-${javaVersion}/${toolName}${c.GRAALVM_FILE_EXTENSION}` + const downloader = async () => downloadGraalVMJDK(downloadUrl, javaVersion) + return downloadExtractAndCacheJDK(downloader, toolName, javaVersion) +} + +export async function findLatestGraalVMJDKCEJavaVersion( + majorJavaVersion: string +): Promise { + const matchingRefs = await getMatchingTags( + `${GRAALVM_JDK_TAG_PREFIX}${majorJavaVersion}` + ) + const lowestNonExistingVersion = '0.0.1' + let highestVersion = lowestNonExistingVersion + const versionNumberStartIndex = `refs/tags/${GRAALVM_JDK_TAG_PREFIX}`.length + for (const matchingRef of matchingRefs) { + const currentVersion = matchingRef.ref.substring(versionNumberStartIndex) + if (gt(currentVersion, highestVersion)) { + highestVersion = currentVersion + } + } + if (highestVersion === lowestNonExistingVersion) { + throw new Error( + `Unable to find the latest Java version for '${majorJavaVersion}'. Please make sure the java-version is set correctly. ${c.ERROR_HINT}` + ) + } + return highestVersion +} + +function determineToolName(javaVersion: string, isCommunity: boolean) { + return `graalvm${isCommunity ? '-community' : ''}-jdk-${javaVersion}_${ + c.JDK_PLATFORM + }-${c.JDK_ARCH}_bin` +} + +async function downloadGraalVMJDK( + downloadUrl: string, + javaVersion: string +): Promise { + try { + return await downloadTool(downloadUrl) + } catch (error) { + if (error instanceof Error && error.message.includes('404')) { + // Not Found + throw new Error( + `Failed to download ${basename( + downloadUrl + )}. Are you sure java-version: '${javaVersion}' is correct?` + ) + } + throw new Error( + `Failed to download ${basename(downloadUrl)} (error: ${error}).` + ) + } +} + +// Support for GraalVM dev builds + +export async function setUpGraalVMJDKDevBuild(): Promise { + const latestDevBuild = await getLatestRelease(GRAALVM_REPO_DEV_BUILDS) + const resolvedJavaVersion = findHighestJavaVersion( + latestDevBuild, + c.VERSION_DEV + ) const downloadUrl = findDownloadUrl(latestDevBuild, resolvedJavaVersion) return downloadAndExtractJDK(downloadUrl) } -export async function setUpGraalVMRelease( - gdsToken: string, - version: string, - javaVersion: string -): Promise { - const isEE = gdsToken.length > 0 - const toolName = determineToolName(isEE, version, javaVersion) - let downloader: () => Promise - if (isEE) { - downloader = async () => downloadGraalVMEE(gdsToken, version, javaVersion) - } else { - downloader = async () => downloadGraalVMCE(version, javaVersion) - } - return downloadExtractAndCacheJDK(downloader, toolName, version) -} - export function findHighestJavaVersion( release: c.LatestReleaseResponse['data'], version: string ): string { - const graalVMIdentifierPattern = determineGraalVMIdentifier( + const graalVMIdentifierPattern = determineGraalVMLegacyIdentifier( false, version, '(\\d+)' @@ -104,11 +155,54 @@ export function findHighestJavaVersion( } } +// Support for GraalVM 22.X releases and earlier + +export async function setUpGraalVMLatest( + gdsToken: string, + javaVersion: string +): Promise { + const lockedVersion = '22.3.1' + if (gdsToken.length > 0) { + return setUpGraalVMRelease(gdsToken, lockedVersion, javaVersion) + } + const latestRelease = await getTaggedRelease( + c.GRAALVM_RELEASES_REPO, + GRAALVM_TAG_PREFIX + lockedVersion + ) + const version = findGraalVMVersion(latestRelease) + return setUpGraalVMRelease(gdsToken, version, javaVersion) +} + +export function findGraalVMVersion(release: c.LatestReleaseResponse['data']) { + const tag_name = release.tag_name + if (!tag_name.startsWith(GRAALVM_TAG_PREFIX)) { + throw new Error(`Could not find latest GraalVM release: ${tag_name}`) + } + return tag_name.substring(GRAALVM_TAG_PREFIX.length, tag_name.length) +} + +export async function setUpGraalVMRelease( + gdsToken: string, + version: string, + javaVersion: string +): Promise { + const isEE = gdsToken.length > 0 + const toolName = determineLegacyToolName(isEE, version, javaVersion) + let downloader: () => Promise + if (isEE) { + downloader = async () => + downloadGraalVMEELegacy(gdsToken, version, javaVersion) + } else { + downloader = async () => downloadGraalVMCELegacy(version, javaVersion) + } + return downloadExtractAndCacheJDK(downloader, toolName, version) +} + function findDownloadUrl( release: c.LatestReleaseResponse['data'], javaVersion: string ): string { - const graalVMIdentifier = determineGraalVMIdentifier( + const graalVMIdentifier = determineGraalVMLegacyIdentifier( false, c.VERSION_DEV, javaVersion @@ -120,21 +214,21 @@ function findDownloadUrl( } } throw new Error( - `Could not find GraalVM dev build for Java ${javaVersion}. It may no longer be available, so please consider upgrading the Java version. If you think this is a mistake, please file an issue at: https://github.com/graalvm/setup-graalvm/issues.` + `Could not find GraalVM dev build for Java ${javaVersion}. It may no longer be available, so please consider upgrading the Java version. ${c.ERROR_HINT}` ) } -function determineGraalVMIdentifier( +function determineGraalVMLegacyIdentifier( isEE: boolean, version: string, javaVersion: string ): string { - return `${determineToolName(isEE, version, javaVersion)}-${ + return `${determineLegacyToolName(isEE, version, javaVersion)}-${ c.GRAALVM_ARCH }-${version}` } -function determineToolName( +function determineLegacyToolName( isEE: boolean, version: string, javaVersion: string @@ -143,11 +237,11 @@ function determineToolName( return `graalvm-${infix}-java${javaVersion}-${c.GRAALVM_PLATFORM}` } -async function downloadGraalVMCE( +async function downloadGraalVMCELegacy( version: string, javaVersion: string ): Promise { - const graalVMIdentifier = determineGraalVMIdentifier( + const graalVMIdentifier = determineGraalVMLegacyIdentifier( false, version, javaVersion diff --git a/src/main.ts b/src/main.ts index 6579716..b0f6936 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,9 +14,10 @@ import {setUpNativeImageBuildReports} from './features/reports' async function run(): Promise { try { - const graalvmVersion = core.getInput(c.INPUT_VERSION, {required: true}) - const gdsToken = core.getInput(c.INPUT_GDS_TOKEN) const javaVersion = core.getInput(c.INPUT_JAVA_VERSION, {required: true}) + const distribution = core.getInput(c.INPUT_DISTRIBUTION) + const graalvmVersion = core.getInput(c.INPUT_VERSION) + const gdsToken = core.getInput(c.INPUT_GDS_TOKEN) const componentsString: string = core.getInput(c.INPUT_COMPONENTS) const components: string[] = componentsString.length > 0 @@ -38,27 +39,71 @@ async function run(): Promise { // Download or build GraalVM let graalVMHome - switch (graalvmVersion) { - case c.VERSION_LATEST: - graalVMHome = await graalvm.setUpGraalVMLatest(gdsToken, javaVersion) - break - case c.VERSION_DEV: - graalVMHome = await graalvm.setUpGraalVMDevBuild(gdsToken, javaVersion) - break - default: - if (graalvmVersion.startsWith(c.MANDREL_NAMESPACE)) { - graalVMHome = await setUpMandrel(graalvmVersion, javaVersion) - } else { - if (enableCheckForUpdates) { - await checkForUpdates(graalvmVersion, javaVersion) - } - graalVMHome = await graalvm.setUpGraalVMRelease( - gdsToken, - graalvmVersion, - javaVersion + if (distribution.length > 0 || graalvmVersion.length == 0) { + switch (distribution) { + case c.DISTRIBUTION_GRAALVM: + graalVMHome = await graalvm.setUpGraalVMJDK(javaVersion) + break + case c.DISTRIBUTION_GRAALVM_COMMUNITY: + graalVMHome = await graalvm.setUpGraalVMJDKCE(javaVersion) + break + case c.DISTRIBUTION_MANDREL: + throw new Error( + `Mandrel requires the 'version' option (see https://github.com/graalvm/setup-graalvm/tree/main#options).` ) - } - break + case '': + if (javaVersion === c.VERSION_DEV) { + core.info( + `This build is using the GraalVM Community Edition. To select a specific distribution, use the 'distribution' option (see https://github.com/graalvm/setup-graalvm/tree/main#options).` + ) + graalVMHome = await graalvm.setUpGraalVMJDKDevBuild() + } else { + core.info( + `This build is using the new Oracle GraalVM. To select a specific distribution, use the 'distribution' option (see https://github.com/graalvm/setup-graalvm/tree/main#options).` + ) + graalVMHome = await graalvm.setUpGraalVMJDK(javaVersion) + } + default: + throw new Error(`Unsupported distribution: ${distribution}`) + } + } else { + switch (graalvmVersion) { + case c.VERSION_LATEST: + if (javaVersion.startsWith('17') || javaVersion.startsWith('20')) { + core.info( + `This build is using the new Oracle GraalVM. To select a specific distribution, use the 'distribution' option (see https://github.com/graalvm/setup-graalvm/tree/main#options).` + ) + graalVMHome = await graalvm.setUpGraalVMJDK(javaVersion) + } else { + graalVMHome = await graalvm.setUpGraalVMLatest( + gdsToken, + javaVersion + ) + } + break + case c.VERSION_DEV: + if (gdsToken.length > 0) { + throw new Error( + 'Downloading GraalVM EE dev builds is not supported' + ) + } + graalVMHome = await graalvm.setUpGraalVMJDKDevBuild() + break + default: + if (graalvmVersion.startsWith(c.MANDREL_NAMESPACE)) { + graalVMHome = await setUpMandrel(graalvmVersion, javaVersion) + } else { + if (enableCheckForUpdates) { + await checkForUpdates(graalvmVersion, javaVersion) + } + graalVMHome = await graalvm.setUpGraalVMRelease( + gdsToken, + graalvmVersion, + javaVersion + ) + } + break + } } // Activate GraalVM diff --git a/src/utils.ts b/src/utils.ts index 88f2b76..0288dad 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -63,6 +63,24 @@ export async function getTaggedRelease( ).data } +export async function getMatchingTags( + tagPrefix: string +): Promise { + const githubToken = getGitHubToken() + const options = githubToken.length > 0 ? {auth: githubToken} : {} + const octokit = new GitHubDotCom(options) + return ( + await octokit.request( + 'GET /repos/{owner}/{repo}/git/matching-refs/tags/{tagPrefix}', + { + owner: c.GRAALVM_GH_USER, + repo: c.GRAALVM_RELEASES_REPO, + tagPrefix + } + ) + ).data +} + export async function downloadAndExtractJDK( downloadUrl: string ): Promise {