mirror of
https://github.com/docker/login-action.git
synced 2025-11-09 20:26:22 +08:00
multiple registries support
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -6,6 +6,7 @@ export interface Inputs {
|
||||
password: string;
|
||||
ecr: string;
|
||||
logout: boolean;
|
||||
add: string;
|
||||
}
|
||||
|
||||
export function getInputs(): Inputs {
|
||||
@@ -14,6 +15,7 @@ export function getInputs(): Inputs {
|
||||
username: core.getInput('username'),
|
||||
password: core.getInput('password'),
|
||||
ecr: core.getInput('ecr'),
|
||||
logout: core.getBooleanInput('logout')
|
||||
logout: core.getBooleanInput('logout'),
|
||||
add: core.getInput('add')
|
||||
};
|
||||
}
|
||||
|
||||
57
src/main.ts
57
src/main.ts
@@ -1,21 +1,68 @@
|
||||
import * as yaml from 'js-yaml';
|
||||
import * as core from '@actions/core';
|
||||
import * as actionsToolkit from '@docker/actions-toolkit';
|
||||
|
||||
import * as context from './context';
|
||||
import * as docker from './docker';
|
||||
import * as stateHelper from './state-helper';
|
||||
|
||||
interface Auth {
|
||||
registry: string;
|
||||
username: string;
|
||||
password: string;
|
||||
ecr: string;
|
||||
}
|
||||
|
||||
export async function main(): Promise<void> {
|
||||
const input: context.Inputs = context.getInputs();
|
||||
stateHelper.setRegistry(input.registry);
|
||||
stateHelper.setLogout(input.logout);
|
||||
await docker.login(input.registry, input.username, input.password, input.ecr);
|
||||
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[];
|
||||
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, 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, auth.username, auth.password, auth.ecr || 'auto');
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function post(): Promise<void> {
|
||||
if (!stateHelper.logout) {
|
||||
return;
|
||||
}
|
||||
await docker.logout(stateHelper.registry);
|
||||
for (const registry of stateHelper.registries.split(',')) {
|
||||
await docker.logout(registry);
|
||||
}
|
||||
}
|
||||
|
||||
actionsToolkit.run(main, post);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import * as core from '@actions/core';
|
||||
|
||||
export const registry = process.env['STATE_registry'] || '';
|
||||
export const registries = process.env['STATE_registries'] || '';
|
||||
export const logout = /true/i.test(process.env['STATE_logout'] || '');
|
||||
|
||||
export function setRegistry(registry: string) {
|
||||
core.saveState('registry', registry);
|
||||
export function setRegistries(registries: string[]) {
|
||||
core.saveState('registries', registries.join(','));
|
||||
}
|
||||
|
||||
export function setLogout(logout: boolean) {
|
||||
|
||||
Reference in New Issue
Block a user