import * as semver from 'semver'; import * as io from '@actions/io'; import * as execm from './exec'; export const isECR = async (registry: string): Promise => { return registry.includes('amazonaws'); }; export const getRegion = async (registry: string): Promise => { return registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws')); }; export const getCLI = async (): Promise => { return io.which('aws', true); }; export const getCLICmdOutput = async (args: string[]): Promise => { return execm.exec(await getCLI(), args, true).then(res => { if (res.stderr != '' && !res.success) { throw new Error(res.stderr); } else if (res.stderr != '') { return res.stderr.trim(); } else { return res.stdout.trim(); } }); }; export const getCLIVersion = async (): Promise => { return parseCLIVersion(await getCLICmdOutput(['--version'])); }; export const parseCLIVersion = async (stdout: string): Promise => { const matches = /aws-cli\/([0-9.]+)/.exec(stdout); 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 => { 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; }); } };