actions/setup-java@v2 - Support different distributions (#132)

* Implement support for custom vendors in setup-java

* minor improvements

* minor refactoring

* Add unit tests and e2e tests

* Update documentation for setup-java@v2 release

* minor improvements

* regenerate dist

* fix comments

* resolve comments

* resolve comments

* fix tests

* Update README.md

Co-authored-by: George Adams <george.adams@microsoft.com>

* Apply suggestions from code review

Co-authored-by: Konrad Pabjan <konradpabjan@github.com>

* fix minor nitpicks

* handle 4th digit

* pull latest main

* Update README.md

* rename adoptium to adopt

* rename adoptium to adopt

* rename adoptium to adopt

* Update README.md

* make java-version and distribution required for action

* update readme

* fix tests

* fix e2e tests

Co-authored-by: George Adams <george.adams@microsoft.com>
Co-authored-by: Konrad Pabjan <konradpabjan@github.com>
This commit is contained in:
Maxim Lobanov
2021-03-15 13:39:46 +03:00
committed by GitHub
parent e73e96a93b
commit 7c88894700
44 changed files with 40141 additions and 22003 deletions

View File

@@ -1,27 +1,47 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as core from '@actions/core';
import * as io from '@actions/io';
import {create as xmlCreate} from 'xmlbuilder2';
import * as fs from 'fs';
import * as os from 'os';
import { create as xmlCreate } from 'xmlbuilder2';
import * as constants from './constants';
import * as gpg from './gpg';
export const M2_DIR = '.m2';
export const SETTINGS_FILE = 'settings.xml';
export async function configAuthentication(
export async function configureAuthentication() {
const id = core.getInput(constants.INPUT_SERVER_ID);
const username = core.getInput(constants.INPUT_SERVER_USERNAME);
const password = core.getInput(constants.INPUT_SERVER_PASSWORD);
const gpgPrivateKey =
core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY;
const gpgPassphrase =
core.getInput(constants.INPUT_GPG_PASSPHRASE) ||
(gpgPrivateKey ? constants.INPUT_DEFAULT_GPG_PASSPHRASE : undefined);
if (gpgPrivateKey) {
core.setSecret(gpgPrivateKey);
}
await createAuthenticationSettings(id, username, password, gpgPassphrase);
if (gpgPrivateKey) {
core.info('Importing private gpg key');
const keyFingerprint = (await gpg.importKey(gpgPrivateKey)) || '';
core.saveState(constants.STATE_GPG_PRIVATE_KEY_FINGERPRINT, keyFingerprint);
}
}
export async function createAuthenticationSettings(
id: string,
username: string,
password: string,
gpgPassphrase: string | undefined = undefined
) {
console.log(
`creating ${SETTINGS_FILE} with server-id: ${id};`,
'environment variables:',
`username=\$${username},`,
`password=\$${password},`,
`and gpg-passphrase=${gpgPassphrase ? '$' + gpgPassphrase : null}`
);
core.info(`Creating ${SETTINGS_FILE} with server-id: ${id}`);
// when an alternate m2 location is specified use only that location (no .m2 directory)
// otherwise use the home/.m2/ path
const settingsDirectory: string = path.join(
@@ -29,11 +49,7 @@ export async function configAuthentication(
core.getInput(constants.INPUT_SETTINGS_PATH) ? '' : M2_DIR
);
await io.mkdirP(settingsDirectory);
core.debug(`created directory ${settingsDirectory}`);
await write(
settingsDirectory,
generate(id, username, password, gpgPassphrase)
);
await write(settingsDirectory, generate(id, username, password, gpgPassphrase));
}
// only exported for testing purposes
@@ -41,9 +57,9 @@ export function generate(
id: string,
username: string,
password: string,
gpgPassphrase: string | undefined = undefined
gpgPassphrase?: string | undefined
) {
const xmlObj: {[key: string]: any} = {
const xmlObj: { [key: string]: any } = {
settings: {
'@xmlns': 'http://maven.apache.org/SETTINGS/1.0.0',
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
@@ -69,15 +85,19 @@ export function generate(
xmlObj.settings.servers.server.push(gpgServer);
}
return xmlCreate(xmlObj).end({headless: true, prettyPrint: true, width: 80});
return xmlCreate(xmlObj).end({
headless: true,
prettyPrint: true,
width: 80
});
}
async function write(directory: string, settings: string) {
const location = path.join(directory, SETTINGS_FILE);
if (fs.existsSync(location)) {
console.warn(`overwriting existing file ${location}`);
core.info(`Overwriting existing file ${location}`);
} else {
console.log(`writing ${location}`);
core.info(`Writing ${location}`);
}
return fs.writeFileSync(location, settings, {