Skip to content

Commit 2a1bf9c

Browse files
Merge pull request #86 from s4u/fix-84
Allow defining custom configuration for server
2 parents 4309eaf + 060e444 commit 2a1bf9c

File tree

5 files changed

+218
-38
lines changed

5 files changed

+218
-38
lines changed

README.md

Lines changed: 88 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,101 +25,163 @@ or use automatic tools like [Dependabot](https://dependabot.com/).
2525
# Usage
2626
See [action.yml](action.yml)
2727

28-
Create default ```settings.xml```:
28+
## default ```settings.xml```
2929
```yml
3030
steps:
3131
- uses: s4u/[email protected]
3232
```
3333
34-
Create ```settings.xml``` with servers section:
34+
## ```settings.xml``` with servers section
35+
3536
```yml
3637
steps:
3738
- uses: s4u/[email protected]
3839
with:
3940
servers: '[{"id": "serverId", "username": "username", "password": "password"}]'
4041
```
4142

42-
Create ```settings.xml``` with mirrors section:
43+
## ```settings.xml``` with servers section and additional configuration
44+
45+
``` yml
46+
steps:
47+
- uses: s4u/[email protected]
48+
with:
49+
servers: |
50+
[{
51+
"id": "serverId",
52+
"configuration": {
53+
"item1": "value1",
54+
"item2": {
55+
"item21": "value21",
56+
"item22": "value22"
57+
}
58+
}
59+
}]
60+
```
61+
62+
result will be:
63+
64+
```xml
65+
<server>
66+
<id>serverId</id>
67+
<configuration>
68+
<item1>value1</item1>
69+
<item2>
70+
<item21>value21</item21>
71+
<item22>value22</item22>
72+
</item1>
73+
</configuration>
74+
</server></servers>
75+
```
76+
77+
78+
## ```settings.xml``` with mirrors section
4379
```yml
4480
steps:
4581
- uses: s4u/[email protected]
4682
with:
4783
mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]'
4884
```
4985

50-
Create ```settings.xml``` with maven properties:
86+
## ```settings.xml``` with properties
5187
```yml
5288
steps:
5389
- uses: s4u/[email protected]
5490
with:
5591
properties: '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'
5692
```
5793

58-
It is also possible pass in Github Secrets e.g.
59-
60-
``` yml
61-
with:
62-
servers: |
63-
[{
64-
"id": "sonatype-nexus-snapshots",
65-
"username": "${{ secrets.SONATYPE_USERNAME }}",
66-
"password": "${{ secrets.SONATYPE_PASSWORD }}"
67-
}]
68-
69-
```
70-
71-
**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'`.
94+
## ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list
7295

73-
74-
Create ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list
7596
```yml
7697
steps:
7798
- uses: s4u/[email protected]
7899
with:
79100
sonatypeSnapshots: true
80101
```
81102

82-
Create ```settings.xml``` with https://repository.apache.org/snapshots/ in repository list
103+
## ```settings.xml``` with https://repository.apache.org/snapshots/ in repository list
104+
83105
```yml
84106
steps:
85107
- uses: s4u/[email protected]
86108
with:
87109
apacheSnapshots: true
88110
```
89111

90-
Do not override existing ```settings.xml```, from version 2.0 file is override by default :
112+
## Do not override existing ```settings.xml```, from version **2.0** file is override by default :
91113
```yml
92114
steps:
93115
- uses: s4u/[email protected]
94116
with:
95117
override: false
96118
```
97119

98-
Do not add github to server in ```settings.xml```, by default is added:
120+
## Do not add github to server in ```settings.xml```, by default is added:
99121
```yml
100122
steps:
101123
- uses: s4u/[email protected]
102124
with:
103125
githubServer: false
104126
```
105127

106-
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)
128+
## ```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)
129+
107130
```yml
108131
steps:
109132
- uses: s4u/[email protected]
110133
with:
111134
oracleServers: '[{"id": "serverId", "username": "username", "password": "password"}]'
112135
```
113136

114-
Create ```settings.xml``` with [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9017)
137+
## ```settings.xml``` with [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9017)
115138
```yml
116139
steps:
117140
- uses: s4u/[email protected]
118141
with:
119142
oracleRepo: true
120143
```
121144

122-
**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.
145+
## GitHub actions secrets
146+
147+
It is also possible pass in Github Secrets e.g.
148+
149+
``` yml
150+
steps:
151+
- uses: s4u/[email protected]
152+
with:
153+
servers: |
154+
[{
155+
"id": "sonatype-nexus-snapshots",
156+
"username": "${{ secrets.SONATYPE_USERNAME }}",
157+
"password": "${{ secrets.SONATYPE_PASSWORD }}"
158+
}]
159+
```
160+
161+
**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'`.
162+
163+
# Notes
164+
165+
**maven-settings-action** should be put at the latest position before maven run in order to avoid override ```setting.xml``` by another action
166+
167+
```yml
168+
steps:
169+
- uses: actions/checkout@v2
170+
171+
- uses: actions/cache@v2
172+
with:
173+
path: ~/.m2/repository
174+
key: maven-${{ hashFiles('**/pom.xml') }}
175+
restore-keys: maven-
176+
177+
- uses: actions/setup-java@v1
178+
with:
179+
java-version: 8
180+
181+
- uses: s4u/[email protected]
182+
183+
- run: mvn verify
184+
```
123185

124186
# License
125187

index.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ afterAll(() => {
6363

6464
test('run with all feature', () => {
6565

66-
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword"}]';
66+
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword", "configuration": {"props1": "value1"}}]';
6767
process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]';
6868
process.env['INPUT_GITHUBSERVER'] = true;
6969

@@ -80,7 +80,7 @@ test('run with all feature', () => {
8080
expect(settingsStatus.isFile()).toBeTruthy();
8181
expect(settingsStatus.size).toBeGreaterThan(0);
8282

83-
const settingsBody = fs.readFileSync(settingsPath).toString();
83+
const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^ $/mg, '');
8484
expect(settingsBody).toBe(`<settings>
8585
<interactiveMode>false</interactiveMode>
8686
<profiles>
@@ -186,6 +186,7 @@ test('run with all feature', () => {
186186
<id>serverId</id>
187187
<username>sUsername</username>
188188
<password>sPassword</password>
189+
<configuration><props1>value1</props1></configuration>
189190
</server>
190191
<server>
191192
<id>oServerId</id>
@@ -213,6 +214,7 @@ test('run with all feature', () => {
213214
<id>github</id>
214215
<username>\${env.GITHUB_ACTOR}</username>
215216
<password>\${env.GITHUB_TOKEN}</password>
217+
216218
</server></servers>
217219
<mirrors>
218220
<mirror>

settings.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,51 @@ function writeSettings(settingsPath, templateXml) {
5050
fs.writeFileSync(settingsPath, settingStr);
5151
}
5252

53-
function fillServer(templateXml, templateName, id, username, password) {
53+
function jsonToXml(templateXml, xmlTag, json) {
54+
for (const key in json) {
55+
const keyXml = templateXml.createElement(key);
56+
const value = json[key];
57+
if ( value instanceof Object) {
58+
jsonToXml(templateXml, keyXml, value);
59+
} else {
60+
keyXml.textContent = value;
61+
}
62+
xmlTag.appendChild(keyXml);
63+
}
64+
}
5465

55-
if (!id || !username || !password) {
56-
core.setFailed(templateName + ' must contain id, username and password');
66+
function fillServer(templateXml, templateName, id, username, password, configurations) {
67+
68+
if (!id || ((!username || !password) && !configurations) ) {
69+
core.setFailed(templateName + ' must contain id, (username and password) or configuration');
5770
return;
5871
}
5972

6073
const serverXml = getTemplate(templateName + '.xml')
6174
serverXml.getElementsByTagName('id')[0].textContent = id;
62-
serverXml.getElementsByTagName('username')[0].textContent = username;
63-
serverXml.getElementsByTagName('password')[0].textContent = password;
75+
76+
const usernameTag = serverXml.getElementsByTagName('username')[0];
77+
if (username) {
78+
usernameTag.textContent = username;
79+
} else {
80+
serverXml.documentElement.removeChild(usernameTag);
81+
}
82+
83+
const passwordTag = serverXml.getElementsByTagName('password')[0];
84+
if (password) {
85+
passwordTag.textContent = password;
86+
} else {
87+
serverXml.documentElement.removeChild(passwordTag);
88+
}
89+
90+
const configurationTag = serverXml.getElementsByTagName('configuration')[0];
91+
if (configurations) {
92+
jsonToXml(templateXml, configurationTag, configurations);
93+
} else {
94+
if (configurationTag.childNodes.length == 0) {
95+
serverXml.documentElement.removeChild(configurationTag);
96+
}
97+
}
6498

6599
const serversXml = templateXml.getElementsByTagName('servers')[0];
66100
serversXml.appendChild(serverXml);
@@ -74,7 +108,8 @@ function fillServers(template, templateName) {
74108
return;
75109
}
76110

77-
JSON.parse(servers).forEach((server) => fillServer(template, templateName, server.id, server.username, server.password));
111+
JSON.parse(servers).forEach((server) =>
112+
fillServer(template, templateName, server.id, server.username, server.password, server.configuration));
78113
}
79114

80115
function fillMirror(template, id, name, mirrorOf, url) {

0 commit comments

Comments
 (0)