switch to Docker exec

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-07-22 10:41:37 +02:00
parent be5150d9fe
commit 668190adc5
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
2 changed files with 23 additions and 9 deletions

View File

@ -2,14 +2,15 @@ import {expect, jest, test} from '@jest/globals';
import * as path from 'path'; import * as path from 'path';
import {loginStandard, logout} from '../src/docker'; import {loginStandard, logout} from '../src/docker';
import {Exec} from '@docker/actions-toolkit/lib/exec';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner'); process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner');
test('loginStandard calls exec', async () => { test('loginStandard calls exec', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
const execSpy = jest.spyOn(Exec, 'getExecOutput').mockImplementation(async () => { const execSpy = jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
return { return {
exitCode: expect.any(Number), exitCode: expect.any(Number),
stdout: expect.any(Function), stdout: expect.any(Function),
@ -23,7 +24,13 @@ test('loginStandard calls exec', async () => {
await loginStandard(registry, username, password); await loginStandard(registry, username, password);
expect(execSpy).toHaveBeenCalledWith(`docker`, ['login', '--password-stdin', '--username', username, registry], { expect(execSpy).toHaveBeenCalledTimes(1);
const callfunc = execSpy.mock.calls[0];
if (callfunc && callfunc[1]) {
// we don't want to check env opt
callfunc[1].env = undefined;
}
expect(execSpy).toHaveBeenCalledWith(['login', '--password-stdin', '--username', username, registry], {
input: Buffer.from(password), input: Buffer.from(password),
silent: true, silent: true,
ignoreReturnCode: true ignoreReturnCode: true
@ -33,7 +40,7 @@ test('loginStandard calls exec', async () => {
test('logout calls exec', async () => { test('logout calls exec', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
const execSpy = jest.spyOn(Exec, 'getExecOutput').mockImplementation(async () => { const execSpy = jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
return { return {
exitCode: expect.any(Number), exitCode: expect.any(Number),
stdout: expect.any(Function), stdout: expect.any(Function),
@ -45,7 +52,13 @@ test('logout calls exec', async () => {
await logout(registry); await logout(registry);
expect(execSpy).toHaveBeenCalledWith(`docker`, ['logout', registry], { expect(execSpy).toHaveBeenCalledTimes(1);
const callfunc = execSpy.mock.calls[0];
if (callfunc && callfunc[1]) {
// we don't want to check env opt
callfunc[1].env = undefined;
}
expect(execSpy).toHaveBeenCalledWith(['logout', registry], {
ignoreReturnCode: true ignoreReturnCode: true
}); });
}); });

View File

@ -1,6 +1,7 @@
import * as aws from './aws'; import * as aws from './aws';
import * as core from '@actions/core'; import * as core from '@actions/core';
import {Exec} from '@docker/actions-toolkit/lib/exec';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
export async function login(registry: string, username: string, password: string, ecr: string): Promise<void> { export async function login(registry: string, username: string, password: string, ecr: string): Promise<void> {
if (/true/i.test(ecr) || (ecr == 'auto' && aws.isECR(registry))) { if (/true/i.test(ecr) || (ecr == 'auto' && aws.isECR(registry))) {
@ -11,7 +12,7 @@ export async function login(registry: string, username: string, password: string
} }
export async function logout(registry: string): Promise<void> { export async function logout(registry: string): Promise<void> {
await Exec.getExecOutput('docker', ['logout', registry], { await Docker.getExecOutput(['logout', registry], {
ignoreReturnCode: true ignoreReturnCode: true
}).then(res => { }).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) { if (res.stderr.length > 0 && res.exitCode != 0) {
@ -40,7 +41,7 @@ export async function loginStandard(registry: string, username: string, password
} else { } else {
core.info(`Logging into Docker Hub...`); core.info(`Logging into Docker Hub...`);
} }
await Exec.getExecOutput('docker', loginArgs, { await Docker.getExecOutput(loginArgs, {
ignoreReturnCode: true, ignoreReturnCode: true,
silent: true, silent: true,
input: Buffer.from(password) input: Buffer.from(password)
@ -57,7 +58,7 @@ export async function loginECR(registry: string, username: string, password: str
const regDatas = await aws.getRegistriesData(registry, username, password); const regDatas = await aws.getRegistriesData(registry, username, password);
for (const regData of regDatas) { for (const regData of regDatas) {
core.info(`Logging into ${regData.registry}...`); core.info(`Logging into ${regData.registry}...`);
await Exec.getExecOutput('docker', ['login', '--password-stdin', '--username', regData.username, regData.registry], { await Docker.getExecOutput(['login', '--password-stdin', '--username', regData.username, regData.registry], {
ignoreReturnCode: true, ignoreReturnCode: true,
silent: true, silent: true,
input: Buffer.from(regData.password) input: Buffer.from(regData.password)