mirror of
https://github.com/docker/login-action.git
synced 2026-03-10 17:34:30 +00:00
Merge 0d13a90284 into 3227f5311c
This commit is contained in:
commit
183f0f2576
@ -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';
|
||||
@ -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
|
||||
@ -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);
|
||||
|
||||
@ -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
|
||||
@ -48,7 +110,7 @@ test('logout calls exec', async () => {
|
||||
};
|
||||
});
|
||||
|
||||
const registry = 'https://ghcr.io';
|
||||
const registry = 'ghcr.io';
|
||||
|
||||
await logout(registry, '');
|
||||
|
||||
|
||||
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
@ -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))) {
|
||||
@ -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');
|
||||
}
|
||||
@ -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…
Reference in New Issue
Block a user