Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c90f042
commit 3e7c6d4
Showing
8 changed files
with
5,849 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"env": { | ||
"commonjs": true, | ||
"es6": true, | ||
"node": true, | ||
"jest": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"rules": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# comment this out distribution branches | ||
node_modules/ | ||
|
||
# Editors | ||
.vscode | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Other Dependency directories | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# next.js build output | ||
.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: 'Amazon ECR "Login" Action for GitHub Actions' | ||
description: 'Logs in the local Docker client to one or more ECR registries' | ||
inputs: | ||
registry-ids: | ||
description: 'A comma-delimited list of AWS account IDs that are associated with the ECR registries. If you do not specify a registry, the default ECR registry is assumed.' | ||
required: false | ||
outputs: | ||
registry: | ||
description: 'The URI of the default ECR registry i.e. aws_account_id.dkr.ecr.region.amazonaws.com, if none were provided as inputs' | ||
runs: | ||
using: 'node12' | ||
main: 'dist/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const core = require('@actions/core'); | ||
const exec = require('@actions/exec'); | ||
const aws = require('aws-sdk'); | ||
|
||
async function run() { | ||
try { | ||
const registries = core.getInput('registries', { required: false }); | ||
|
||
// Get the ECR authorization token | ||
const ecr = new aws.ECR(); | ||
const authTokenRequest = {}; | ||
if (registries) { | ||
authTokenRequest.registryIds = registries.split(',') | ||
} | ||
const authTokenResponse = await ecr.getAuthorizationToken(authTokenRequest).promise(); | ||
if (!Array.isArray(authTokenResponse.authorizationData) || !authTokenResponse.authorizationData.length) { | ||
throw new Error('Could not retrieve an authorization token from Amazon ECR'); | ||
} | ||
|
||
for (const authData of authTokenResponse.authorizationData) { | ||
const authToken = Buffer.from(authData.authorizationToken, 'base64').toString('utf-8'); | ||
const creds = authToken.split(':', 2); | ||
const proxyEndpoint = authData.proxyEndpoint; | ||
|
||
if (!registries) { | ||
// output the default registry if none were provided | ||
const registryId = proxyEndpoint.replace(/^https?:\/\//,''); | ||
core.setOutput('registry', registryId); | ||
} | ||
|
||
// Execute the docker login command | ||
let doLoginStdout = ''; | ||
let doLoginStderr = ''; | ||
const exitCode = await exec.exec('docker login', ['-u', creds[0], '-p', creds[1], proxyEndpoint], { | ||
silent: true, | ||
ignoreReturnCode: true, | ||
listeners: { | ||
stdout: (data) => { | ||
doLoginStdout += data.toString(); | ||
}, | ||
stderr: (data) => { | ||
doLoginStderr += data.toString(); | ||
} | ||
} | ||
}); | ||
|
||
if (exitCode != 0) { | ||
core.debug(doLoginStdout); | ||
throw new Error('Could not login: ' + doLoginStderr); | ||
} | ||
} | ||
} | ||
catch (error) { | ||
core.setFailed(error.message); | ||
} | ||
} | ||
|
||
module.exports = run; | ||
|
||
/* istanbul ignore next */ | ||
if (require.main === module) { | ||
run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const run = require('.'); | ||
const core = require('@actions/core'); | ||
const exec = require('@actions/exec'); | ||
|
||
jest.mock('@actions/core'); | ||
jest.mock('@actions/exec'); | ||
|
||
const mockEcrGetAuthToken = jest.fn(); | ||
jest.mock('aws-sdk', () => { | ||
return { | ||
ECR: jest.fn(() => ({ | ||
getAuthorizationToken: mockEcrGetAuthToken | ||
})) | ||
}; | ||
}); | ||
|
||
describe('Login to ECR', () => { | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
mockEcrGetAuthToken.mockImplementation((params) => { | ||
return { | ||
promise() { | ||
return Promise.resolve({ | ||
authorizationData: [ | ||
{ | ||
authorizationToken: Buffer.from('hello:world').toString('base64'), | ||
proxyEndpoint: 'https://123456789012.dkr.ecr.aws-region-1.amazonaws.com' | ||
} | ||
] | ||
}); | ||
} | ||
}; | ||
}); | ||
|
||
exec.exec.mockReturnValue(0); | ||
}); | ||
|
||
test('gets auth token from ECR and logins the Docker client for the default registry', async () => { | ||
await run(); | ||
expect(mockEcrGetAuthToken).toHaveBeenCalled(); | ||
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'registry', '123456789012.dkr.ecr.aws-region-1.amazonaws.com'); | ||
expect(exec.exec).toHaveBeenNthCalledWith(1, | ||
'docker login', | ||
['-u', 'hello', '-p', 'world', 'https://123456789012.dkr.ecr.aws-region-1.amazonaws.com'], | ||
expect.anything()); | ||
}); | ||
|
||
test('gets auth token from ECR and logins the Docker client for each provided registry', async () => { | ||
core.getInput = jest.fn().mockReturnValueOnce('123456789012,111111111111'); | ||
mockEcrGetAuthToken.mockImplementation((params) => { | ||
return { | ||
promise() { | ||
return Promise.resolve({ | ||
authorizationData: [ | ||
{ | ||
authorizationToken: Buffer.from('hello:world').toString('base64'), | ||
proxyEndpoint: 'https://123456789012.dkr.ecr.aws-region-1.amazonaws.com' | ||
}, | ||
{ | ||
authorizationToken: Buffer.from('foo:bar').toString('base64'), | ||
proxyEndpoint: 'https://111111111111.dkr.ecr.aws-region-1.amazonaws.com' | ||
} | ||
] | ||
}); | ||
} | ||
}; | ||
}); | ||
|
||
await run(); | ||
|
||
expect(mockEcrGetAuthToken).toHaveBeenCalledWith({ | ||
registryIds: ['123456789012','111111111111'] | ||
}); | ||
expect(core.setOutput).toHaveBeenCalledTimes(0); | ||
expect(exec.exec).toHaveBeenCalledTimes(2); | ||
expect(exec.exec).toHaveBeenNthCalledWith(1, | ||
'docker login', | ||
['-u', 'hello', '-p', 'world', 'https://123456789012.dkr.ecr.aws-region-1.amazonaws.com'], | ||
expect.anything()); | ||
expect(exec.exec).toHaveBeenNthCalledWith(2, | ||
'docker login', | ||
['-u', 'foo', '-p', 'bar', 'https://111111111111.dkr.ecr.aws-region-1.amazonaws.com'], | ||
expect.anything()); | ||
}); | ||
|
||
test('error is caught by core.setFailed for failed docker login', async () => { | ||
exec.exec.mockReturnValue(1); | ||
|
||
await run(); | ||
|
||
expect(core.setFailed).toBeCalled(); | ||
}); | ||
|
||
test('error is caught by core.setFailed for ECR call', async () => { | ||
mockEcrGetAuthToken.mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
|
||
await run(); | ||
|
||
expect(core.setFailed).toBeCalled(); | ||
}); | ||
}); |
Oops, something went wrong.