Skip to content

Commit

Permalink
Add support for http url in jdkFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mdvorak committed Jan 17, 2022
1 parent f5cbdad commit d875d99
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/e2e-local-file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions __tests__/distributors/local-installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 15 additions & 0 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions src/distributions/local/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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}'`);
}
Expand Down

0 comments on commit d875d99

Please sign in to comment.