2020-08-21 08:45:16 -04:00
|
|
|
import * as semver from 'semver';
|
|
|
|
import * as io from '@actions/io';
|
|
|
|
import * as execm from './exec';
|
|
|
|
|
2020-12-17 14:21:48 -05:00
|
|
|
const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
|
|
|
|
|
|
|
|
export const isECR = (registry: string): boolean => {
|
|
|
|
return ecrRegistryRegex.test(registry) || isPubECR(registry);
|
2020-12-11 01:15:35 -05:00
|
|
|
};
|
|
|
|
|
2020-12-17 14:21:48 -05:00
|
|
|
export const isPubECR = (registry: string): boolean => {
|
2020-12-11 01:15:35 -05:00
|
|
|
return registry === 'public.ecr.aws';
|
2020-08-21 08:45:16 -04:00
|
|
|
};
|
|
|
|
|
2020-12-17 14:21:48 -05:00
|
|
|
export const getRegion = (registry: string): string => {
|
|
|
|
if (isPubECR(registry)) {
|
2020-12-11 01:15:35 -05:00
|
|
|
return process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';
|
|
|
|
}
|
2020-12-17 14:21:48 -05:00
|
|
|
const matches = registry.match(ecrRegistryRegex);
|
|
|
|
if (!matches) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return matches[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getAccountIDs = (registry: string): string[] => {
|
|
|
|
if (isPubECR(registry)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const matches = registry.match(ecrRegistryRegex);
|
|
|
|
if (!matches) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
let accountIDs: Array<string> = [matches[2]];
|
|
|
|
if (process.env.AWS_ACCOUNT_IDS) {
|
|
|
|
accountIDs.push(...process.env.AWS_ACCOUNT_IDS.split(','));
|
|
|
|
}
|
|
|
|
return accountIDs.filter((item, index) => accountIDs.indexOf(item) === index);
|
2020-08-21 09:07:43 -04:00
|
|
|
};
|
|
|
|
|
2020-08-21 08:45:16 -04:00
|
|
|
export const getCLI = async (): Promise<string> => {
|
|
|
|
return io.which('aws', true);
|
|
|
|
};
|
|
|
|
|
2020-08-21 10:29:54 -04:00
|
|
|
export const execCLI = async (args: string[]): Promise<string> => {
|
2020-08-21 08:56:11 -04:00
|
|
|
return execm.exec(await getCLI(), args, true).then(res => {
|
2020-08-21 08:45:16 -04:00
|
|
|
if (res.stderr != '' && !res.success) {
|
|
|
|
throw new Error(res.stderr);
|
2020-08-21 09:07:43 -04:00
|
|
|
} else if (res.stderr != '') {
|
|
|
|
return res.stderr.trim();
|
|
|
|
} else {
|
|
|
|
return res.stdout.trim();
|
2020-08-21 08:45:16 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-21 09:27:22 -04:00
|
|
|
export const getCLIVersion = async (): Promise<string> => {
|
2020-08-21 10:29:54 -04:00
|
|
|
return parseCLIVersion(await execCLI(['--version']));
|
2020-08-21 08:56:11 -04:00
|
|
|
};
|
|
|
|
|
2020-08-21 09:27:22 -04:00
|
|
|
export const parseCLIVersion = async (stdout: string): Promise<string> => {
|
2020-08-21 08:45:16 -04:00
|
|
|
const matches = /aws-cli\/([0-9.]+)/.exec(stdout);
|
2020-08-21 09:27:22 -04:00
|
|
|
if (!matches) {
|
|
|
|
throw new Error(`Cannot parse AWS CLI version`);
|
|
|
|
}
|
|
|
|
return semver.clean(matches[1]);
|
|
|
|
};
|
|
|
|
|
2020-12-17 14:21:48 -05:00
|
|
|
export const getDockerLoginCmds = async (
|
|
|
|
cliVersion: string,
|
|
|
|
registry: string,
|
|
|
|
region: string,
|
|
|
|
accountIDs: string[]
|
|
|
|
): Promise<string[]> => {
|
2020-12-11 01:15:35 -05:00
|
|
|
let ecrCmd = (await isPubECR(registry)) ? 'ecr-public' : 'ecr';
|
2020-12-17 01:49:50 -05:00
|
|
|
if (semver.satisfies(cliVersion, '>=2.0.0') || (await isPubECR(registry))) {
|
2020-12-11 01:15:35 -05:00
|
|
|
return execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
|
2020-12-16 15:53:24 -05:00
|
|
|
return [`docker login --username AWS --password ${pwd} ${registry}`];
|
2020-08-21 09:27:22 -04:00
|
|
|
});
|
|
|
|
} else {
|
2020-12-17 14:21:48 -05:00
|
|
|
return execCLI([
|
|
|
|
ecrCmd,
|
|
|
|
'get-login',
|
|
|
|
'--region',
|
|
|
|
region,
|
|
|
|
'--registry-ids',
|
|
|
|
accountIDs.join(' '),
|
|
|
|
'--no-include-email'
|
|
|
|
]).then(dockerLoginCmds => {
|
2020-12-16 15:53:24 -05:00
|
|
|
return dockerLoginCmds.trim().split(`\n`);
|
2020-08-21 09:27:22 -04:00
|
|
|
});
|
2020-08-21 08:45:16 -04:00
|
|
|
}
|
|
|
|
};
|