Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions __tests__/docker.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect, jest, test} from '@jest/globals';
import {afterEach, beforeEach, expect, jest, test} from '@jest/globals';
import * as path from 'path';

import {loginStandard, logout} from '../src/docker';
Expand All @@ -7,6 +7,14 @@ import {Docker} from '@docker/actions-toolkit/lib/docker/docker';

process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner');

beforeEach(() => {
delete process.env.DOCKER_LOGIN_SKIP_IF_MISSING_CREDS;
});

afterEach(() => {
delete process.env.DOCKER_LOGIN_SKIP_IF_MISSING_CREDS;
});

test('loginStandard calls exec', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -20,7 +28,7 @@ test('loginStandard calls exec', async () => {

const username = 'dbowie';
const password = 'groundcontrol';
const registry = 'https://ghcr.io';
const registry = 'ghcr.io';

await loginStandard(registry, username, password);

Expand All @@ -37,6 +45,60 @@ test('loginStandard calls exec', async () => {
});
});

test('loginStandard throws if username and password are missing', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const execSpy = jest.spyOn(Docker, 'getExecOutput');
await expect(loginStandard('ghcr.io', '', '')).rejects.toThrow('Username and password required');
expect(execSpy).not.toHaveBeenCalled();
});

test('loginStandard throws if username is missing', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const execSpy = jest.spyOn(Docker, 'getExecOutput');
await expect(loginStandard('ghcr.io', '', 'groundcontrol')).rejects.toThrow('Username required');
expect(execSpy).not.toHaveBeenCalled();
});

test('loginStandard throws if password is missing', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const execSpy = jest.spyOn(Docker, 'getExecOutput');
await expect(loginStandard('ghcr.io', 'dbowie', '')).rejects.toThrow('Password required');
expect(execSpy).not.toHaveBeenCalled();
});

test('loginStandard skips if both credentials are missing and env opt-in is enabled', async () => {
process.env.DOCKER_LOGIN_SKIP_IF_MISSING_CREDS = 'true';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const execSpy = jest.spyOn(Docker, 'getExecOutput');

await expect(loginStandard('ghcr.io', '', '')).resolves.toBeUndefined();
expect(execSpy).not.toHaveBeenCalled();
});

test('loginStandard skips if username is missing and env opt-in is enabled', async () => {
process.env.DOCKER_LOGIN_SKIP_IF_MISSING_CREDS = 'true';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const execSpy = jest.spyOn(Docker, 'getExecOutput');

await expect(loginStandard('ghcr.io', '', 'groundcontrol')).resolves.toBeUndefined();
expect(execSpy).not.toHaveBeenCalled();
});

test('loginStandard skips if password is missing and env opt-in is enabled', async () => {
process.env.DOCKER_LOGIN_SKIP_IF_MISSING_CREDS = 'true';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const execSpy = jest.spyOn(Docker, 'getExecOutput');

await expect(loginStandard('ghcr.io', 'dbowie', '')).resolves.toBeUndefined();
expect(execSpy).not.toHaveBeenCalled();
});

test('logout calls exec', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -48,7 +110,7 @@ test('logout calls exec', async () => {
};
});

const registry = 'https://ghcr.io';
const registry = 'ghcr.io';

await logout(registry, '');

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as aws from './aws';
import * as context from './context';

import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
import {Util} from '@docker/actions-toolkit/lib/util';

export async function login(auth: context.Auth): Promise<void> {
if (/true/i.test(auth.ecr) || (auth.ecr == 'auto' && aws.isECR(auth.registry))) {
Expand Down Expand Up @@ -34,6 +35,10 @@ export async function logout(registry: string, configDir: string): Promise<void>
}

export async function loginStandard(registry: string, username: string, password: string, scope?: string): Promise<void> {
if ((!username || !password) && skipLoginIfMissingCredsEnabled()) {
core.info(`Skipping login to ${registry}. Username or password is not set and DOCKER_LOGIN_SKIP_IF_MISSING_CREDS is enabled.`);
return;
}
if (!username && !password) {
throw new Error('Username and password required');
}
Expand Down Expand Up @@ -79,3 +84,10 @@ async function loginExec(registry: string, username: string, password: string, s
core.info('Login Succeeded!');
});
}

function skipLoginIfMissingCredsEnabled(): boolean {
if (process.env.DOCKER_LOGIN_SKIP_IF_MISSING_CREDS) {
return Util.parseBool(process.env.DOCKER_LOGIN_SKIP_IF_MISSING_CREDS);
}
return false;
}
Loading