Skip to content

Commit d8ada52

Browse files
authored
Merge pull request actions#29 from clarkbw/maven-auth
Adding maven auth support
2 parents cfdbba3 + 9b11fe4 commit d8ada52

File tree

9 files changed

+4421
-4391
lines changed

9 files changed

+4421
-4391
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/index.js -diff -merge
2+
dist/index.js linguist-generated=true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,4 @@ typings/
9393

9494
# DynamoDB Local files
9595
.dynamodb/
96+
.vscode/

README.md

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This action sets up a java environment for use in actions by:
1313

1414
See [action.yml](action.yml)
1515

16-
Basic:
16+
## Basic
1717
```yaml
1818
steps:
1919
- uses: actions/checkout@v1
@@ -25,7 +25,7 @@ steps:
2525
- run: java -cp java HelloWorldApp
2626
```
2727
28-
From local file:
28+
## Local file
2929
```yaml
3030
steps:
3131
- uses: actions/checkout@v1
@@ -37,7 +37,7 @@ steps:
3737
- run: java -cp java HelloWorldApp
3838
```
3939
40-
Matrix Testing:
40+
## Matrix Testing
4141
```yaml
4242
jobs:
4343
build:
@@ -56,6 +56,126 @@ jobs:
5656
- run: java -cp java HelloWorldApp
5757
```
5858
59+
## Publishing using Apache Maven
60+
```yaml
61+
jobs:
62+
build:
63+
64+
runs-on: ubuntu-latest
65+
66+
steps:
67+
- uses: actions/checkout@v1
68+
- name: Set up JDK 1.8
69+
uses: actions/setup-java@v1
70+
with:
71+
java-version: 1.8
72+
73+
- name: Build with Maven
74+
run: mvn -B package --file pom.xml
75+
76+
- name: Publish to GitHub Packages Apache Maven
77+
run: mvn deploy
78+
env:
79+
GITHUB_TOKEN: ${{ github.token }} # GITHUB_TOKEN is the default env for the password
80+
81+
- name: Set up Apache Maven Central
82+
uses: actions/setup-java@v1
83+
with: # running setup-java again overwrites the settings.xml
84+
java-version: 1.8
85+
server-id: maven # Value of the distributionManagement/repository/id field of the pom.xml
86+
server-username: MAVEN_USERNAME # env variable for username in deploy
87+
server-password: MAVEN_CENTRAL_TOKEN # env variable for token in deploy
88+
89+
- name: Publish to Apache Maven Central
90+
run: mvn deploy
91+
env:
92+
MAVEN_USERNAME: maven_username123
93+
MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_TOKEN }}
94+
```
95+
96+
The two `settings.xml` files created from the above example look like the following.
97+
98+
`settings.xml` file created for the first deploy to GitHub Packages
99+
```xml
100+
<servers>
101+
<server>
102+
<id>github</id>
103+
<username>${env.GITHUB_ACTOR}</username>
104+
<password>${env.GITHUB_TOKEN}</password>
105+
</server>
106+
</servers>
107+
```
108+
109+
`settings.xml` file created for the second deploy to Apache Maven Central
110+
```xml
111+
<servers>
112+
<server>
113+
<id>maven</id>
114+
<username>${env.MAVEN_USERNAME}</username>
115+
<password>${env.MAVEN_CENTRAL_TOKEN}</password>
116+
</server>
117+
</servers>
118+
```
119+
120+
***NOTE: The `settings.xml` file is created in the Actions $HOME 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.***
121+
122+
See the help docs on [Publishing a Package](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-apache-maven-for-use-with-github-packages#publishing-a-package) for more information on the `pom.xml` file.
123+
124+
## Publishing using Gradle
125+
```yaml
126+
jobs:
127+
128+
build:
129+
runs-on: ubuntu-latest
130+
131+
steps:
132+
- uses: actions/checkout@v1
133+
134+
- name: Set up JDK 1.8
135+
uses: actions/setup-java@v1
136+
137+
- name: Build with Gradle
138+
run: gradle build
139+
140+
- name: Publish to GitHub Packages
141+
run: gradle publish
142+
env:
143+
USERNAME: ${{ github.actor }}
144+
PASSWORD: ${{ secrets.GITHUB_TOKEN }}
145+
```
146+
147+
***NOTE: The `USERNAME` and `PASSWORD` need to correspond to the credentials environment variables used in the publishing section of your `build.gradle`.***
148+
149+
See the help docs on [Publishing a Package with Gradle](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages#example-using-gradle-groovy-for-a-single-package-in-a-repository) for more information on the `build.gradle` configuration file.
150+
151+
## Apache Maven with a settings path
152+
153+
When using an Actions self-hosted runner with multiple shared runners the default `$HOME` directory can be shared by a number runners at the same time which could overwrite existing settings file. Setting the `settings-path` variable allows you to choose a unique location for your settings file.
154+
155+
```yaml
156+
jobs:
157+
build:
158+
159+
runs-on: ubuntu-latest
160+
161+
steps:
162+
- uses: actions/checkout@v1
163+
- name: Set up JDK 1.8 for Shared Runner
164+
uses: actions/setup-java@v1
165+
with:
166+
java-version: 1.8
167+
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
168+
settings-path: ${{ github.workspace }} # location for the settings.xml file
169+
170+
- name: Build with Maven
171+
run: mvn -B package --file pom.xml
172+
173+
- name: Publish to GitHub Packages Apache Maven
174+
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
175+
env:
176+
GITHUB_TOKEN: ${{ github.token }}
177+
```
178+
59179
# License
60180

61181
The scripts and documentation in this project are released under the [MIT License](LICENSE)

__tests__/auth.test.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import io = require('@actions/io');
2+
import fs = require('fs');
3+
import os = require('os');
4+
import path = require('path');
5+
6+
// make the os.homedir() call be local to the tests
7+
jest.doMock('os', () => {
8+
return {
9+
homedir: jest.fn(() => __dirname)
10+
};
11+
});
12+
13+
import * as auth from '../src/auth';
14+
15+
const m2Dir = path.join(__dirname, auth.M2_DIR);
16+
const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE);
17+
18+
describe('auth tests', () => {
19+
beforeEach(async () => {
20+
await io.rmRF(m2Dir);
21+
}, 300000);
22+
23+
afterAll(async () => {
24+
try {
25+
await io.rmRF(m2Dir);
26+
} catch {
27+
console.log('Failed to remove test directories');
28+
}
29+
}, 100000);
30+
31+
it('creates settings.xml in alternate locations', async () => {
32+
const id = 'packages';
33+
const username = 'UNAMI';
34+
const password = 'TOLKIEN';
35+
36+
const altHome = path.join(__dirname, 'runner', 'settings');
37+
const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE);
38+
process.env[`INPUT_SETTINGS-PATH`] = altHome;
39+
await io.rmRF(altHome); // ensure it doesn't already exist
40+
41+
await auth.configAuthentication(id, username, password);
42+
43+
expect(fs.existsSync(m2Dir)).toBe(false);
44+
expect(fs.existsSync(settingsFile)).toBe(false);
45+
46+
expect(fs.existsSync(altHome)).toBe(true);
47+
expect(fs.existsSync(altSettingsFile)).toBe(true);
48+
expect(fs.readFileSync(altSettingsFile, 'utf-8')).toEqual(
49+
auth.generate(id, username, password)
50+
);
51+
52+
delete process.env[`INPUT_SETTINGS-PATH`];
53+
await io.rmRF(altHome);
54+
}, 100000);
55+
56+
it('creates settings.xml with username and password', async () => {
57+
const id = 'packages';
58+
const username = 'UNAME';
59+
const password = 'TOKEN';
60+
61+
await auth.configAuthentication(id, username, password);
62+
63+
expect(fs.existsSync(m2Dir)).toBe(true);
64+
expect(fs.existsSync(settingsFile)).toBe(true);
65+
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
66+
auth.generate(id, username, password)
67+
);
68+
}, 100000);
69+
70+
it('overwrites existing settings.xml files', async () => {
71+
const id = 'packages';
72+
const username = 'USERNAME';
73+
const password = 'PASSWORD';
74+
75+
fs.mkdirSync(m2Dir, {recursive: true});
76+
fs.writeFileSync(settingsFile, 'FAKE FILE');
77+
expect(fs.existsSync(m2Dir)).toBe(true);
78+
expect(fs.existsSync(settingsFile)).toBe(true);
79+
80+
await auth.configAuthentication(id, username, password);
81+
82+
expect(fs.existsSync(m2Dir)).toBe(true);
83+
expect(fs.existsSync(settingsFile)).toBe(true);
84+
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
85+
auth.generate(id, username, password)
86+
);
87+
}, 100000);
88+
89+
it('does not create settings.xml without required parameters', async () => {
90+
await auth.configAuthentication('FOO');
91+
92+
expect(fs.existsSync(m2Dir)).toBe(true);
93+
expect(fs.existsSync(settingsFile)).toBe(true);
94+
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
95+
auth.generate('FOO', auth.DEFAULT_USERNAME, auth.DEFAULT_PASSWORD)
96+
);
97+
98+
await auth.configAuthentication(undefined, 'BAR', undefined);
99+
100+
expect(fs.existsSync(m2Dir)).toBe(true);
101+
expect(fs.existsSync(settingsFile)).toBe(true);
102+
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
103+
auth.generate(auth.DEFAULT_ID, 'BAR', auth.DEFAULT_PASSWORD)
104+
);
105+
106+
await auth.configAuthentication(undefined, undefined, 'BAZ');
107+
108+
expect(fs.existsSync(m2Dir)).toBe(true);
109+
expect(fs.existsSync(settingsFile)).toBe(true);
110+
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
111+
auth.generate(auth.DEFAULT_ID, auth.DEFAULT_USERNAME, 'BAZ')
112+
);
113+
114+
await auth.configAuthentication();
115+
116+
expect(fs.existsSync(m2Dir)).toBe(true);
117+
expect(fs.existsSync(settingsFile)).toBe(true);
118+
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
119+
auth.generate(
120+
auth.DEFAULT_ID,
121+
auth.DEFAULT_USERNAME,
122+
auth.DEFAULT_PASSWORD
123+
)
124+
);
125+
}, 100000);
126+
127+
it('escapes invalid XML inputs', () => {
128+
const id = 'packages';
129+
const username = 'USER';
130+
const password = '&<>"\'\'"><&';
131+
132+
expect(auth.generate(id, username, password)).toEqual(`
133+
<settings>
134+
<servers>
135+
<server>
136+
<id>${id}</id>
137+
<username>\${env.${username}}</username>
138+
<password>\${env.&amp;&lt;&gt;&quot;&apos;&apos;&quot;&gt;&lt;&amp;}</password>
139+
</server>
140+
</servers>
141+
</settings>
142+
`);
143+
});
144+
});

action.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name: 'Setup Java JDK'
2-
description: 'Set up a specific version of the Java JDK and add the command-line tools to the PATH'
2+
description: 'Set up a specific version of the Java JDK and add the
3+
command-line tools to the PATH'
34
author: 'GitHub'
4-
inputs:
5+
inputs:
56
java-version:
6-
description: 'The Java version to make available on the path. Takes a whole or semver Java version, or 1.x syntax (e.g. 1.8 => Java 8.x)'
7+
description: 'The Java version to make available on the path. Takes a whole
8+
or semver Java version, or 1.x syntax (e.g. 1.8 => Java 8.x)'
79
required: true
810
java-package:
911
description: 'The package type (jre, jdk, jdk+fx)'
@@ -14,7 +16,23 @@ inputs:
1416
required: false
1517
default: 'x64'
1618
jdkFile:
17-
description: 'Path to where the compressed JDK is located. The path could be in your source repository or a local path on the agent.'
19+
description: 'Path to where the compressed JDK is located. The path could
20+
be in your source repository or a local path on the agent.'
21+
required: false
22+
server-id:
23+
description: 'ID of the distributionManagement repository in the pom.xml
24+
file. Default is `github`'
25+
required: false
26+
server-username:
27+
description: 'Environment variable name for the username for authentication
28+
to the Apache Maven repository. Default is $GITHUB_ACTOR'
29+
required: false
30+
server-password:
31+
description: 'Environment variable name for password or token for
32+
authentication to the Apache Maven repository. Default is $GITHUB_TOKEN'
33+
required: false
34+
settings-path:
35+
description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
1836
required: false
1937
runs:
2038
using: 'node12'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy