Skip to content

Commit 1bba665

Browse files
author
Bryan Clark
committed
Try env variables
1 parent 6924f73 commit 1bba665

6 files changed

Lines changed: 90 additions & 83 deletions

File tree

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ jobs:
6969
uses: actions/setup-java@v1
7070
with:
7171
java-version: 1.8
72-
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
73-
username: ${{ github.actor }} # username for server authentication
74-
password: ${{ github.token }} # password or token for authentication
72+
env:
73+
GITHUB_TOKEN: ${{ github.token }}
7574

7675
- name: Build with Maven
7776
run: mvn -B package --file pom.xml
@@ -83,9 +82,12 @@ jobs:
8382
uses: actions/setup-java@v1
8483
with: # running setup-java again overwrites the settings.xml
8584
java-version: 1.8
86-
server-id: maven
87-
server-username: maven_username
88-
server-password: ${{ secrets.MAVEN_CENTRAL_TOKEN }} # password from secrets store
85+
server-id: maven # Value of the distributionManagement/repository/id field of the pom.xml
86+
server-username: MAVEN_USERNAME # env variable for username below
87+
server-password: MAVEN_CENTRAL_TOKEN # env variable for token below
88+
env:
89+
MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_TOKEN }}
90+
MAVEN_USERNAME: maven_username123
8991

9092
- name: Publish to Apache Maven Central
9193
run: mvn deploy
@@ -139,9 +141,9 @@ jobs:
139141
with:
140142
java-version: 1.8
141143
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
142-
server-username: ${{ github.actor }} # username for server authentication
143-
server-password: ${{ github.token }} # password or token for authentication
144144
settings-path: ${{ github.workspace }} # location for the settings.xml file
145+
env:
146+
GITHUB_TOKEN: ${{ github.token }}
145147
146148
- name: Build with Maven
147149
run: mvn -B package --file pom.xml

__tests__/auth.test.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ describe('auth tests', () => {
3030

3131
it('creates settings.xml in alternate locations', async () => {
3232
const id = 'packages';
33-
const username = 'bluebottle';
34-
const password = 'SingleOrigin';
33+
const username = 'UNAMI';
34+
const password = 'TOLKIEN';
3535

3636
const altHome = path.join(__dirname, 'runner', 'settings');
3737
const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE);
@@ -55,8 +55,8 @@ describe('auth tests', () => {
5555

5656
it('creates settings.xml with username and password', async () => {
5757
const id = 'packages';
58-
const username = 'bluebottle';
59-
const password = 'SingleOrigin';
58+
const username = 'UNAME';
59+
const password = 'TOKEN';
6060

6161
await auth.configAuthentication(id, username, password);
6262

@@ -69,8 +69,8 @@ describe('auth tests', () => {
6969

7070
it('overwrites existing settings.xml files', async () => {
7171
const id = 'packages';
72-
const username = 'bluebottle';
73-
const password = 'SingleOrigin';
72+
const username = 'USERNAME';
73+
const password = 'PASSWORD';
7474

7575
fs.mkdirSync(m2Dir, {recursive: true});
7676
fs.writeFileSync(settingsFile, 'FAKE FILE');
@@ -87,39 +87,51 @@ describe('auth tests', () => {
8787
}, 100000);
8888

8989
it('does not create settings.xml without required parameters', async () => {
90-
await auth.configAuthentication('FOO', '', '');
90+
await auth.configAuthentication('FOO');
9191

92-
expect(fs.existsSync(m2Dir)).toBe(false);
93-
expect(fs.existsSync(settingsFile)).toBe(false);
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')
96+
);
9497

95-
await auth.configAuthentication('', 'BAR', '');
98+
await auth.configAuthentication(undefined, 'BAR', undefined);
9699

97-
expect(fs.existsSync(m2Dir)).toBe(false);
98-
expect(fs.existsSync(settingsFile)).toBe(false);
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(undefined, 'BAR', undefined)
104+
);
99105

100-
await auth.configAuthentication('', '', 'BAZ');
106+
await auth.configAuthentication(undefined, undefined, 'BAZ');
101107

102-
expect(fs.existsSync(m2Dir)).toBe(false);
103-
expect(fs.existsSync(settingsFile)).toBe(false);
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(undefined, undefined, 'BAZ')
112+
);
104113

105-
await auth.configAuthentication('', '', '');
114+
await auth.configAuthentication();
106115

107-
expect(fs.existsSync(m2Dir)).toBe(false);
108-
expect(fs.existsSync(settingsFile)).toBe(false);
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(undefined, undefined, undefined)
120+
);
109121
}, 100000);
110122

111123
it('escapes invalid XML inputs', () => {
112124
const id = 'packages';
113-
const username = 'bluebottle';
125+
const username = 'USER';
114126
const password = '&<>"\'\'"><&';
115127

116128
expect(auth.generate(id, username, password)).toEqual(`
117129
<settings>
118130
<servers>
119131
<server>
120132
<id>${id}</id>
121-
<username>${username}</username>
122-
<password>&amp;&lt;&gt;&quot;&apos;&apos;&quot;&gt;&lt;&amp;</password>
133+
<username>\${env.${username}}</username>
134+
<password>\${env.&amp;&lt;&gt;&quot;&apos;&apos;&quot;&gt;&lt;&amp;}</password>
123135
</server>
124136
</servers>
125137
</settings>

action.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ inputs:
2424
file.'
2525
required: false
2626
server-username:
27-
description: 'Username for authentication to the Apache Maven repository.'
27+
description: 'Environment variable name for the username for authentication
28+
to the Apache Maven repository.'
2829
required: false
2930
server-password:
30-
description: 'Password or token for authentication to the Apache Maven
31-
repository.'
31+
description: 'Environment variable name for password or token for
32+
authentication to the Apache Maven repository.'
3233
required: false
3334
settings-path:
3435
description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'

dist/index.js

Lines changed: 15 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/auth.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@ import * as io from '@actions/io';
77
export const M2_DIR = '.m2';
88
export const SETTINGS_FILE = 'settings.xml';
99

10+
export const DEFAULT_ID = 'github';
11+
export const DEFAULT_USERNAME = 'GITHUB_ACTOR';
12+
export const DEFAULT_PASSWORD = 'GITHUB_TOKEN';
13+
1014
export async function configAuthentication(
11-
id: string,
12-
username: string,
13-
password: string
15+
id = DEFAULT_ID,
16+
username = DEFAULT_USERNAME,
17+
password = DEFAULT_PASSWORD
1418
) {
15-
if (id && username && password) {
16-
console.log(
17-
`creating ${SETTINGS_FILE} with server-id: ${id}, username: ${username}, and a password`
18-
);
19-
// when an alternate m2 location is specified use only that location (no .m2 directory)
20-
// otherwise use the home/.m2/ path
21-
const directory: string = path.join(
22-
core.getInput('settings-path') || os.homedir(),
23-
core.getInput('settings-path') ? '' : M2_DIR
24-
);
25-
await io.mkdirP(directory);
26-
core.debug(`created directory ${directory}`);
27-
await write(directory, generate(id, username, password));
28-
} else {
29-
core.debug(
30-
`no ${SETTINGS_FILE} without server-id: ${id}, username: ${username}, and a password`
31-
);
32-
}
19+
console.log(
20+
`creating ${SETTINGS_FILE} with server-id: ${id};`,
21+
`environment variables: username=\$${username} and password=\$${password}`
22+
);
23+
// when an alternate m2 location is specified use only that location (no .m2 directory)
24+
// otherwise use the home/.m2/ path
25+
const directory: string = path.join(
26+
core.getInput('settings-path') || os.homedir(),
27+
core.getInput('settings-path') ? '' : M2_DIR
28+
);
29+
await io.mkdirP(directory);
30+
core.debug(`created directory ${directory}`);
31+
await write(directory, generate(id, username, password));
3332
}
3433

3534
function escapeXML(value: string) {
@@ -42,14 +41,18 @@ function escapeXML(value: string) {
4241
}
4342

4443
// only exported for testing purposes
45-
export function generate(id: string, username: string, password: string) {
44+
export function generate(
45+
id = DEFAULT_ID,
46+
username = DEFAULT_USERNAME,
47+
password = DEFAULT_PASSWORD
48+
) {
4649
return `
4750
<settings>
4851
<servers>
4952
<server>
5053
<id>${escapeXML(id)}</id>
51-
<username>${escapeXML(username)}</username>
52-
<password>${escapeXML(password)}</password>
54+
<username>\${env.${escapeXML(username)}}</username>
55+
<password>\${env.${escapeXML(password)}}</password>
5356
</server>
5457
</servers>
5558
</settings>

src/setup-java.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ async function run() {
2222
const username = core.getInput('server-username', {required: false});
2323
const password = core.getInput('server-password', {required: false});
2424

25-
if (id && username && password) {
26-
await auth.configAuthentication(id, username, password);
27-
} else if (id || username || password) {
28-
console.warn('All 3 server-(id, username, and password) are required.');
29-
}
25+
await auth.configAuthentication(id, username, password);
3026
} catch (error) {
3127
core.setFailed(error.message);
3228
}

0 commit comments

Comments
 (0)