Skip to content

Commit 804a60f

Browse files
Add "overwrite-settings" input parameter (#136)
* add overwrite-settings parameter * fix e2e tests * print debug * fix e2e tests * add comment * remove comment
1 parent 7c88894 commit 804a60f

10 files changed

Lines changed: 184 additions & 37 deletions

File tree

.github/workflows/e2e-publishing.yml

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ on:
1111
paths-ignore:
1212
- '**.md'
1313

14+
defaults:
15+
run:
16+
shell: pwsh
17+
1418
jobs:
1519
setup-java-publishing:
1620
name: Validate settings.xml
@@ -34,12 +38,7 @@ jobs:
3438
gpg-passphrase: MAVEN_GPG_PASSPHRASE
3539
- name: Validate settings.xml
3640
run: |
37-
$homePath = $env:USERPROFILE
38-
if (-not $homePath) {
39-
$homePath = $env:HOME
40-
}
41-
$xmlPath = Join-Path $homePath ".m2" "settings.xml"
42-
41+
$xmlPath = Join-Path $HOME ".m2" "settings.xml"
4342
Get-Content $xmlPath | ForEach-Object { Write-Host $_ }
4443
4544
[xml]$xml = Get-Content $xmlPath
@@ -51,10 +50,82 @@ jobs:
5150
if (($servers[1].id -ne 'gpg.passphrase') -or ($servers[1].passphrase -ne '${env.MAVEN_GPG_PASSPHRASE}')) {
5251
throw "Generated XML file is incorrect"
5352
}
54-
shell: pwsh
53+
54+
test-publishing-overwrite:
55+
name: settings.xml is overwritten if flag is true
56+
runs-on: ${{ matrix.os }}
57+
strategy:
58+
fail-fast: false
59+
matrix:
60+
os: [macos-latest, windows-latest, ubuntu-latest]
61+
steps:
62+
- name: Checkout
63+
uses: actions/checkout@v2
64+
- name: Create fake settings.xml
65+
run: |
66+
$xmlDirectory = Join-Path $HOME ".m2"
67+
$xmlPath = Join-Path $xmlDirectory "settings.xml"
68+
New-Item -Path $xmlDirectory -ItemType Directory
69+
Set-Content -Path $xmlPath -Value "Fake_XML"
70+
- name: setup-java
71+
uses: ./
72+
id: setup-java
73+
with:
74+
distribution: 'adopt'
75+
java-version: '11'
76+
server-id: maven
77+
server-username: MAVEN_USERNAME
78+
server-password: MAVEN_CENTRAL_TOKEN
79+
gpg-passphrase: MAVEN_GPG_PASSPHRASE
80+
- name: Validate settings.xml is overwritten
81+
run: |
82+
$xmlPath = Join-Path $HOME ".m2" "settings.xml"
83+
Get-Content $xmlPath | ForEach-Object { Write-Host $_ }
84+
85+
$content = Get-Content $xmlPath -Raw
86+
if ($content -notlike '*maven*') {
87+
throw "settings.xml file is not overwritten"
88+
}
89+
90+
test-publishing-skip-overwrite:
91+
name: settings.xml is not overwritten if flag is false
92+
runs-on: ${{ matrix.os }}
93+
strategy:
94+
fail-fast: false
95+
matrix:
96+
os: [macos-latest, windows-latest, ubuntu-latest]
97+
steps:
98+
- name: Checkout
99+
uses: actions/checkout@v2
100+
- name: Create fake settings.xml
101+
run: |
102+
$xmlDirectory = Join-Path $HOME ".m2"
103+
$xmlPath = Join-Path $xmlDirectory "settings.xml"
104+
New-Item -Path $xmlDirectory -ItemType Directory
105+
Set-Content -Path $xmlPath -Value "Fake_XML"
106+
- name: setup-java
107+
uses: ./
108+
id: setup-java
109+
with:
110+
distribution: 'adopt'
111+
java-version: '11'
112+
server-id: maven
113+
server-username: MAVEN_USERNAME
114+
server-password: MAVEN_CENTRAL_TOKEN
115+
overwrite-settings: false
116+
gpg-passphrase: MAVEN_GPG_PASSPHRASE
117+
- name: Validate that settings.xml is not overwritten
118+
run: |
119+
$xmlPath = Join-Path $HOME ".m2" "settings.xml"
120+
$content = Get-Content -Path $xmlPath -Raw
121+
Write-Host $content
122+
123+
if ($content -notlike "*Fake_XML*") {
124+
throw "settings.xml file was overwritten but it should not be"
125+
}
55126
56127
test-publishing-custom-location:
57-
name: Validate settings.xml in custom location
128+
name: settings.xml in custom location
58129
runs-on: ${{ matrix.os }}
59130
strategy:
60131
fail-fast: false
@@ -79,5 +150,4 @@ jobs:
79150
$path = Join-Path $env:RUNNER_TEMP "settings.xml"
80151
if (-not (Test-Path $path)) {
81152
throw "settings.xml file is not found in expected location"
82-
}
83-
shell: pwsh
153+
}

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This action provides the following functionality for GitHub Actions runners:
2020
Inputs `java-version` and `distribution` are mandatory. See [Supported distributions](../README.md#Supported-distributions) section for a list of available options.
2121

2222
### Basic
23+
**Adopt OpenJDK**
2324
```yaml
2425
steps:
2526
- uses: actions/checkout@v2
@@ -30,6 +31,17 @@ steps:
3031
- run: java -cp java HelloWorldApp
3132
```
3233
34+
**Zulu OpenJDK**
35+
```yaml
36+
steps:
37+
- uses: actions/checkout@v2
38+
- uses: actions/setup-java@v2-preview
39+
with:
40+
distribution: 'zulu' # See 'Supported distributions' for available options
41+
java-version: '11'
42+
- run: java -cp java HelloWorldApp
43+
```
44+
3345
#### Supported version syntax
3446
The `java-version` input supports an exact version or a version range using [SemVer](https://semver.org/) notation:
3547
- major versions: `8`, `11`, `15`

__tests__/auth.test.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ describe('auth tests', () => {
3535

3636
const altHome = path.join(__dirname, 'runner', 'settings');
3737
const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE);
38-
process.env[`INPUT_SETTINGS-PATH`] = altHome;
3938
await io.rmRF(altHome); // ensure it doesn't already exist
4039

41-
await auth.createAuthenticationSettings(id, username, password);
40+
await auth.createAuthenticationSettings(id, username, password, altHome, true);
4241

4342
expect(fs.existsSync(m2Dir)).toBe(false);
4443
expect(fs.existsSync(settingsFile)).toBe(false);
@@ -49,7 +48,6 @@ describe('auth tests', () => {
4948
auth.generate(id, username, password)
5049
);
5150

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

@@ -58,7 +56,7 @@ describe('auth tests', () => {
5856
const username = 'UNAME';
5957
const password = 'TOKEN';
6058

61-
await auth.createAuthenticationSettings(id, username, password);
59+
await auth.createAuthenticationSettings(id, username, password, m2Dir, true);
6260

6361
expect(fs.existsSync(m2Dir)).toBe(true);
6462
expect(fs.existsSync(settingsFile)).toBe(true);
@@ -71,7 +69,7 @@ describe('auth tests', () => {
7169
const password = 'TOKEN';
7270
const gpgPassphrase = 'GPG';
7371

74-
await auth.createAuthenticationSettings(id, username, password, gpgPassphrase);
72+
await auth.createAuthenticationSettings(id, username, password, m2Dir, true, gpgPassphrase);
7573

7674
expect(fs.existsSync(m2Dir)).toBe(true);
7775
expect(fs.existsSync(settingsFile)).toBe(true);
@@ -90,13 +88,30 @@ describe('auth tests', () => {
9088
expect(fs.existsSync(m2Dir)).toBe(true);
9189
expect(fs.existsSync(settingsFile)).toBe(true);
9290

93-
await auth.createAuthenticationSettings(id, username, password);
91+
await auth.createAuthenticationSettings(id, username, password, m2Dir, true);
9492

9593
expect(fs.existsSync(m2Dir)).toBe(true);
9694
expect(fs.existsSync(settingsFile)).toBe(true);
9795
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(auth.generate(id, username, password));
9896
}, 100000);
9997

98+
it('does not overwrite existing settings.xml files', async () => {
99+
const id = 'packages';
100+
const username = 'USERNAME';
101+
const password = 'PASSWORD';
102+
103+
fs.mkdirSync(m2Dir, { recursive: true });
104+
fs.writeFileSync(settingsFile, 'FAKE FILE');
105+
expect(fs.existsSync(m2Dir)).toBe(true);
106+
expect(fs.existsSync(settingsFile)).toBe(true);
107+
108+
await auth.createAuthenticationSettings(id, username, password, m2Dir, false);
109+
110+
expect(fs.existsSync(m2Dir)).toBe(true);
111+
expect(fs.existsSync(settingsFile)).toBe(true);
112+
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual('FAKE FILE');
113+
}, 100000);
114+
100115
it('generates valid settings.xml with minimal configuration', () => {
101116
const id = 'packages';
102117
const username = 'USER';

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ inputs:
3838
settings-path:
3939
description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
4040
required: false
41+
overwrite-settings:
42+
description: 'Overwrite the settings.xml file if it exists. Default is "true".'
43+
required: false
44+
default: true
4145
gpg-private-key:
4246
description: 'GPG private key to import. Default is empty string.'
4347
required: false

dist/cleanup/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,17 +2685,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26852685
return (mod && mod.__esModule) ? mod : { "default": mod };
26862686
};
26872687
Object.defineProperty(exports, "__esModule", { value: true });
2688-
exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getTempDir = void 0;
2688+
exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
26892689
const os_1 = __importDefault(__webpack_require__(87));
26902690
const path_1 = __importDefault(__webpack_require__(622));
26912691
const fs = __importStar(__webpack_require__(747));
26922692
const semver = __importStar(__webpack_require__(876));
2693+
const core = __importStar(__webpack_require__(470));
26932694
const tc = __importStar(__webpack_require__(533));
26942695
function getTempDir() {
26952696
let tempDirectory = process.env['RUNNER_TEMP'] || os_1.default.tmpdir();
26962697
return tempDirectory;
26972698
}
26982699
exports.getTempDir = getTempDir;
2700+
function getBooleanInput(inputName, defaultValue = false) {
2701+
return (core.getInput(inputName) || String(defaultValue)).toUpperCase() === 'TRUE';
2702+
}
2703+
exports.getBooleanInput = getBooleanInput;
26992704
function getVersionFromToolcachePath(toolPath) {
27002705
if (toolPath) {
27012706
return path_1.default.basename(path_1.default.dirname(toolPath));
@@ -6824,7 +6829,7 @@ function isUnixExecutable(stats) {
68246829
"use strict";
68256830

68266831
Object.defineProperty(exports, "__esModule", { value: true });
6827-
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;
6832+
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;
68286833
exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home';
68296834
exports.INPUT_JAVA_VERSION = 'java-version';
68306835
exports.INPUT_ARCHITECTURE = 'architecture';
@@ -6835,6 +6840,7 @@ exports.INPUT_SERVER_ID = 'server-id';
68356840
exports.INPUT_SERVER_USERNAME = 'server-username';
68366841
exports.INPUT_SERVER_PASSWORD = 'server-password';
68376842
exports.INPUT_SETTINGS_PATH = 'settings-path';
6843+
exports.INPUT_OVERWRITE_SETTINGS = 'overwrite-settings';
68386844
exports.INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
68396845
exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase';
68406846
exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined;

dist/setup/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11302,7 +11302,7 @@ exports.HTMLCollectionImpl = HTMLCollectionImpl;
1130211302
"use strict";
1130311303

1130411304
Object.defineProperty(exports, "__esModule", { value: true });
11305-
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;
11305+
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;
1130611306
exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home';
1130711307
exports.INPUT_JAVA_VERSION = 'java-version';
1130811308
exports.INPUT_ARCHITECTURE = 'architecture';
@@ -11313,6 +11313,7 @@ exports.INPUT_SERVER_ID = 'server-id';
1131311313
exports.INPUT_SERVER_USERNAME = 'server-username';
1131411314
exports.INPUT_SERVER_PASSWORD = 'server-password';
1131511315
exports.INPUT_SETTINGS_PATH = 'settings-path';
11316+
exports.INPUT_OVERWRITE_SETTINGS = 'overwrite-settings';
1131611317
exports.INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
1131711318
exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase';
1131811319
exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined;
@@ -12935,17 +12936,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1293512936
return (mod && mod.__esModule) ? mod : { "default": mod };
1293612937
};
1293712938
Object.defineProperty(exports, "__esModule", { value: true });
12938-
exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getTempDir = void 0;
12939+
exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
1293912940
const os_1 = __importDefault(__webpack_require__(87));
1294012941
const path_1 = __importDefault(__webpack_require__(622));
1294112942
const fs = __importStar(__webpack_require__(747));
1294212943
const semver = __importStar(__webpack_require__(876));
12944+
const core = __importStar(__webpack_require__(470));
1294312945
const tc = __importStar(__webpack_require__(139));
1294412946
function getTempDir() {
1294512947
let tempDirectory = process.env['RUNNER_TEMP'] || os_1.default.tmpdir();
1294612948
return tempDirectory;
1294712949
}
1294812950
exports.getTempDir = getTempDir;
12951+
function getBooleanInput(inputName, defaultValue = false) {
12952+
return (core.getInput(inputName) || String(defaultValue)).toUpperCase() === 'TRUE';
12953+
}
12954+
exports.getBooleanInput = getBooleanInput;
1294912955
function getVersionFromToolcachePath(toolPath) {
1295012956
if (toolPath) {
1295112957
return path_1.default.basename(path_1.default.dirname(toolPath));
@@ -13257,20 +13263,23 @@ const os = __importStar(__webpack_require__(87));
1325713263
const xmlbuilder2_1 = __webpack_require__(255);
1325813264
const constants = __importStar(__webpack_require__(211));
1325913265
const gpg = __importStar(__webpack_require__(884));
13266+
const util_1 = __webpack_require__(322);
1326013267
exports.M2_DIR = '.m2';
1326113268
exports.SETTINGS_FILE = 'settings.xml';
1326213269
function configureAuthentication() {
1326313270
return __awaiter(this, void 0, void 0, function* () {
1326413271
const id = core.getInput(constants.INPUT_SERVER_ID);
1326513272
const username = core.getInput(constants.INPUT_SERVER_USERNAME);
1326613273
const password = core.getInput(constants.INPUT_SERVER_PASSWORD);
13274+
const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), exports.M2_DIR);
13275+
const overwriteSettings = util_1.getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true);
1326713276
const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY;
1326813277
const gpgPassphrase = core.getInput(constants.INPUT_GPG_PASSPHRASE) ||
1326913278
(gpgPrivateKey ? constants.INPUT_DEFAULT_GPG_PASSPHRASE : undefined);
1327013279
if (gpgPrivateKey) {
1327113280
core.setSecret(gpgPrivateKey);
1327213281
}
13273-
yield createAuthenticationSettings(id, username, password, gpgPassphrase);
13282+
yield createAuthenticationSettings(id, username, password, settingsDirectory, overwriteSettings, gpgPassphrase);
1327413283
if (gpgPrivateKey) {
1327513284
core.info('Importing private gpg key');
1327613285
const keyFingerprint = (yield gpg.importKey(gpgPrivateKey)) || '';
@@ -13279,14 +13288,13 @@ function configureAuthentication() {
1327913288
});
1328013289
}
1328113290
exports.configureAuthentication = configureAuthentication;
13282-
function createAuthenticationSettings(id, username, password, gpgPassphrase = undefined) {
13291+
function createAuthenticationSettings(id, username, password, settingsDirectory, overwriteSettings, gpgPassphrase = undefined) {
1328313292
return __awaiter(this, void 0, void 0, function* () {
1328413293
core.info(`Creating ${exports.SETTINGS_FILE} with server-id: ${id}`);
1328513294
// when an alternate m2 location is specified use only that location (no .m2 directory)
1328613295
// otherwise use the home/.m2/ path
13287-
const settingsDirectory = path.join(core.getInput(constants.INPUT_SETTINGS_PATH) || os.homedir(), core.getInput(constants.INPUT_SETTINGS_PATH) ? '' : exports.M2_DIR);
1328813296
yield io.mkdirP(settingsDirectory);
13289-
yield write(settingsDirectory, generate(id, username, password, gpgPassphrase));
13297+
yield write(settingsDirectory, generate(id, username, password, gpgPassphrase), overwriteSettings);
1329013298
});
1329113299
}
1329213300
exports.createAuthenticationSettings = createAuthenticationSettings;
@@ -13322,14 +13330,19 @@ function generate(id, username, password, gpgPassphrase) {
1332213330
});
1332313331
}
1332413332
exports.generate = generate;
13325-
function write(directory, settings) {
13333+
function write(directory, settings, overwriteSettings) {
1332613334
return __awaiter(this, void 0, void 0, function* () {
1332713335
const location = path.join(directory, exports.SETTINGS_FILE);
13328-
if (fs.existsSync(location)) {
13336+
const settingsExists = fs.existsSync(location);
13337+
if (settingsExists && overwriteSettings) {
1332913338
core.info(`Overwriting existing file ${location}`);
1333013339
}
13340+
else if (!settingsExists) {
13341+
core.info(`Writing to ${location}`);
13342+
}
1333113343
else {
13332-
core.info(`Writing ${location}`);
13344+
core.info(`Skipping generation ${location} because file already exists and overwriting is not required`);
13345+
return;
1333313346
}
1333413347
return fs.writeFileSync(location, settings, {
1333513348
encoding: 'utf-8',

docs/advanced-usage.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ The two `settings.xml` files created from the above example look like the follow
206206
</settings>
207207
```
208208

209-
***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.***
209+
***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.
210+
211+
If you don't want to overwrite the `settings.xml` file, you can set `overwrite-settings: false`
210212

211213
### Extra setup for pom.xml:
212214

0 commit comments

Comments
 (0)