Skip to content

Commit adb7347

Browse files
authored
Merge pull request #21 from crazy-max/handle-aws-env
Handle AWS credentials
2 parents 39ef12f + 5df5104 commit adb7347

File tree

9 files changed

+82
-47
lines changed

9 files changed

+82
-47
lines changed

.github/docker-login.png

-13 Bytes
Loading

.github/workflows/ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,34 @@ jobs:
117117
# if: always()
118118
# run: |
119119
# rm -f ${HOME}/.docker/config.json
120+
#
121+
# ecr-aws-creds:
122+
# runs-on: ${{ matrix.os }}
123+
# strategy:
124+
# fail-fast: false
125+
# matrix:
126+
# os:
127+
# - ubuntu-20.04
128+
# - ubuntu-18.04
129+
# - ubuntu-16.04
130+
# steps:
131+
# -
132+
# name: Checkout
133+
# uses: actions/checkout@v2.3.1
134+
# -
135+
# name: Configure AWS Credentials
136+
# uses: aws-actions/configure-aws-credentials@v1
137+
# with:
138+
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
139+
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
140+
# aws-region: ${{ secrets.AWS_REGION }}
141+
# -
142+
# name: Login to ECR
143+
# uses: ./
144+
# with:
145+
# registry: ${{ secrets.AWS_ACCOUNT_NUMBER }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com
146+
# -
147+
# name: Clear
148+
# if: always()
149+
# run: |
150+
# rm -f ${HOME}/.docker/config.json

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,34 @@ jobs:
213213
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
214214
```
215215

216+
You can also use the [Configure AWS Credentials](https://github.com/aws-actions/configure-aws-credentials) action in
217+
combination with this action:
218+
219+
```yaml
220+
name: ci
221+
222+
on:
223+
push:
224+
branches: master
225+
226+
jobs:
227+
login:
228+
runs-on: ubuntu-latest
229+
steps:
230+
-
231+
name: Configure AWS Credentials
232+
uses: aws-actions/configure-aws-credentials@v1
233+
with:
234+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
235+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
236+
aws-region: <region>
237+
-
238+
name: Login to ECR
239+
uses: docker/login-action@v1
240+
with:
241+
registry: <aws-account-number>.dkr.ecr.<region>.amazonaws.com
242+
```
243+
216244
> Replace `<aws-account-number>` and `<region>` with their respective values.
217245

218246
## Customizing

__tests__/context.test.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,7 @@ import osm = require('os');
22

33
import {getInputs} from '../src/context';
44

5-
test('without username getInputs throws errors', async () => {
6-
expect(() => {
7-
getInputs();
8-
}).toThrowError('Input required and not supplied: username');
9-
});
10-
11-
test('without password getInputs throws errors', async () => {
12-
process.env['INPUT_USERNAME'] = 'dbowie';
13-
expect(() => {
14-
getInputs();
15-
}).toThrowError('Input required and not supplied: password');
16-
});
17-
18-
test('with password and username getInputs does not error', async () => {
5+
test('with password and username getInputs does not throw error', async () => {
196
process.env['INPUT_USERNAME'] = 'dbowie';
207
process.env['INPUT_PASSWORD'] = 'groundcontrol';
218
expect(() => {

__tests__/main.test.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,15 @@ test('errors when not run on linux platform', async () => {
1717
expect(coreSpy).toHaveBeenCalledWith('Only supported on linux platform');
1818
});
1919

20-
test('errors without username', async () => {
20+
test('errors without username and password', async () => {
2121
const platSpy = jest.spyOn(osm, 'platform');
2222
platSpy.mockImplementation(() => 'linux');
2323

2424
const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed');
2525

2626
await run();
2727

28-
expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: username');
29-
});
30-
31-
test('errors without password', async () => {
32-
const platSpy = jest.spyOn(osm, 'platform');
33-
platSpy.mockImplementation(() => 'linux');
34-
35-
const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed');
36-
37-
const username: string = 'dbowie';
38-
process.env[`INPUT_USERNAME`] = username;
39-
40-
await run();
41-
42-
expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: password');
28+
expect(coreSpy).toHaveBeenCalledWith('Username and password required');
4329
});
4430

4531
test('successful with username and password', async () => {
@@ -79,7 +65,7 @@ test('calls docker login', async () => {
7965
const password: string = 'groundcontrol';
8066
process.env[`INPUT_PASSWORD`] = password;
8167

82-
const registry: string = 'https://ghcr.io';
68+
const registry: string = 'ghcr.io';
8369
process.env[`INPUT_REGISTRY`] = registry;
8470

8571
const logout: string = 'true';

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ inputs:
1212
required: false
1313
username:
1414
description: 'Username used to log against the Docker registry'
15-
required: true
15+
required: false
1616
password:
1717
description: 'Password or personal access token used to log against the Docker registry'
18-
required: true
18+
required: false
1919
logout:
2020
description: 'Log out from the Docker registry at the end of a job'
2121
default: 'true'

dist/index.js

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

src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export interface Inputs {
1010
export function getInputs(): Inputs {
1111
return {
1212
registry: core.getInput('registry'),
13-
username: core.getInput('username', {required: true}),
14-
password: core.getInput('password', {required: true}),
13+
username: core.getInput('username'),
14+
password: core.getInput('password'),
1515
logout: core.getInput('logout')
1616
};
1717
}

src/docker.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ export async function logout(registry: string): Promise<void> {
1919
}
2020

2121
export async function loginStandard(registry: string, username: string, password: string): Promise<void> {
22-
let loginArgs: Array<string> = ['login', '--password-stdin'];
23-
if (username) {
24-
loginArgs.push('--username', username);
22+
if (!username || !password) {
23+
throw new Error('Username and password required');
2524
}
25+
26+
let loginArgs: Array<string> = ['login', '--password-stdin'];
27+
loginArgs.push('--username', username);
2628
loginArgs.push(registry);
2729

2830
if (registry) {
@@ -44,8 +46,8 @@ export async function loginECR(registry: string, username: string, password: str
4446
const region = await aws.getRegion(registry);
4547
core.info(`💡 AWS ECR detected with ${region} region`);
4648

47-
process.env.AWS_ACCESS_KEY_ID = username;
48-
process.env.AWS_SECRET_ACCESS_KEY = password;
49+
process.env.AWS_ACCESS_KEY_ID = username || process.env.AWS_ACCESS_KEY_ID;
50+
process.env.AWS_SECRET_ACCESS_KEY = password || process.env.AWS_SECRET_ACCESS_KEY;
4951

5052
core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
5153
const loginCmd = await aws.getDockerLoginCmd(cliVersion, registry, region);

0 commit comments

Comments
 (0)