2020-08-21 14:45:16 +02:00
|
|
|
import * as semver from 'semver';
|
|
|
|
import * as io from '@actions/io';
|
|
|
|
import * as execm from './exec';
|
|
|
|
|
|
|
|
export const isECR = async (registry: string): Promise<boolean> => {
|
2020-12-11 07:15:35 +01:00
|
|
|
return registry.includes('amazonaws') || (await isPubECR(registry));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isPubECR = async (registry: string): Promise<boolean> => {
|
|
|
|
return registry === 'public.ecr.aws';
|
2020-08-21 14:45:16 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 15:07:43 +02:00
|
|
|
export const getRegion = async (registry: string): Promise<string> => {
|
2020-12-11 07:15:35 +01:00
|
|
|
if (await isPubECR(registry)) {
|
|
|
|
return process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';
|
|
|
|
}
|
2020-08-21 15:07:43 +02:00
|
|
|
return registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws'));
|
|
|
|
};
|
|
|
|
|
2020-08-21 14:45:16 +02:00
|
|
|
export const getCLI = async (): Promise<string> => {
|
|
|
|
return io.which('aws', true);
|
|
|
|
};
|
|
|
|
|
2020-08-21 16:29:54 +02:00
|
|
|
export const execCLI = async (args: string[]): Promise<string> => {
|
2020-08-21 14:56:11 +02:00
|
|
|
return execm.exec(await getCLI(), args, true).then(res => {
|
2020-08-21 14:45:16 +02:00
|
|
|
if (res.stderr != '' && !res.success) {
|
|
|
|
throw new Error(res.stderr);
|
2020-08-21 15:07:43 +02:00
|
|
|
} else if (res.stderr != '') {
|
|
|
|
return res.stderr.trim();
|
|
|
|
} else {
|
|
|
|
return res.stdout.trim();
|
2020-08-21 14:45:16 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-21 15:27:22 +02:00
|
|
|
export const getCLIVersion = async (): Promise<string> => {
|
2020-08-21 16:29:54 +02:00
|
|
|
return parseCLIVersion(await execCLI(['--version']));
|
2020-08-21 14:56:11 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 15:27:22 +02:00
|
|
|
export const parseCLIVersion = async (stdout: string): Promise<string> => {
|
2020-08-21 14:45:16 +02:00
|
|
|
const matches = /aws-cli\/([0-9.]+)/.exec(stdout);
|
2020-08-21 15:27:22 +02:00
|
|
|
if (!matches) {
|
|
|
|
throw new Error(`Cannot parse AWS CLI version`);
|
|
|
|
}
|
|
|
|
return semver.clean(matches[1]);
|
|
|
|
};
|
|
|
|
|
2020-12-16 21:53:24 +01:00
|
|
|
export const getDockerLoginCmds = async (cliVersion: string, registry: string, region: string): Promise<string[]> => {
|
2020-12-11 07:15:35 +01:00
|
|
|
let ecrCmd = (await isPubECR(registry)) ? 'ecr-public' : 'ecr';
|
2020-08-21 15:27:22 +02:00
|
|
|
if (semver.satisfies(cliVersion, '>=2.0.0')) {
|
2020-12-11 07:15:35 +01:00
|
|
|
return execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
|
2020-12-16 21:53:24 +01:00
|
|
|
return [`docker login --username AWS --password ${pwd} ${registry}`];
|
2020-08-21 15:27:22 +02:00
|
|
|
});
|
|
|
|
} else {
|
2020-12-16 21:53:24 +01:00
|
|
|
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`);
|
2020-08-21 15:27:22 +02:00
|
|
|
});
|
2020-08-21 14:45:16 +02:00
|
|
|
}
|
|
|
|
};
|