From d875d9939146bf6fbd2e7acc53a7a411bcf2d762 Mon Sep 17 00:00:00 2001 From: Michal Dvorak Date: Mon, 17 Jan 2022 16:00:38 +0100 Subject: [PATCH] Add support for http url in jdkFile Closes #232 --- .github/workflows/e2e-local-file.yml | 36 +++++++++++++++++++ .../distributors/local-installer.test.ts | 30 ++++++++++++++++ action.yml | 2 +- docs/advanced-usage.md | 15 ++++++++ src/distributions/local/installer.ts | 13 +++++-- 5 files changed, 92 insertions(+), 4 deletions(-) diff --git a/.github/workflows/e2e-local-file.yml b/.github/workflows/e2e-local-file.yml index 4f4cd0327..baa026ae1 100644 --- a/.github/workflows/e2e-local-file.yml +++ b/.github/workflows/e2e-local-file.yml @@ -121,3 +121,39 @@ jobs: - name: Verify Java version run: bash __tests__/verify-java.sh "11.0.12" "${{ steps.setup-java.outputs.path }}" shell: bash + + setup-java-remote-file-temurin: + name: Validate download from remote file Eclipse Temurin + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest, windows-latest, ubuntu-latest] + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Download Eclipse Temurin file + run: | + if ($IsLinux) { + $downloadUrl = "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.12%2B7/OpenJDK11U-jdk_x64_linux_hotspot_11.0.12_7.tar.gz" + $localFilename = "java_package.tar.gz" + } elseif ($IsMacOS) { + $downloadUrl = "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.12%2B7/OpenJDK11U-jdk_x64_mac_hotspot_11.0.12_7.tar.gz" + $localFilename = "java_package.tar.gz" + } elseif ($IsWindows) { + $downloadUrl = "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.12%2B7/OpenJDK11U-jdk_x64_windows_hotspot_11.0.12_7.zip" + $localFilename = "java_package.zip" + } + echo "DownloadUrl=$downloadUrl" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append + shell: pwsh + - name: setup-java + uses: ./ + id: setup-java + with: + distribution: 'jdkfile' + jdkFile: ${{ env.DownloadUrl }} + java-version: '11.0.0-ea' + architecture: x64 + - name: Verify Java version + run: bash __tests__/verify-java.sh "11.0.12" "${{ steps.setup-java.outputs.path }}" + shell: bash diff --git a/__tests__/distributors/local-installer.test.ts b/__tests__/distributors/local-installer.test.ts index c1618971b..4a0caeb68 100644 --- a/__tests__/distributors/local-installer.test.ts +++ b/__tests__/distributors/local-installer.test.ts @@ -161,6 +161,36 @@ describe('setupJava', () => { ); }); + it('java is downloaded from remote jdkfile', async () => { + const inputs = { + version: '11.0.289', + architecture: 'x86', + packageType: 'jdk', + checkLatest: false + }; + const expected = { + version: '11.0.289', + path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture) + }; + + const jdkFile = 'https://example.com/jdk.tar.gz' + + const spyTcDownloadTool = jest.spyOn(tc, 'downloadTool'); + spyTcDownloadTool.mockReturnValue(Promise.resolve(expectedJdkFile)); + + mockJavaBase = new LocalDistribution(inputs, jdkFile); + await expect(mockJavaBase.setupJava()).resolves.toEqual(expected); + expect(spyTcFindAllVersions).toHaveBeenCalled(); + expect(spyTcDownloadTool).toHaveBeenCalledWith(jdkFile); + expect(spyCoreInfo).not.toHaveBeenCalledWith( + `Resolved Java ${actualJavaVersion} from tool-cache` + ); + expect(spyCoreInfo).toHaveBeenCalledWith(`Extracting Java from '${expectedJdkFile}'`); + expect(spyCoreInfo).toHaveBeenCalledWith( + `Java ${inputs.version} was not found in tool-cache. Trying to unpack JDK file...` + ); + }); + it('jdk file is not found', async () => { const inputs = { version: '11.0.289', diff --git a/action.yml b/action.yml index faad23c38..ae9bc9535 100644 --- a/action.yml +++ b/action.yml @@ -18,7 +18,7 @@ inputs: required: false default: 'x64' jdkFile: - description: 'Path to where the compressed JDK is located' + description: 'Path to where the compressed JDK is located, or URL where it should be downloaded from' required: false check-latest: description: 'Set this option if you want the action to check for the latest available version that satisfies the version spec' diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index a0b7aff7a..86391f188 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -120,6 +120,21 @@ steps: - run: java -cp java HelloWorldApp ``` +## Installing Java from remote archive +If your use-case requires a custom distribution or a version that is not provided by setup-java, you can specify URL directly and setup-java will take care of the installation and caching on the VM: + +```yaml +steps: +- uses: actions/setup-java@v2 + with: + distribution: 'jdkfile' + jdkFile: https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.10%2B9/OpenJDK11U-jdk_x64_linux_hotspot_11.0.10_9.tar.gz + java-version: '11.0.0' + architecture: x64 + +- run: java -cp java HelloWorldApp +``` + ## Testing against different Java distributions **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. ```yaml diff --git a/src/distributions/local/installer.ts b/src/distributions/local/installer.ts index 1402bc85b..cd835928d 100644 --- a/src/distributions/local/installer.ts +++ b/src/distributions/local/installer.ts @@ -3,7 +3,6 @@ import * as core from '@actions/core'; import fs from 'fs'; import path from 'path'; -import semver from 'semver'; import { JavaBase } from '../base-installer'; import { JavaInstallerOptions, JavaDownloadRelease, JavaInstallerResults } from '../base-models'; @@ -25,9 +24,17 @@ export class LocalDistribution extends JavaBase { if (!this.jdkFile) { throw new Error("'jdkFile' is not specified"); } - const jdkFilePath = path.resolve(this.jdkFile); - const stats = fs.statSync(jdkFilePath); + let jdkFilePath; + if (this.jdkFile.match(/^https?:\/\//)) { + core.info(`Downloading Java archive from ${this.jdkFile}...`); + const javaRelease = await tc.downloadTool(this.jdkFile); + jdkFilePath = path.resolve(javaRelease); + } else { + jdkFilePath = path.resolve(this.jdkFile); + } + + const stats = fs.statSync(jdkFilePath); if (!stats.isFile()) { throw new Error(`JDK file was not found in path '${jdkFilePath}'`); }