1
0
Fork 0
mirror of https://github.com/docker/login-action.git synced 2025-04-27 17:16:35 +02:00

Use built-in getExecOutput

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2021-06-22 11:09:26 +02:00
parent 4608add020
commit 39efbd2c12
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
5 changed files with 107 additions and 170 deletions

View file

@ -1,6 +1,6 @@
import * as semver from 'semver';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as execm from './exec';
const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
@ -43,15 +43,20 @@ export const getCLI = async (): Promise<string> => {
};
export const execCLI = async (args: string[]): Promise<string> => {
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();
}
});
return exec
.getExecOutput(await getCLI(), args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
} else if (res.stderr.length > 0) {
return res.stderr.trim();
} else {
return res.stdout.trim();
}
});
};
export const getCLIVersion = async (): Promise<string> => {

View file

@ -1,6 +1,6 @@
import * as core from '@actions/core';
import * as aws from './aws';
import * as execm from './exec';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
export async function login(registry: string, username: string, password: string): Promise<void> {
if (await aws.isECR(registry)) {
@ -11,11 +11,15 @@ export async function login(registry: string, username: string, password: string
}
export async function logout(registry: string): Promise<void> {
await execm.exec('docker', ['logout', registry], false).then(res => {
if (res.stderr != '' && !res.success) {
core.warning(res.stderr);
}
});
await exec
.getExecOutput('docker', ['logout', registry], {
ignoreReturnCode: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
}
});
}
export async function loginStandard(registry: string, username: string, password: string): Promise<void> {
@ -32,12 +36,18 @@ export async function loginStandard(registry: string, username: string, password
} else {
core.info(`Logging into Docker Hub...`);
}
await execm.exec('docker', loginArgs, true, password).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
core.info(`Login Succeeded!`);
});
await exec
.getExecOutput('docker', loginArgs, {
ignoreReturnCode: true,
silent: true,
input: Buffer.from(password)
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
core.info(`Login Succeeded!`);
});
}
export async function loginECR(registry: string, username: string, password: string): Promise<void> {
@ -60,15 +70,20 @@ export async function loginECR(registry: string, username: string, password: str
core.info(`Logging into ${registry}...`);
loginCmds.forEach((loginCmd, index) => {
execm.exec(loginCmd, [], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
if (loginCmds.length > 1) {
core.info(`Login Succeeded! (${index}/${loginCmds.length})`);
} else {
core.info('Login Succeeded!');
}
});
exec
.getExecOutput(loginCmd, [], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
if (loginCmds.length > 1) {
core.info(`Login Succeeded! (${index}/${loginCmds.length})`);
} else {
core.info('Login Succeeded!');
}
});
});
}

View file

@ -1,40 +0,0 @@
import * as actionsExec from '@actions/exec';
import {ExecOptions} from '@actions/exec';
export interface ExecResult {
success: boolean;
stdout: string;
stderr: string;
}
export const exec = async (
command: string,
args: string[] = [],
silent: boolean,
stdin?: string
): Promise<ExecResult> => {
let stdout: string = '';
let stderr: string = '';
const options: ExecOptions = {
silent: silent,
ignoreReturnCode: true,
input: Buffer.from(stdin || '')
};
options.listeners = {
stdout: (data: Buffer) => {
stdout += data.toString();
},
stderr: (data: Buffer) => {
stderr += data.toString();
}
};
const returnCode: number = await actionsExec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
};