This commit is contained in:
2025-12-14 01:42:14 +00:00
parent 9ad5222b95
commit 5d18e53e30
10 changed files with 310 additions and 9863 deletions

View File

@@ -22,7 +22,7 @@ export class Dockerfile {
public static async readDockerfiles(
szciDockerManagerRefArg: SzciDockerManager
): Promise<Dockerfile[]> {
const fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, 'Dockerfile*');
const fileTree = await plugins.smartfile.fs.listFileTree(paths.getCwd(), 'Dockerfile*');
// create the Dockerfile array
const readDockerfilesArray: Dockerfile[] = [];

View File

@@ -1,10 +1,20 @@
import * as plugins from './mod.plugins.ts';
import * as paths from '../szci.paths.ts';
import { logger } from '../szci.logging.ts';
/**
* cleans szci config files
* Cleans szci config files from the project directory
*/
export let clean = async (): Promise<void> => {
plugins.smartfile.fs.removeSync(paths.SzciPackageConfig);
return;
export const clean = async (): Promise<void> => {
try {
if (plugins.smartfile.fs.fileExistsSync(paths.SzciPackageConfig)) {
plugins.smartfile.fs.removeSync(paths.SzciPackageConfig);
logger.log('ok', 'Cleaned szci config files');
} else {
logger.log('info', 'No szci config files to clean');
}
} catch (error) {
logger.log('error', `Failed to clean config files: ${(error as Error).message}`);
throw error;
}
};

View File

@@ -1,15 +1,30 @@
import * as plugins from './mod.plugins.ts';
import { bash } from '../szci.bash.ts';
import { logger } from '../szci.logging.ts';
export let command = async () => {
let wrappedCommand: string = '';
let argvArray = ['deno', 'mod.ts', ...Deno.args];
for (let i = 3; i < argvArray.length; i++) {
wrappedCommand = wrappedCommand + argvArray[i];
if (i + 1 !== argvArray.length) {
wrappedCommand = wrappedCommand + ' ';
}
/**
* Executes a wrapped command passed via CLI arguments.
* Usage: szci command <your-command-here>
*
* This allows running arbitrary commands through szci's bash wrapper
* which handles nvm and other environment setup.
*/
export const command = async (): Promise<void> => {
// Skip 'deno', 'mod.ts', 'command' and get the rest
const commandArgs = Deno.args.slice(1); // Skip 'command'
if (commandArgs.length === 0) {
logger.log('error', 'No command specified. Usage: szci command <your-command>');
Deno.exit(1);
}
const wrappedCommand = commandArgs.join(' ');
logger.log('info', `Executing command: ${wrappedCommand}`);
try {
await bash(wrappedCommand);
logger.log('ok', 'Command executed successfully');
} catch (error) {
logger.log('error', `Command failed: ${(error as Error).message}`);
throw error;
}
await bash(wrappedCommand);
return;
};

View File

@@ -1,10 +1,22 @@
import { logger } from '../szci.logging.ts';
import * as plugins from './mod.plugins.ts';
let sshInstance: plugins.smartssh.SshInstance;
export let handleCli = async (argvArg: any) => {
/**
* Interface for CLI arguments
*/
interface ICliArgs {
_: string[];
[key: string]: unknown;
}
/**
* Handle SSH CLI commands
*/
export const handleCli = async (argvArg: ICliArgs): Promise<void> => {
if (argvArg._.length >= 2) {
const action: string = argvArg._[1];
const action = argvArg._[1];
switch (action) {
case 'prepare':
await prepare();
@@ -20,45 +32,66 @@ export let handleCli = async (argvArg: any) => {
};
/**
* checks if not undefined
* Checks if a string value is defined and not a placeholder
*/
const notUndefined = (stringArg: string) => {
return stringArg && stringArg !== 'undefined' && stringArg !== '##';
const isValidValue = (value: string | undefined): boolean => {
return Boolean(value && value !== 'undefined' && value !== '##');
};
/**
* checks for ENV vars in form of SZCI_SSHKEY_* and deploys any found ones
* Checks for ENV vars in form of SZCI_SSHKEY_* and deploys any found ones
*/
export let prepare = async () => {
sshInstance = new plugins.smartssh.SshInstance(); // init ssh instance
plugins.smartobject.forEachMinimatch(Deno.env.toObject(), 'SZCI_SSHKEY_*', evaluateSshEnv);
if (!Deno.env.get("SZCI_TEST")) {
sshInstance.writeToDisk();
export const prepare = async (): Promise<void> => {
sshInstance = new plugins.smartssh.SshInstance();
// Get all env vars and filter for SSH keys
const envVars = Deno.env.toObject();
const sshKeyEnvVars = Object.entries(envVars).filter(([key]) =>
key.startsWith('SZCI_SSHKEY_')
);
// Process each SSH key env var
for (const [key, value] of sshKeyEnvVars) {
logger.log('info', `Processing SSH key from ${key}`);
addSshKeyFromEnvVar(value);
}
// Only write to disk if not in test mode
if (!Deno.env.get('SZCI_TEST')) {
try {
sshInstance.writeToDisk();
logger.log('ok', 'SSH keys written to disk');
} catch (error) {
logger.log('error', `Failed to write SSH keys: ${(error as Error).message}`);
throw error;
}
} else {
logger.log('info', 'In test mode, so not storing SSH keys to disk!');
}
};
/**
* gets called for each found SSH ENV Var and deploys it
* Parses an SSH key env var and adds it to the SSH instance
* Format: host|privKeyBase64|pubKeyBase64
*/
const evaluateSshEnv = async (sshkeyEnvVarArg: string) => {
const sshEnvArray = sshkeyEnvVarArg.split('|');
const addSshKeyFromEnvVar = (sshkeyEnvVarArg: string): void => {
const [host, privKeyBase64, pubKeyBase64] = sshkeyEnvVarArg.split('|');
const sshKey = new plugins.smartssh.SshKey();
logger.log('info', 'Found SSH identity for ' + sshEnvArray[1]);
if (notUndefined(sshEnvArray[0])) {
logger.log('info', `Found SSH identity for ${host || 'unknown host'}`);
if (isValidValue(host)) {
logger.log('info', '---> host defined!');
sshKey.host = sshEnvArray[0];
sshKey.host = host;
}
if (notUndefined(sshEnvArray[1])) {
if (isValidValue(privKeyBase64)) {
logger.log('info', '---> privKey defined!');
sshKey.privKeyBase64 = sshEnvArray[1];
sshKey.privKeyBase64 = privKeyBase64;
}
if (notUndefined(sshEnvArray[2])) {
if (isValidValue(pubKeyBase64)) {
logger.log('info', '---> pubKey defined!');
sshKey.pubKeyBase64 = sshEnvArray[2];
sshKey.pubKeyBase64 = pubKeyBase64;
}
sshInstance.addKey(sshKey);
return;
};

View File

@@ -1,47 +1,101 @@
import * as plugins from './mod.plugins.ts';
import { bash } from '../szci.bash.ts';
import { logger } from '../szci.logging.ts';
const triggerValueRegex =
/^([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|?([a-zA-Z0-9\.\-\/]*)/;
/**
* Interface for parsed trigger configuration
*/
interface ITriggerConfig {
domain: string;
projectId: string;
triggerToken: string;
refName: string;
triggerName: string;
}
export let trigger = async () => {
/**
* Regex to parse trigger env var format:
* domain|projectId|triggerToken|refName|triggerName (optional)
*/
const TRIGGER_VALUE_REGEX =
/^([a-zA-Z0-9.]+)\|([a-zA-Z0-9.]+)\|([a-zA-Z0-9.]+)\|([a-zA-Z0-9.]+)\|?([a-zA-Z0-9.\-/]*)$/;
/**
* Execute all configured triggers from environment variables
*/
export const trigger = async (): Promise<void> => {
logger.log('info', 'now running triggers');
await plugins.smartobject.forEachMinimatch(Deno.env.toObject(), 'SZCI_TRIGGER_*', evaluateTrigger);
};
const evaluateTrigger = async (triggerEnvVarArg: string) => {
const triggerRegexResultArray = triggerValueRegex.exec(triggerEnvVarArg);
if (!triggerRegexResultArray) {
logger.log('error', 'malformed trigger env var...');
// Get all env vars and filter for triggers
const envVars = Deno.env.toObject();
const triggerEnvVars = Object.entries(envVars).filter(([key]) =>
key.startsWith('SZCI_TRIGGER_')
);
if (triggerEnvVars.length === 0) {
logger.log('info', 'no triggers configured');
return;
}
const regexDomain = triggerRegexResultArray[1];
const regexProjectId = triggerRegexResultArray[2];
const regexProjectTriggerToken = triggerRegexResultArray[3];
const regexRefName = triggerRegexResultArray[4];
let regexTriggerName: string;
if (triggerRegexResultArray.length === 6) {
regexTriggerName = triggerRegexResultArray[5];
} else {
regexTriggerName = 'Unnamed Trigger';
// Process each trigger
for (const [key, value] of triggerEnvVars) {
logger.log('info', `Processing trigger from ${key}`);
await executeTrigger(value);
}
logger.log('ok', `executed ${triggerEnvVars.length} trigger(s)`);
};
/**
* Parse a trigger env var string into a config object
*/
const parseTriggerConfig = (triggerEnvVar: string): ITriggerConfig | null => {
const match = TRIGGER_VALUE_REGEX.exec(triggerEnvVar);
if (!match) {
return null;
}
return {
domain: match[1],
projectId: match[2],
triggerToken: match[3],
refName: match[4],
triggerName: match[5] || 'Unnamed Trigger',
};
};
/**
* Execute a single trigger by calling the GitLab API
*/
const executeTrigger = async (triggerEnvVar: string): Promise<void> => {
const config = parseTriggerConfig(triggerEnvVar);
if (!config) {
logger.log('error', 'malformed trigger env var, expected format: domain|projectId|token|ref|name');
return;
}
logger.log('info', `Found Trigger: ${config.triggerName}`);
logger.log('info', `Triggering build for ref "${config.refName}" of "${config.triggerName}"`);
try {
await plugins.smartrequest.postFormData(
`https://${config.domain}/api/v3/projects/${config.projectId}/trigger/builds`,
{},
[
{
name: 'token',
payload: config.triggerToken,
type: 'string',
},
{
name: 'ref',
payload: config.refName,
type: 'string',
},
]
);
logger.log('ok', `Trigger "${config.triggerName}" executed successfully`);
} catch (error) {
logger.log('error', `Failed to execute trigger: ${(error as Error).message}`);
}
logger.log('info', 'Found Trigger!');
logger.log('info', 'triggering build for ref ' + regexRefName + ' of ' + regexTriggerName);
plugins.smartrequest.postFormData(
'https://gitlab.com/api/v3/projects/' + regexProjectId + '/trigger/builds',
{},
[
{
name: 'token',
payload: regexProjectTriggerToken,
type: 'string',
},
{
name: 'ref',
payload: regexRefName,
type: 'string',
},
]
);
};

View File

@@ -1,5 +1,14 @@
import * as plugins from './szci.plugins.ts';
/**
* Get current working directory (evaluated at call time, not module load time)
*/
export const getCwd = (): string => Deno.cwd();
/**
* Current working directory - use getCwd() if you need the live value after chdir
* @deprecated Use getCwd() for dynamic cwd resolution
*/
export const cwd = Deno.cwd();
// package paths
@@ -9,7 +18,13 @@ export const SzciPackageRoot = plugins.path.join(
);
export const SzciPackageConfig = plugins.path.join(SzciPackageRoot, './config.json');
// project paths
// project paths - use functions for dynamic resolution
export const getSzciProjectDir = (): string => getCwd();
export const getSzciProjectNogitDir = (): string => plugins.path.join(getCwd(), './.nogit');
export const getSzciTestDir = (): string => plugins.path.join(getCwd(), './test');
export const getSzciCacheDir = (): string => plugins.path.join(getCwd(), './.szci_cache');
// Static paths (for backwards compatibility - captured at module load)
export const SzciProjectDir = cwd;
export const SzciProjectNogitDir = plugins.path.join(SzciProjectDir, './.nogit');
export const SzciTestDir = plugins.path.join(cwd, './test');