Add "overwrite-settings" input parameter (#136)

* add overwrite-settings parameter

* fix e2e tests

* print debug

* fix e2e tests

* add comment

* remove comment
This commit is contained in:
Maxim Lobanov 2021-03-17 11:04:00 +03:00 committed by GitHub
parent 7c88894700
commit 804a60faf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 184 additions and 37 deletions

View File

@ -11,6 +11,10 @@ on:
paths-ignore:
- '**.md'
defaults:
run:
shell: pwsh
jobs:
setup-java-publishing:
name: Validate settings.xml
@ -34,12 +38,7 @@ jobs:
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Validate settings.xml
run: |
$homePath = $env:USERPROFILE
if (-not $homePath) {
$homePath = $env:HOME
}
$xmlPath = Join-Path $homePath ".m2" "settings.xml"
$xmlPath = Join-Path $HOME ".m2" "settings.xml"
Get-Content $xmlPath | ForEach-Object { Write-Host $_ }
[xml]$xml = Get-Content $xmlPath
@ -51,10 +50,82 @@ jobs:
if (($servers[1].id -ne 'gpg.passphrase') -or ($servers[1].passphrase -ne '${env.MAVEN_GPG_PASSPHRASE}')) {
throw "Generated XML file is incorrect"
}
shell: pwsh
test-publishing-overwrite:
name: settings.xml is overwritten if flag is true
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Create fake settings.xml
run: |
$xmlDirectory = Join-Path $HOME ".m2"
$xmlPath = Join-Path $xmlDirectory "settings.xml"
New-Item -Path $xmlDirectory -ItemType Directory
Set-Content -Path $xmlPath -Value "Fake_XML"
- name: setup-java
uses: ./
id: setup-java
with:
distribution: 'adopt'
java-version: '11'
server-id: maven
server-username: MAVEN_USERNAME
server-password: MAVEN_CENTRAL_TOKEN
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Validate settings.xml is overwritten
run: |
$xmlPath = Join-Path $HOME ".m2" "settings.xml"
Get-Content $xmlPath | ForEach-Object { Write-Host $_ }
$content = Get-Content $xmlPath -Raw
if ($content -notlike '*maven*') {
throw "settings.xml file is not overwritten"
}
test-publishing-skip-overwrite:
name: settings.xml is not overwritten if flag is false
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Create fake settings.xml
run: |
$xmlDirectory = Join-Path $HOME ".m2"
$xmlPath = Join-Path $xmlDirectory "settings.xml"
New-Item -Path $xmlDirectory -ItemType Directory
Set-Content -Path $xmlPath -Value "Fake_XML"
- name: setup-java
uses: ./
id: setup-java
with:
distribution: 'adopt'
java-version: '11'
server-id: maven
server-username: MAVEN_USERNAME
server-password: MAVEN_CENTRAL_TOKEN
overwrite-settings: false
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Validate that settings.xml is not overwritten
run: |
$xmlPath = Join-Path $HOME ".m2" "settings.xml"
$content = Get-Content -Path $xmlPath -Raw
Write-Host $content
if ($content -notlike "*Fake_XML*") {
throw "settings.xml file was overwritten but it should not be"
}
test-publishing-custom-location:
name: Validate settings.xml in custom location
name: settings.xml in custom location
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
@ -80,4 +151,3 @@ jobs:
if (-not (Test-Path $path)) {
throw "settings.xml file is not found in expected location"
}
shell: pwsh

View File

@ -20,6 +20,7 @@ This action provides the following functionality for GitHub Actions runners:
Inputs `java-version` and `distribution` are mandatory. See [Supported distributions](../README.md#Supported-distributions) section for a list of available options.
### Basic
**Adopt OpenJDK**
```yaml
steps:
- uses: actions/checkout@v2
@ -30,6 +31,17 @@ steps:
- run: java -cp java HelloWorldApp
```
**Zulu OpenJDK**
```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2-preview
with:
distribution: 'zulu' # See 'Supported distributions' for available options
java-version: '11'
- run: java -cp java HelloWorldApp
```
#### Supported version syntax
The `java-version` input supports an exact version or a version range using [SemVer](https://semver.org/) notation:
- major versions: `8`, `11`, `15`

View File

@ -35,10 +35,9 @@ describe('auth tests', () => {
const altHome = path.join(__dirname, 'runner', 'settings');
const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE);
process.env[`INPUT_SETTINGS-PATH`] = altHome;
await io.rmRF(altHome); // ensure it doesn't already exist
await auth.createAuthenticationSettings(id, username, password);
await auth.createAuthenticationSettings(id, username, password, altHome, true);
expect(fs.existsSync(m2Dir)).toBe(false);
expect(fs.existsSync(settingsFile)).toBe(false);
@ -49,7 +48,6 @@ describe('auth tests', () => {
auth.generate(id, username, password)
);
delete process.env[`INPUT_SETTINGS-PATH`];
await io.rmRF(altHome);
}, 100000);
@ -58,7 +56,7 @@ describe('auth tests', () => {
const username = 'UNAME';
const password = 'TOKEN';
await auth.createAuthenticationSettings(id, username, password);
await auth.createAuthenticationSettings(id, username, password, m2Dir, true);
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
@ -71,7 +69,7 @@ describe('auth tests', () => {
const password = 'TOKEN';
const gpgPassphrase = 'GPG';
await auth.createAuthenticationSettings(id, username, password, gpgPassphrase);
await auth.createAuthenticationSettings(id, username, password, m2Dir, true, gpgPassphrase);
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
@ -90,13 +88,30 @@ describe('auth tests', () => {
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
await auth.createAuthenticationSettings(id, username, password);
await auth.createAuthenticationSettings(id, username, password, m2Dir, true);
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(auth.generate(id, username, password));
}, 100000);
it('does not overwrite existing settings.xml files', async () => {
const id = 'packages';
const username = 'USERNAME';
const password = 'PASSWORD';
fs.mkdirSync(m2Dir, { recursive: true });
fs.writeFileSync(settingsFile, 'FAKE FILE');
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
await auth.createAuthenticationSettings(id, username, password, m2Dir, false);
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual('FAKE FILE');
}, 100000);
it('generates valid settings.xml with minimal configuration', () => {
const id = 'packages';
const username = 'USER';

View File

@ -38,6 +38,10 @@ inputs:
settings-path:
description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
required: false
overwrite-settings:
description: 'Overwrite the settings.xml file if it exists. Default is "true".'
required: false
default: true
gpg-private-key:
description: 'GPG private key to import. Default is empty string.'
required: false

10
dist/cleanup/index.js vendored
View File

@ -2685,17 +2685,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getTempDir = void 0;
exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
const os_1 = __importDefault(__webpack_require__(87));
const path_1 = __importDefault(__webpack_require__(622));
const fs = __importStar(__webpack_require__(747));
const semver = __importStar(__webpack_require__(876));
const core = __importStar(__webpack_require__(470));
const tc = __importStar(__webpack_require__(533));
function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || os_1.default.tmpdir();
return tempDirectory;
}
exports.getTempDir = getTempDir;
function getBooleanInput(inputName, defaultValue = false) {
return (core.getInput(inputName) || String(defaultValue)).toUpperCase() === 'TRUE';
}
exports.getBooleanInput = getBooleanInput;
function getVersionFromToolcachePath(toolPath) {
if (toolPath) {
return path_1.default.basename(path_1.default.dirname(toolPath));
@ -6824,7 +6829,7 @@ function isUnixExecutable(stats) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0;
exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0;
exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home';
exports.INPUT_JAVA_VERSION = 'java-version';
exports.INPUT_ARCHITECTURE = 'architecture';
@ -6835,6 +6840,7 @@ exports.INPUT_SERVER_ID = 'server-id';
exports.INPUT_SERVER_USERNAME = 'server-username';
exports.INPUT_SERVER_PASSWORD = 'server-password';
exports.INPUT_SETTINGS_PATH = 'settings-path';
exports.INPUT_OVERWRITE_SETTINGS = 'overwrite-settings';
exports.INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase';
exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined;

31
dist/setup/index.js vendored
View File

@ -11302,7 +11302,7 @@ exports.HTMLCollectionImpl = HTMLCollectionImpl;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0;
exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0;
exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home';
exports.INPUT_JAVA_VERSION = 'java-version';
exports.INPUT_ARCHITECTURE = 'architecture';
@ -11313,6 +11313,7 @@ exports.INPUT_SERVER_ID = 'server-id';
exports.INPUT_SERVER_USERNAME = 'server-username';
exports.INPUT_SERVER_PASSWORD = 'server-password';
exports.INPUT_SETTINGS_PATH = 'settings-path';
exports.INPUT_OVERWRITE_SETTINGS = 'overwrite-settings';
exports.INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase';
exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined;
@ -12935,17 +12936,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getTempDir = void 0;
exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
const os_1 = __importDefault(__webpack_require__(87));
const path_1 = __importDefault(__webpack_require__(622));
const fs = __importStar(__webpack_require__(747));
const semver = __importStar(__webpack_require__(876));
const core = __importStar(__webpack_require__(470));
const tc = __importStar(__webpack_require__(139));
function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || os_1.default.tmpdir();
return tempDirectory;
}
exports.getTempDir = getTempDir;
function getBooleanInput(inputName, defaultValue = false) {
return (core.getInput(inputName) || String(defaultValue)).toUpperCase() === 'TRUE';
}
exports.getBooleanInput = getBooleanInput;
function getVersionFromToolcachePath(toolPath) {
if (toolPath) {
return path_1.default.basename(path_1.default.dirname(toolPath));
@ -13257,6 +13263,7 @@ const os = __importStar(__webpack_require__(87));
const xmlbuilder2_1 = __webpack_require__(255);
const constants = __importStar(__webpack_require__(211));
const gpg = __importStar(__webpack_require__(884));
const util_1 = __webpack_require__(322);
exports.M2_DIR = '.m2';
exports.SETTINGS_FILE = 'settings.xml';
function configureAuthentication() {
@ -13264,13 +13271,15 @@ 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 settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), exports.M2_DIR);
const overwriteSettings = util_1.getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true);
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);
}
yield createAuthenticationSettings(id, username, password, gpgPassphrase);
yield createAuthenticationSettings(id, username, password, settingsDirectory, overwriteSettings, gpgPassphrase);
if (gpgPrivateKey) {
core.info('Importing private gpg key');
const keyFingerprint = (yield gpg.importKey(gpgPrivateKey)) || '';
@ -13279,14 +13288,13 @@ function configureAuthentication() {
});
}
exports.configureAuthentication = configureAuthentication;
function createAuthenticationSettings(id, username, password, gpgPassphrase = undefined) {
function createAuthenticationSettings(id, username, password, settingsDirectory, overwriteSettings, gpgPassphrase = undefined) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Creating ${exports.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 = path.join(core.getInput(constants.INPUT_SETTINGS_PATH) || os.homedir(), core.getInput(constants.INPUT_SETTINGS_PATH) ? '' : exports.M2_DIR);
yield io.mkdirP(settingsDirectory);
yield write(settingsDirectory, generate(id, username, password, gpgPassphrase));
yield write(settingsDirectory, generate(id, username, password, gpgPassphrase), overwriteSettings);
});
}
exports.createAuthenticationSettings = createAuthenticationSettings;
@ -13322,14 +13330,19 @@ function generate(id, username, password, gpgPassphrase) {
});
}
exports.generate = generate;
function write(directory, settings) {
function write(directory, settings, overwriteSettings) {
return __awaiter(this, void 0, void 0, function* () {
const location = path.join(directory, exports.SETTINGS_FILE);
if (fs.existsSync(location)) {
const settingsExists = fs.existsSync(location);
if (settingsExists && overwriteSettings) {
core.info(`Overwriting existing file ${location}`);
}
else if (!settingsExists) {
core.info(`Writing to ${location}`);
}
else {
core.info(`Writing ${location}`);
core.info(`Skipping generation ${location} because file already exists and overwriting is not required`);
return;
}
return fs.writeFileSync(location, settings, {
encoding: 'utf-8',

View File

@ -206,7 +206,9 @@ The two `settings.xml` files created from the above example look like the follow
</settings>
```
***NOTE: The `settings.xml` file is created in the Actions $HOME/.m2 directory. If you have an existing `settings.xml` file at that location, it will be overwritten. See below for using the `settings-path` to change your `settings.xml` file location.***
***NOTE***: The `settings.xml` file is created in the Actions $HOME/.m2 directory. If you have an existing `settings.xml` file at that location, it will be overwritten. See below for using the `settings-path` to change your `settings.xml` file location.
If you don't want to overwrite the `settings.xml` file, you can set `overwrite-settings: false`
### Extra setup for pom.xml:

View File

@ -8,6 +8,7 @@ import * as os from 'os';
import { create as xmlCreate } from 'xmlbuilder2';
import * as constants from './constants';
import * as gpg from './gpg';
import { getBooleanInput } from './util';
export const M2_DIR = '.m2';
export const SETTINGS_FILE = 'settings.xml';
@ -16,6 +17,9 @@ 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 settingsDirectory =
core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), M2_DIR);
const overwriteSettings = getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true);
const gpgPrivateKey =
core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY;
const gpgPassphrase =
@ -26,7 +30,14 @@ export async function configureAuthentication() {
core.setSecret(gpgPrivateKey);
}
await createAuthenticationSettings(id, username, password, gpgPassphrase);
await createAuthenticationSettings(
id,
username,
password,
settingsDirectory,
overwriteSettings,
gpgPassphrase
);
if (gpgPrivateKey) {
core.info('Importing private gpg key');
@ -39,17 +50,19 @@ export async function createAuthenticationSettings(
id: string,
username: string,
password: string,
settingsDirectory: string,
overwriteSettings: boolean,
gpgPassphrase: string | undefined = undefined
) {
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(
core.getInput(constants.INPUT_SETTINGS_PATH) || os.homedir(),
core.getInput(constants.INPUT_SETTINGS_PATH) ? '' : M2_DIR
);
await io.mkdirP(settingsDirectory);
await write(settingsDirectory, generate(id, username, password, gpgPassphrase));
await write(
settingsDirectory,
generate(id, username, password, gpgPassphrase),
overwriteSettings
);
}
// only exported for testing purposes
@ -92,12 +105,18 @@ export function generate(
});
}
async function write(directory: string, settings: string) {
async function write(directory: string, settings: string, overwriteSettings: boolean) {
const location = path.join(directory, SETTINGS_FILE);
if (fs.existsSync(location)) {
const settingsExists = fs.existsSync(location);
if (settingsExists && overwriteSettings) {
core.info(`Overwriting existing file ${location}`);
} else if (!settingsExists) {
core.info(`Writing to ${location}`);
} else {
core.info(`Writing ${location}`);
core.info(
`Skipping generation ${location} because file already exists and overwriting is not required`
);
return;
}
return fs.writeFileSync(location, settings, {

View File

@ -8,6 +8,7 @@ export const INPUT_SERVER_ID = 'server-id';
export const INPUT_SERVER_USERNAME = 'server-username';
export const INPUT_SERVER_PASSWORD = 'server-password';
export const INPUT_SETTINGS_PATH = 'settings-path';
export const INPUT_OVERWRITE_SETTINGS = 'overwrite-settings';
export const INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
export const INPUT_GPG_PASSPHRASE = 'gpg-passphrase';

View File

@ -2,6 +2,7 @@ import os from 'os';
import path from 'path';
import * as fs from 'fs';
import * as semver from 'semver';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
export function getTempDir() {
@ -10,6 +11,10 @@ export function getTempDir() {
return tempDirectory;
}
export function getBooleanInput(inputName: string, defaultValue: boolean = false) {
return (core.getInput(inputName) || String(defaultValue)).toUpperCase() === 'TRUE';
}
export function getVersionFromToolcachePath(toolPath: string) {
if (toolPath) {
return path.basename(path.dirname(toolPath));