Skip to content

Commit

Permalink
Handle AWS CLI v2
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyMax committed Aug 21, 2020
1 parent 16d491f commit d833f7c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 30 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ on:

jobs:
dockerhub:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-20.04
- ubuntu-18.04
- ubuntu-16.04
logout:
- true
- false
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![GitHub release](https://img.shields.io/github/release/crazy-max/ghaction-docker-login.svg?style=flat-square)](https://github.com/crazy-max/ghaction-docker-login/releases/latest)
[![GitHub marketplace](https://img.shields.io/badge/marketplace-docker--login-blue?logo=github&style=flat-square)](https://github.com/marketplace/actions/docker-login)
[![CI workflow](https://img.shields.io/github/workflow/status/crazy-max/ghaction-docker-login/test?label=ci&logo=github&style=flat-square)](https://github.com/crazy-max/ghaction-docker-login/actions?workflow=ci)
[![Test workflow](https://img.shields.io/github/workflow/status/crazy-max/ghaction-docker-login/test?label=test&logo=github&style=flat-square)](https://github.com/crazy-max/ghaction-docker-login/actions?workflow=test)
[![Codecov](https://img.shields.io/codecov/c/github/crazy-max/ghaction-docker-login?logo=codecov&style=flat-square)](https://codecov.io/gh/crazy-max/ghaction-docker-login)
[![Become a sponsor](https://img.shields.io/badge/sponsor-crazy--max-181717.svg?logo=github&style=flat-square)](https://github.com/sponsors/crazy-max)
Expand Down
39 changes: 25 additions & 14 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions src/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@ export const getCLICmdOutput = async (args: string[]): Promise<string> => {
});
};

export const getCLIVersion = async (): Promise<string | undefined> => {
export const getCLIVersion = async (): Promise<string> => {
return parseCLIVersion(await getCLICmdOutput(['--version']));
};

export const parseCLIVersion = async (stdout: string): Promise<string | undefined> => {
export const parseCLIVersion = async (stdout: string): Promise<string> => {
const matches = /aws-cli\/([0-9.]+)/.exec(stdout);
if (matches) {
return semver.clean(matches[1]);
if (!matches) {
throw new Error(`Cannot parse AWS CLI version`);
}
return semver.clean(matches[1]);
};

export const getECRLoginCmd = async (cliVersion: string, registry: string, region: string): Promise<string> => {
if (semver.satisfies(cliVersion, '>=2.0.0')) {
return getCLICmdOutput(['ecr', 'get-login-password', '--region', region]).then(pwd => {
return `docker login --username AWS --password ${pwd} ${registry}`;
});
} else {
return getCLICmdOutput(['ecr', 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
return dockerLoginCmd;
});
}
return undefined;
};
20 changes: 10 additions & 10 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ export async function loginStandard(registry: string, username: string, password
export async function loginECR(registry: string, username: string, password: string): Promise<void> {
const cliPath = await aws.getCLI();
const cliVersion = await aws.getCLIVersion();
const ecrRegion = await aws.getRegion(registry);
core.info(`💡 AWS ECR registry detected with ${ecrRegion} region`);
const region = await aws.getRegion(registry);
core.info(`💡 AWS ECR registry detected with ${region} region`);

process.env.AWS_ACCESS_KEY_ID = username;
process.env.AWS_SECRET_ACCESS_KEY = password;

core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
aws.getCLICmdOutput(['ecr', 'get-login', '--region', ecrRegion, '--no-include-email']).then(stdout => {
core.info(`🔑 Logging into ${registry}...`);
execm.exec(stdout, [], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
core.info('🎉 Login Succeeded!');
});
const loginCmd = await aws.getECRLoginCmd(cliVersion, registry, region);

core.info(`🔑 Logging into ${registry}...`);
execm.exec(loginCmd, [], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
core.info('🎉 Login Succeeded!');
});
}

0 comments on commit d833f7c

Please sign in to comment.