mirror of
https://github.com/docker/login-action.git
synced 2025-08-18 01:59:53 +08:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
39ef12fb7a | ||
|
1c402b7c97 | ||
|
1c2cf9942d | ||
|
4b15841c41 |
24
__tests__/context.test.ts
Normal file
24
__tests__/context.test.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import osm = require('os');
|
||||||
|
|
||||||
|
import {getInputs} from '../src/context';
|
||||||
|
|
||||||
|
test('without username getInputs throws errors', async () => {
|
||||||
|
expect(() => {
|
||||||
|
getInputs();
|
||||||
|
}).toThrowError('Input required and not supplied: username');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('without password getInputs throws errors', async () => {
|
||||||
|
process.env['INPUT_USERNAME'] = 'dbowie';
|
||||||
|
expect(() => {
|
||||||
|
getInputs();
|
||||||
|
}).toThrowError('Input required and not supplied: password');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('with password and username getInputs does not error', async () => {
|
||||||
|
process.env['INPUT_USERNAME'] = 'dbowie';
|
||||||
|
process.env['INPUT_PASSWORD'] = 'groundcontrol';
|
||||||
|
expect(() => {
|
||||||
|
getInputs();
|
||||||
|
}).not.toThrowError();
|
||||||
|
});
|
49
__tests__/docker.test.ts
Normal file
49
__tests__/docker.test.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import {loginStandard, logout} from '../src/docker';
|
||||||
|
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
import * as exec from '@actions/exec';
|
||||||
|
|
||||||
|
process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner');
|
||||||
|
|
||||||
|
test('loginStandard calls exec', async () => {
|
||||||
|
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'exec');
|
||||||
|
// don't let exec try to actually run the commands
|
||||||
|
execSpy.mockImplementation(() => {});
|
||||||
|
|
||||||
|
const username: string = 'dbowie';
|
||||||
|
const password: string = 'groundcontrol';
|
||||||
|
const registry: string = 'https://ghcr.io';
|
||||||
|
|
||||||
|
await loginStandard(registry, username, password);
|
||||||
|
|
||||||
|
expect(execSpy).toHaveBeenCalledWith(`docker`, ['login', '--password-stdin', '--username', username, registry], {
|
||||||
|
input: Buffer.from(password),
|
||||||
|
silent: true,
|
||||||
|
ignoreReturnCode: true,
|
||||||
|
listeners: expect.objectContaining({
|
||||||
|
stdout: expect.any(Function),
|
||||||
|
stderr: expect.any(Function)
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('logout calls exec', async () => {
|
||||||
|
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'exec');
|
||||||
|
// don't let exec try to actually run the commands
|
||||||
|
execSpy.mockImplementation(() => {});
|
||||||
|
|
||||||
|
const registry: string = 'https://ghcr.io';
|
||||||
|
|
||||||
|
await logout(registry);
|
||||||
|
|
||||||
|
expect(execSpy).toHaveBeenCalledWith(`docker`, ['logout', registry], {
|
||||||
|
silent: false,
|
||||||
|
ignoreReturnCode: true,
|
||||||
|
input: Buffer.from(''),
|
||||||
|
listeners: expect.objectContaining({
|
||||||
|
stdout: expect.any(Function),
|
||||||
|
stderr: expect.any(Function)
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
93
__tests__/main.test.ts
Normal file
93
__tests__/main.test.ts
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
import osm = require('os');
|
||||||
|
|
||||||
|
import {run} from '../src/main';
|
||||||
|
import * as docker from '../src/docker';
|
||||||
|
import * as stateHelper from '../src/state-helper';
|
||||||
|
|
||||||
|
import * as core from '@actions/core';
|
||||||
|
|
||||||
|
test('errors when not run on linux platform', async () => {
|
||||||
|
const platSpy = jest.spyOn(osm, 'platform');
|
||||||
|
platSpy.mockImplementation(() => 'netbsd');
|
||||||
|
|
||||||
|
const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed');
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(coreSpy).toHaveBeenCalledWith('Only supported on linux platform');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('errors without username', async () => {
|
||||||
|
const platSpy = jest.spyOn(osm, 'platform');
|
||||||
|
platSpy.mockImplementation(() => 'linux');
|
||||||
|
|
||||||
|
const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed');
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: username');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('errors without password', async () => {
|
||||||
|
const platSpy = jest.spyOn(osm, 'platform');
|
||||||
|
platSpy.mockImplementation(() => 'linux');
|
||||||
|
|
||||||
|
const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed');
|
||||||
|
|
||||||
|
const username: string = 'dbowie';
|
||||||
|
process.env[`INPUT_USERNAME`] = username;
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: password');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('successful with username and password', async () => {
|
||||||
|
const platSpy = jest.spyOn(osm, 'platform');
|
||||||
|
platSpy.mockImplementation(() => 'linux');
|
||||||
|
|
||||||
|
const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry');
|
||||||
|
const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout');
|
||||||
|
const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login');
|
||||||
|
dockerSpy.mockImplementation(() => {});
|
||||||
|
|
||||||
|
const username: string = 'dbowie';
|
||||||
|
process.env[`INPUT_USERNAME`] = username;
|
||||||
|
|
||||||
|
const password: string = 'groundcontrol';
|
||||||
|
process.env[`INPUT_PASSWORD`] = password;
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(setRegistrySpy).toHaveBeenCalledWith('');
|
||||||
|
expect(setLogoutSpy).toHaveBeenCalledWith('');
|
||||||
|
expect(dockerSpy).toHaveBeenCalledWith('', username, password);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('calls docker login', async () => {
|
||||||
|
const platSpy = jest.spyOn(osm, 'platform');
|
||||||
|
platSpy.mockImplementation(() => 'linux');
|
||||||
|
|
||||||
|
const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry');
|
||||||
|
const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout');
|
||||||
|
const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login');
|
||||||
|
dockerSpy.mockImplementation(() => {});
|
||||||
|
|
||||||
|
const username: string = 'dbowie';
|
||||||
|
process.env[`INPUT_USERNAME`] = username;
|
||||||
|
|
||||||
|
const password: string = 'groundcontrol';
|
||||||
|
process.env[`INPUT_PASSWORD`] = password;
|
||||||
|
|
||||||
|
const registry: string = 'https://ghcr.io';
|
||||||
|
process.env[`INPUT_REGISTRY`] = registry;
|
||||||
|
|
||||||
|
const logout: string = 'true';
|
||||||
|
process.env['INPUT_LOGOUT'] = logout;
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(setRegistrySpy).toHaveBeenCalledWith(registry);
|
||||||
|
expect(setLogoutSpy).toHaveBeenCalledWith(logout);
|
||||||
|
expect(dockerSpy).toHaveBeenCalledWith(registry, username, password);
|
||||||
|
});
|
@@ -12,7 +12,7 @@ inputs:
|
|||||||
required: false
|
required: false
|
||||||
username:
|
username:
|
||||||
description: 'Username used to log against the Docker registry'
|
description: 'Username used to log against the Docker registry'
|
||||||
required: false
|
required: true
|
||||||
password:
|
password:
|
||||||
description: 'Password or personal access token used to log against the Docker registry'
|
description: 'Password or personal access token used to log against the Docker registry'
|
||||||
required: true
|
required: true
|
||||||
|
36
dist/index.js
generated
vendored
36
dist/index.js
generated
vendored
@@ -496,6 +496,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.run = void 0;
|
||||||
const os = __importStar(__webpack_require__(87));
|
const os = __importStar(__webpack_require__(87));
|
||||||
const core = __importStar(__webpack_require__(186));
|
const core = __importStar(__webpack_require__(186));
|
||||||
const context_1 = __webpack_require__(842);
|
const context_1 = __webpack_require__(842);
|
||||||
@@ -505,19 +506,19 @@ function run() {
|
|||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
try {
|
try {
|
||||||
if (os.platform() !== 'linux') {
|
if (os.platform() !== 'linux') {
|
||||||
core.setFailed('Only supported on linux platform');
|
throw new Error('Only supported on linux platform');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
let inputs = yield context_1.getInputs();
|
const { registry, username, password, logout } = context_1.getInputs();
|
||||||
stateHelper.setRegistry(inputs.registry);
|
stateHelper.setRegistry(registry);
|
||||||
stateHelper.setLogout(inputs.logout);
|
stateHelper.setLogout(logout);
|
||||||
yield docker.login(inputs.registry, inputs.username, inputs.password);
|
yield docker.login(registry, username, password);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
core.setFailed(error.message);
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
exports.run = run;
|
||||||
function logout() {
|
function logout() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
if (!stateHelper.logout) {
|
if (!stateHelper.logout) {
|
||||||
@@ -3640,27 +3641,16 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||||||
__setModuleDefault(result, mod);
|
__setModuleDefault(result, mod);
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
||||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
||||||
return new (P || (P = Promise))(function (resolve, reject) {
|
|
||||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
||||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
||||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
||||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
||||||
});
|
|
||||||
};
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.getInputs = void 0;
|
exports.getInputs = void 0;
|
||||||
const core = __importStar(__webpack_require__(186));
|
const core = __importStar(__webpack_require__(186));
|
||||||
function getInputs() {
|
function getInputs() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return {
|
||||||
return {
|
registry: core.getInput('registry'),
|
||||||
registry: core.getInput('registry'),
|
username: core.getInput('username', { required: true }),
|
||||||
username: core.getInput('username'),
|
password: core.getInput('password', { required: true }),
|
||||||
password: core.getInput('password', { required: true }),
|
logout: core.getInput('logout')
|
||||||
logout: core.getInput('logout')
|
};
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
exports.getInputs = getInputs;
|
exports.getInputs = getInputs;
|
||||||
//# sourceMappingURL=context.js.map
|
//# sourceMappingURL=context.js.map
|
||||||
|
@@ -7,10 +7,10 @@ export interface Inputs {
|
|||||||
logout: string;
|
logout: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getInputs(): Promise<Inputs> {
|
export function getInputs(): Inputs {
|
||||||
return {
|
return {
|
||||||
registry: core.getInput('registry'),
|
registry: core.getInput('registry'),
|
||||||
username: core.getInput('username'),
|
username: core.getInput('username', {required: true}),
|
||||||
password: core.getInput('password', {required: true}),
|
password: core.getInput('password', {required: true}),
|
||||||
logout: core.getInput('logout')
|
logout: core.getInput('logout')
|
||||||
};
|
};
|
||||||
|
13
src/main.ts
13
src/main.ts
@@ -4,17 +4,16 @@ import {getInputs, Inputs} from './context';
|
|||||||
import * as docker from './docker';
|
import * as docker from './docker';
|
||||||
import * as stateHelper from './state-helper';
|
import * as stateHelper from './state-helper';
|
||||||
|
|
||||||
async function run(): Promise<void> {
|
export async function run(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
if (os.platform() !== 'linux') {
|
if (os.platform() !== 'linux') {
|
||||||
core.setFailed('Only supported on linux platform');
|
throw new Error('Only supported on linux platform');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let inputs: Inputs = await getInputs();
|
const {registry, username, password, logout} = getInputs();
|
||||||
stateHelper.setRegistry(inputs.registry);
|
stateHelper.setRegistry(registry);
|
||||||
stateHelper.setLogout(inputs.logout);
|
stateHelper.setLogout(logout);
|
||||||
await docker.login(inputs.registry, inputs.username, inputs.password);
|
await docker.login(registry, username, password);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error.message);
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user