Files
login-action/src/main.ts

70 lines
1.8 KiB
TypeScript
Raw Normal View History

import * as yaml from 'js-yaml';
import * as core from '@actions/core';
import * as actionsToolkit from '@docker/actions-toolkit';
import * as context from './context';
2020-08-20 16:40:33 +02:00
import * as docker from './docker';
2020-08-15 14:45:36 +02:00
import * as stateHelper from './state-helper';
interface Auth {
registry?: string;
username: string;
password: string;
ecr?: string;
}
export async function main(): Promise<void> {
const inputs: context.Inputs = context.getInputs();
stateHelper.setLogout(inputs.logout);
const auths: Array<Auth> = [];
if (inputs.registry || inputs.username) {
auths.push({
registry: inputs.registry,
username: inputs.username,
password: inputs.password,
ecr: inputs.ecr
});
}
const add = yaml.load(inputs.add) as Auth[];
core.info(`add: ${JSON.stringify(add, null, 2)}`);
if (Array.isArray(add)) {
auths.push(...add);
}
const registries: string[] = [];
for (const auth of auths) {
if (!auth.registry) {
registries.push('docker.io');
} else {
registries.push(auth.registry);
}
}
stateHelper.setRegistries(registries);
if (auths.length == 0) {
throw new Error('No registry to login');
}
if (auths.length === 1) {
await docker.login(auths[0].registry || 'docker.io', auths[0].username, auths[0].password, auths[0].ecr || 'auto');
} else {
for (const auth of auths) {
await core.group(`Login to ${auth.registry || 'docker.io'}`, async () => {
await docker.login(auth.registry || 'docker.io', auth.username, auth.password, auth.ecr || 'auto');
});
}
}
2020-08-15 14:45:36 +02:00
}
async function post(): Promise<void> {
2020-08-15 14:45:36 +02:00
if (!stateHelper.logout) {
return;
}
for (const registry of stateHelper.registries.split(',')) {
await docker.logout(registry);
}
2020-08-15 14:45:36 +02:00
}
actionsToolkit.run(main, post);