Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add overwrite-settings parameter
  • Loading branch information
Maxim Lobanov committed Mar 16, 2021
commit b86b1900447583e4e7c2d54f57bd951c89c0189a
82 changes: 73 additions & 9 deletions .github/workflows/e2e-publishing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ on:
paths-ignore:
- '**.md'

defaults:
run:
shell: pwsh

jobs:
setup-java-publishing:
name: Validate settings.xml
Expand All @@ -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
Expand All @@ -51,7 +50,73 @@ 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: Validate 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: |
$xmlPath = Join-Path $HOME ".m2" "settings.xml"
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 $_ }

$xml = Get-Content $xmlPath -Raw
if ($raw -notlike 'maven') {
throw "settings.xml file is not overwritten"
}

test-publishing-skip-overwrite:
name: Validate 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: |
$xmlPath = Join-Path $HOME ".m2" "settings.xml"
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
if ($content -ne "Fake_XML") {
throw "settings.xml file was overwritten but it should not be"
}

test-publishing-custom-location:
name: Validate settings.xml in custom location
Expand Down Expand Up @@ -79,5 +144,4 @@ jobs:
$path = Join-Path $env:RUNNER_TEMP "settings.xml"
if (-not (Test-Path $path)) {
throw "settings.xml file is not found in expected location"
}
shell: pwsh
}
27 changes: 21 additions & 6 deletions __tests__/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -49,7 +48,6 @@ describe('auth tests', () => {
auth.generate(id, username, password)
);

delete process.env[`INPUT_SETTINGS-PATH`];
await io.rmRF(altHome);
}, 100000);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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';
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions dist/cleanup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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';
Expand All @@ -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;
Expand Down
31 changes: 22 additions & 9 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -13257,20 +13263,23 @@ 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() {
return __awaiter(this, void 0, void 0, function* () {
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)) || '';
Expand All @@ -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;
Expand Down Expand Up @@ -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}' - it already exists and overwriting is not required`);
return;
}
return fs.writeFileSync(location, settings, {
encoding: 'utf-8',
Expand Down
4 changes: 3 additions & 1 deletion docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Loading