execInContainer function

Future<void> execInContainer({
  1. required String name,
  2. required String command,
  3. required String buildJobId,
  4. required String runId,
  5. required String token,
})

Implementation

Future<void> execInContainer({
  required String name,
  required String command,
  required String buildJobId,
  required String runId,
  required String token,
}) async {
  final result = await Process.run('docker', [
    'exec',
    name,
    'bash',
    '-c',
    command,
  ]);

  final stdout = result.stdout?.toString().trim();
  final stderr = result.stderr?.toString().trim();

  if (stdout != null && stdout.isNotEmpty) {
    final masked = stdout.replaceAll(token, '***');
    await logInfo(buildJobId, runId, masked);
  }
  if (stderr != null && stderr.isNotEmpty) {
    final masked = stderr.replaceAll(token, '***');
    await logInfo(buildJobId, runId, masked);
  }

  if (result.exitCode != 0) {
    throw Exception('Command failed with exit code ${result.exitCode}');
  }
}