Allow defining custom configuration for server

fix #84
This commit is contained in:
Slawomir Jaranowski
2020-12-21 23:28:12 +01:00
parent 4309eaf1f7
commit 060e4442b5
5 changed files with 218 additions and 38 deletions

114
README.md
View File

@ -25,13 +25,14 @@ or use automatic tools like [Dependabot](https://dependabot.com/).
# Usage # Usage
See [action.yml](action.yml) See [action.yml](action.yml)
Create default ```settings.xml```: ## default ```settings.xml```
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
``` ```
Create ```settings.xml``` with servers section: ## ```settings.xml``` with servers section
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -39,7 +40,42 @@ steps:
servers: '[{"id": "serverId", "username": "username", "password": "password"}]' servers: '[{"id": "serverId", "username": "username", "password": "password"}]'
``` ```
Create ```settings.xml``` with mirrors section: ## ```settings.xml``` with servers section and additional configuration
``` yml
steps:
- uses: s4u/maven-settings-action@v2.2.0
with:
servers: |
[{
"id": "serverId",
"configuration": {
"item1": "value1",
"item2": {
"item21": "value21",
"item22": "value22"
}
}
}]
```
result will be:
```xml
<server>
<id>serverId</id>
<configuration>
<item1>value1</item1>
<item2>
<item21>value21</item21>
<item22>value22</item22>
</item1>
</configuration>
</server></servers>
```
## ```settings.xml``` with mirrors section
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -47,7 +83,7 @@ steps:
mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]' mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]'
``` ```
Create ```settings.xml``` with maven properties: ## ```settings.xml``` with properties
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -55,23 +91,8 @@ steps:
properties: '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]' properties: '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'
``` ```
It is also possible pass in Github Secrets e.g. ## ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list
``` yml
with:
servers: |
[{
"id": "sonatype-nexus-snapshots",
"username": "${{ secrets.SONATYPE_USERNAME }}",
"password": "${{ secrets.SONATYPE_PASSWORD }}"
}]
```
**Note**: secrets are *not* passed in if the workflow is triggered from a forked repository. See [here](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets#using-encrypted-secrets-in-a-workflow) for further information. This can be avoided by using `if` triggers on the job e.g. `if: github.event_name == 'push'`.
Create ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -79,7 +100,8 @@ steps:
sonatypeSnapshots: true sonatypeSnapshots: true
``` ```
Create ```settings.xml``` with https://repository.apache.org/snapshots/ in repository list ## ```settings.xml``` with https://repository.apache.org/snapshots/ in repository list
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -87,7 +109,7 @@ steps:
apacheSnapshots: true apacheSnapshots: true
``` ```
Do not override existing ```settings.xml```, from version 2.0 file is override by default : ## Do not override existing ```settings.xml```, from version **2.0** file is override by default :
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -95,7 +117,7 @@ steps:
override: false override: false
``` ```
Do not add github to server in ```settings.xml```, by default is added: ## Do not add github to server in ```settings.xml```, by default is added:
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -103,7 +125,8 @@ steps:
githubServer: false githubServer: false
``` ```
Create ```settings.xml``` with special server item configuration for oracle repository [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9015) ## ```settings.xml``` with special server item configuration for oracle repository [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9015)
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -111,7 +134,7 @@ steps:
oracleServers: '[{"id": "serverId", "username": "username", "password": "password"}]' oracleServers: '[{"id": "serverId", "username": "username", "password": "password"}]'
``` ```
Create ```settings.xml``` with [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9017) ## ```settings.xml``` with [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9017)
```yml ```yml
steps: steps:
- uses: s4u/maven-settings-action@v2.2.0 - uses: s4u/maven-settings-action@v2.2.0
@ -119,7 +142,46 @@ steps:
oracleRepo: true oracleRepo: true
``` ```
**Note**: When using maven-settings-action in combination with Cache action (actions/cache) it is required to place the cache action **before** maven-settings-action. ## GitHub actions secrets
It is also possible pass in Github Secrets e.g.
``` yml
steps:
- uses: s4u/maven-settings-action@v2.2.0
with:
servers: |
[{
"id": "sonatype-nexus-snapshots",
"username": "${{ secrets.SONATYPE_USERNAME }}",
"password": "${{ secrets.SONATYPE_PASSWORD }}"
}]
```
**Note**: secrets are *not* passed in if the workflow is triggered from a forked repository. See [here](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets#using-encrypted-secrets-in-a-workflow) for further information. This can be avoided by using `if` triggers on the job e.g. `if: github.event_name == 'push'`.
# Notes
**maven-settings-action** should be put at the latest position before maven run in order to avoid override ```setting.xml``` by another action
```yml
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ~/.m2/repository
key: maven-${{ hashFiles('**/pom.xml') }}
restore-keys: maven-
- uses: actions/setup-java@v1
with:
java-version: 8
- uses: s4u/maven-settings-action@v2.2.0
- run: mvn verify
```
# License # License

View File

@ -63,7 +63,7 @@ afterAll(() => {
test('run with all feature', () => { test('run with all feature', () => {
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword"}]'; process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword", "configuration": {"props1": "value1"}}]';
process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]'; process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]';
process.env['INPUT_GITHUBSERVER'] = true; process.env['INPUT_GITHUBSERVER'] = true;
@ -80,7 +80,7 @@ test('run with all feature', () => {
expect(settingsStatus.isFile()).toBeTruthy(); expect(settingsStatus.isFile()).toBeTruthy();
expect(settingsStatus.size).toBeGreaterThan(0); expect(settingsStatus.size).toBeGreaterThan(0);
const settingsBody = fs.readFileSync(settingsPath).toString(); const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^ $/mg, '');
expect(settingsBody).toBe(`<settings> expect(settingsBody).toBe(`<settings>
<interactiveMode>false</interactiveMode> <interactiveMode>false</interactiveMode>
<profiles> <profiles>
@ -186,6 +186,7 @@ test('run with all feature', () => {
<id>serverId</id> <id>serverId</id>
<username>sUsername</username> <username>sUsername</username>
<password>sPassword</password> <password>sPassword</password>
<configuration><props1>value1</props1></configuration>
</server> </server>
<server> <server>
<id>oServerId</id> <id>oServerId</id>
@ -213,6 +214,7 @@ test('run with all feature', () => {
<id>github</id> <id>github</id>
<username>\${env.GITHUB_ACTOR}</username> <username>\${env.GITHUB_ACTOR}</username>
<password>\${env.GITHUB_TOKEN}</password> <password>\${env.GITHUB_TOKEN}</password>
</server></servers> </server></servers>
<mirrors> <mirrors>
<mirror> <mirror>

View File

@ -50,17 +50,51 @@ function writeSettings(settingsPath, templateXml) {
fs.writeFileSync(settingsPath, settingStr); fs.writeFileSync(settingsPath, settingStr);
} }
function fillServer(templateXml, templateName, id, username, password) { function jsonToXml(templateXml, xmlTag, json) {
for (const key in json) {
const keyXml = templateXml.createElement(key);
const value = json[key];
if ( value instanceof Object) {
jsonToXml(templateXml, keyXml, value);
} else {
keyXml.textContent = value;
}
xmlTag.appendChild(keyXml);
}
}
if (!id || !username || !password) { function fillServer(templateXml, templateName, id, username, password, configurations) {
core.setFailed(templateName + ' must contain id, username and password');
if (!id || ((!username || !password) && !configurations) ) {
core.setFailed(templateName + ' must contain id, (username and password) or configuration');
return; return;
} }
const serverXml = getTemplate(templateName + '.xml') const serverXml = getTemplate(templateName + '.xml')
serverXml.getElementsByTagName('id')[0].textContent = id; serverXml.getElementsByTagName('id')[0].textContent = id;
serverXml.getElementsByTagName('username')[0].textContent = username;
serverXml.getElementsByTagName('password')[0].textContent = password; const usernameTag = serverXml.getElementsByTagName('username')[0];
if (username) {
usernameTag.textContent = username;
} else {
serverXml.documentElement.removeChild(usernameTag);
}
const passwordTag = serverXml.getElementsByTagName('password')[0];
if (password) {
passwordTag.textContent = password;
} else {
serverXml.documentElement.removeChild(passwordTag);
}
const configurationTag = serverXml.getElementsByTagName('configuration')[0];
if (configurations) {
jsonToXml(templateXml, configurationTag, configurations);
} else {
if (configurationTag.childNodes.length == 0) {
serverXml.documentElement.removeChild(configurationTag);
}
}
const serversXml = templateXml.getElementsByTagName('servers')[0]; const serversXml = templateXml.getElementsByTagName('servers')[0];
serversXml.appendChild(serverXml); serversXml.appendChild(serverXml);
@ -74,7 +108,8 @@ function fillServers(template, templateName) {
return; return;
} }
JSON.parse(servers).forEach((server) => fillServer(template, templateName, server.id, server.username, server.password)); JSON.parse(servers).forEach((server) =>
fillServer(template, templateName, server.id, server.username, server.password, server.configuration));
} }
function fillMirror(template, id, name, mirrorOf, url) { function fillMirror(template, id, name, mirrorOf, url) {

View File

@ -136,17 +136,94 @@ test('fillServers one server', () => {
settings.fillServers(xml, 'servers'); settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml); const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
expect(xmlStr).toBe(`<servers> expect(xmlStr).toBe(`<servers>
<server> <server>
<id>id1</id> <id>id1</id>
<username>username1</username> <username>username1</username>
<password>password1</password> <password>password1</password>
</server></servers>`); </server></servers>`);
}); });
test('fillServers with username and configuration', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
expect(xmlStr).toBe(`<servers>
<server>
<id>id1</id>
<username>username</username>
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
</server></servers>`);
});
test('fillServers with username, password and configuration', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "password": "password", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
expect(xmlStr).toBe(`<servers>
<server>
<id>id1</id>
<username>username</username>
<password>password</password>
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
</server></servers>`);
});
test('fillServers with configuration', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
expect(xmlStr).toBe(`<servers>
<server>
<id>id1</id>
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
</server></servers>`);
});
test('fillServers with configuration subLevel', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": {"prop11": "value11", "prop12": "value12"}, "prop2": "value2"}}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
expect(xmlStr).toBe(`<servers>
<server>
<id>id1</id>
<configuration><prop1><prop11>value11</prop11><prop12>value12</prop12></prop1><prop2>value2</prop2></configuration>
</server></servers>`);
});
test('fillServers two servers', () => { test('fillServers two servers', () => {
const xml = new DOMParser().parseFromString("<servers/>"); const xml = new DOMParser().parseFromString("<servers/>");
@ -156,18 +233,20 @@ test('fillServers two servers', () => {
settings.fillServers(xml, 'servers'); settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml); const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
expect(xmlStr).toBe(`<servers> expect(xmlStr).toBe(`<servers>
<server> <server>
<id>id1</id> <id>id1</id>
<username>username1</username> <username>username1</username>
<password>password1</password> <password>password1</password>
</server> </server>
<server> <server>
<id>id2</id> <id>id2</id>
<username>username2</username> <username>username2</username>
<password>password2</password> <password>password2</password>
</server></servers>`); </server></servers>`);
}); });
@ -184,7 +263,7 @@ test('fill servers incorrect fields', () => {
expect(xmlStr).toBe('<servers/>'); expect(xmlStr).toBe('<servers/>');
expect(consoleOutput).toEqual( expect(consoleOutput).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.stringMatching(/::error::servers must contain id, username and password/) expect.stringMatching(/::error::servers must contain id, \(username and password\) or configuration/)
]) ])
); );
}); });
@ -232,13 +311,14 @@ test('fillServers github', () => {
settings.fillServerForGithub(xml); settings.fillServerForGithub(xml);
const xmlStr = new XMLSerializer().serializeToString(xml); const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
expect(xmlStr).toBe(`<servers> expect(xmlStr).toBe(`<servers>
<server> <server>
<id>github</id> <id>github</id>
<username>\${env.GITHUB_ACTOR}</username> <username>\${env.GITHUB_ACTOR}</username>
<password>\${env.GITHUB_TOKEN}</password> <password>\${env.GITHUB_TOKEN}</password>
</server></servers>`); </server></servers>`);
expect(consoleOutput).toEqual([]); expect(consoleOutput).toEqual([]);
}); });

View File

@ -3,4 +3,5 @@
<id /> <id />
<username /> <username />
<password /> <password />
<configuration />
</server> </server>