Files
szci/ts/manager.docker/index.ts

79 lines
2.2 KiB
TypeScript
Raw Normal View History

import { logger } from '../szci.logging.ts';
import { bash } from '../szci.bash.ts';
import { Szci } from '../szci.classes.szci.ts';
2019-08-29 20:26:23 +02:00
export class SzciDockerManager {
public szciRef: Szci;
2019-08-29 20:26:23 +02:00
constructor(szciArg: Szci) {
this.szciRef = szciArg;
2019-08-29 20:26:23 +02:00
}
/**
* Bridges SZCI_LOGIN_DOCKER* env vars to DOCKER_REGISTRY_N format for tsdocker,
* and handles GitLab CI registry auto-login.
2019-08-29 20:26:23 +02:00
*/
private bridgeEnvVars() {
const env = Deno.env.toObject();
2019-08-29 20:26:23 +02:00
// Bridge GitLab CI registry as DOCKER_REGISTRY_0
if (env['GITLAB_CI']) {
const ciJobToken = env['CI_JOB_TOKEN'];
if (!ciJobToken) {
logger.log('error', 'Running in GitLab CI, but no CI_JOB_TOKEN found!');
Deno.exit(1);
2023-06-25 23:59:25 +02:00
}
Deno.env.set('DOCKER_REGISTRY_0', `registry.gitlab.com|gitlab-ci-token|${ciJobToken}`);
2019-08-29 20:26:23 +02:00
}
// Bridge SZCI_LOGIN_DOCKER* → DOCKER_REGISTRY_N
let registryIndex = 1;
const sortedKeys = Object.keys(env)
.filter((key) => key.startsWith('SZCI_LOGIN_DOCKER'))
.sort();
for (const key of sortedKeys) {
Deno.env.set(`DOCKER_REGISTRY_${registryIndex}`, env[key]);
registryIndex++;
}
}
2019-08-29 20:26:23 +02:00
/**
* Handle cli input by bridging env vars and delegating to tsdocker.
2019-08-29 20:26:23 +02:00
*/
public handleCli = async (argvArg: any) => {
if (argvArg._.length < 2) {
logger.log(
'info',
`>>szci docker ...<< cli arguments invalid... Please read the documentation.`
2019-08-29 20:26:23 +02:00
);
return;
2019-08-29 20:26:23 +02:00
}
this.bridgeEnvVars();
2019-08-29 20:26:23 +02:00
const action: string = argvArg._[1];
const extraArgs = argvArg._.slice(2).join(' ');
2019-08-29 20:26:23 +02:00
switch (action) {
case 'build':
await bash(`npx @git.zone/tsdocker build ${extraArgs}`.trim());
break;
case 'login':
case 'prepare':
await bash(`npx @git.zone/tsdocker login ${extraArgs}`.trim());
break;
case 'test':
await bash(`npx @git.zone/tsdocker test ${extraArgs}`.trim());
break;
case 'push':
await bash(`npx @git.zone/tsdocker push ${extraArgs}`.trim());
break;
case 'pull':
await bash(`npx @git.zone/tsdocker pull ${extraArgs}`.trim());
break;
default:
logger.log('error', `>>szci docker ...<< action >>${action}<< not supported`);
2019-08-29 20:26:23 +02:00
}
2019-09-01 13:51:11 +02:00
};
2019-08-29 20:26:23 +02:00
}