1
0
Fork 0
mirror of https://github.com/docker/login-action.git synced 2025-04-27 17:16:35 +02:00

Handle Amazon ECR registries associated with other accounts

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-12-16 21:53:24 +01:00
parent 3b14bab101
commit d3160f671f
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
4 changed files with 77 additions and 54 deletions

View file

@ -45,15 +45,19 @@ export const parseCLIVersion = async (stdout: string): Promise<string> => {
return semver.clean(matches[1]);
};
export const getDockerLoginCmd = async (cliVersion: string, registry: string, region: string): Promise<string> => {
export const getDockerLoginCmds = async (cliVersion: string, registry: string, region: string): Promise<string[]> => {
let ecrCmd = (await isPubECR(registry)) ? 'ecr-public' : 'ecr';
if (semver.satisfies(cliVersion, '>=2.0.0')) {
return execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
return `docker login --username AWS --password ${pwd} ${registry}`;
return [`docker login --username AWS --password ${pwd} ${registry}`];
});
} else {
return execCLI([ecrCmd, 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
return dockerLoginCmd;
let args: Array<string> = [ecrCmd, 'get-login', '--region', region, '--no-include-email'];
if (process.env.AWS_ECR_REGISTRY_IDS) {
args.push('--registry-ids', process.env.AWS_ECR_REGISTRY_IDS);
}
return execCLI(args).then(dockerLoginCmds => {
return dockerLoginCmds.trim().split(`\n`);
});
}
};

View file

@ -55,13 +55,19 @@ export async function loginECR(registry: string, username: string, password: str
process.env.AWS_SECRET_ACCESS_KEY = password || process.env.AWS_SECRET_ACCESS_KEY;
core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
const loginCmd = await aws.getDockerLoginCmd(cliVersion, registry, region);
const loginCmds = await aws.getDockerLoginCmds(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!');
loginCmds.forEach((loginCmd, index) => {
execm.exec(loginCmd, [], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
if (loginCmds.length > 1) {
core.info(`🎉 Login Succeeded! (${index}/${loginCmds.length})`);
} else {
core.info('🎉 Login Succeeded!');
}
});
});
}