1
0
Fork 0
mirror of https://github.com/docker/login-action.git synced 2025-04-22 22:56:36 +02:00

Get AccountID from registry URL and handle ECR registry through regexp

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-12-17 20:21:48 +01:00
parent d3160f671f
commit 24646ef465
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
5 changed files with 122 additions and 34 deletions

58
dist/index.js generated vendored
View file

@ -3088,6 +3088,7 @@ function loginECR(registry, username, password) {
const cliPath = yield aws.getCLI();
const cliVersion = yield aws.getCLIVersion();
const region = yield aws.getRegion(registry);
const accountIDs = yield aws.getAccountIDs(registry);
if (yield aws.isPubECR(registry)) {
core.info(`💡 AWS Public ECR detected with ${region} region`);
}
@ -3097,7 +3098,7 @@ function loginECR(registry, username, password) {
process.env.AWS_ACCESS_KEY_ID = username || process.env.AWS_ACCESS_KEY_ID;
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 loginCmds = yield aws.getDockerLoginCmds(cliVersion, registry, region);
const loginCmds = yield aws.getDockerLoginCmds(cliVersion, registry, region, accountIDs);
core.info(`🔑 Logging into ${registry}...`);
loginCmds.forEach((loginCmd, index) => {
execm.exec(loginCmd, [], true).then(res => {
@ -4167,22 +4168,41 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDockerLoginCmds = exports.parseCLIVersion = exports.getCLIVersion = exports.execCLI = exports.getCLI = exports.getRegion = exports.isPubECR = exports.isECR = void 0;
exports.getDockerLoginCmds = exports.parseCLIVersion = exports.getCLIVersion = exports.execCLI = exports.getCLI = exports.getAccountIDs = exports.getRegion = exports.isPubECR = exports.isECR = void 0;
const semver = __importStar(__webpack_require__(383));
const io = __importStar(__webpack_require__(436));
const execm = __importStar(__webpack_require__(757));
exports.isECR = (registry) => __awaiter(void 0, void 0, void 0, function* () {
return registry.includes('amazonaws') || (yield exports.isPubECR(registry));
});
exports.isPubECR = (registry) => __awaiter(void 0, void 0, void 0, function* () {
const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
exports.isECR = (registry) => {
return ecrRegistryRegex.test(registry) || exports.isPubECR(registry);
};
exports.isPubECR = (registry) => {
return registry === 'public.ecr.aws';
});
exports.getRegion = (registry) => __awaiter(void 0, void 0, void 0, function* () {
if (yield exports.isPubECR(registry)) {
};
exports.getRegion = (registry) => {
if (exports.isPubECR(registry)) {
return process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';
}
return registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws'));
});
const matches = registry.match(ecrRegistryRegex);
if (!matches) {
return '';
}
return matches[3];
};
exports.getAccountIDs = (registry) => {
if (exports.isPubECR(registry)) {
return [];
}
const matches = registry.match(ecrRegistryRegex);
if (!matches) {
return [];
}
let accountIDs = [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);
};
exports.getCLI = () => __awaiter(void 0, void 0, void 0, function* () {
return io.which('aws', true);
});
@ -4209,7 +4229,7 @@ exports.parseCLIVersion = (stdout) => __awaiter(void 0, void 0, void 0, function
}
return semver.clean(matches[1]);
});
exports.getDockerLoginCmds = (cliVersion, registry, region) => __awaiter(void 0, void 0, void 0, function* () {
exports.getDockerLoginCmds = (cliVersion, registry, region, accountIDs) => __awaiter(void 0, void 0, void 0, function* () {
let ecrCmd = (yield exports.isPubECR(registry)) ? 'ecr-public' : 'ecr';
if (semver.satisfies(cliVersion, '>=2.0.0')) {
return exports.execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
@ -4217,11 +4237,15 @@ exports.getDockerLoginCmds = (cliVersion, registry, region) => __awaiter(void 0,
});
}
else {
let args = [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 exports.execCLI(args).then(dockerLoginCmds => {
return exports.execCLI([
ecrCmd,
'get-login',
'--region',
region,
'--registry-ids',
accountIDs.join(' '),
'--no-include-email'
]).then(dockerLoginCmds => {
return dockerLoginCmds.trim().split(`\n`);
});
}