Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
114 changes: 88 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,101 +25,163 @@ or use automatic tools like [Dependabot](https://dependabot.com/).
# Usage
See [action.yml](action.yml)

Create default ```settings.xml```:
## default ```settings.xml```
```yml
steps:
- uses: s4u/[email protected]
```

Create ```settings.xml``` with servers section:
## ```settings.xml``` with servers section

```yml
steps:
- uses: s4u/[email protected]
with:
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/[email protected]
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
steps:
- uses: s4u/[email protected]
with:
mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]'
```

Create ```settings.xml``` with maven properties:
## ```settings.xml``` with properties
```yml
steps:
- uses: s4u/[email protected]
with:
properties: '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'
```

It is also possible pass in Github Secrets e.g.

``` 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'`.
## ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list


Create ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list
```yml
steps:
- uses: s4u/[email protected]
with:
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
steps:
- uses: s4u/[email protected]
with:
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
steps:
- uses: s4u/[email protected]
with:
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
steps:
- uses: s4u/[email protected]
with:
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
steps:
- uses: s4u/[email protected]
with:
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
steps:
- uses: s4u/[email protected]
with:
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/[email protected]
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/[email protected]

- run: mvn verify
```

# License

Expand Down
6 changes: 4 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ afterAll(() => {

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_GITHUBSERVER'] = true;

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

const settingsBody = fs.readFileSync(settingsPath).toString();
const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^ $/mg, '');
expect(settingsBody).toBe(`<settings>
<interactiveMode>false</interactiveMode>
<profiles>
Expand Down Expand Up @@ -186,6 +186,7 @@ test('run with all feature', () => {
<id>serverId</id>
<username>sUsername</username>
<password>sPassword</password>
<configuration><props1>value1</props1></configuration>
</server>
<server>
<id>oServerId</id>
Expand Down Expand Up @@ -213,6 +214,7 @@ test('run with all feature', () => {
<id>github</id>
<username>\${env.GITHUB_ACTOR}</username>
<password>\${env.GITHUB_TOKEN}</password>
</server></servers>
<mirrors>
<mirror>
Expand Down
47 changes: 41 additions & 6 deletions settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,51 @@ function writeSettings(settingsPath, templateXml) {
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) {
core.setFailed(templateName + ' must contain id, username and password');
function fillServer(templateXml, templateName, id, username, password, configurations) {

if (!id || ((!username || !password) && !configurations) ) {
core.setFailed(templateName + ' must contain id, (username and password) or configuration');
return;
}

const serverXml = getTemplate(templateName + '.xml')
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];
serversXml.appendChild(serverXml);
Expand All @@ -74,7 +108,8 @@ function fillServers(template, templateName) {
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) {
Expand Down
Loading