runCiCommands function

Future<(int, String)> runCiCommands(
  1. String directory,
  2. List<String> commands, {
  3. Map<String, String> environment = const {},
  4. String? artifact,
})

Runs commands one after another, each with bash -c, in a clone of a commit of the app in directory, as a job of CI runs them on a runner, and removes the clone. Returns 0 and the output, or the exit code of the first command that fails and the output up to it, whose last line names the command.

So the clone has what git has of the app, and not what the .gitignore of the app keeps out of it, such as .env, build/ or .dart_tool/. The repository of that commit is in a temporary directory too, so the app stays as it was. What the machine ignores, signs or checks in its own configuration of git has no part in it.

Each command gets the variables of environment in its process, next to those of this one. With artifact, a path in the clone, a run whose commands all succeed fails when they left no such file.

Implementation

Future<(int, String)> runCiCommands(
  String directory,
  List<String> commands, {
  Map<String, String> environment = const {},
  String? artifact,
}) async {
  final output = StringBuffer();
  final temp = await Directory.systemTemp.createTemp('smf_ci_');
  try {
    final clone = '${temp.path}/app';
    final cloned =
        await _cloneTo(clone, '${temp.path}/repository', directory, output);
    if (cloned != 0) return (cloned, output.toString());
    for (final command in commands) {
      output.writeln('\$ $command');
      final code = await _run(
        'bash',
        ['-c', command],
        clone,
        output,
        environment: environment,
      );
      if (code != 0) {
        output.writeln('This command of CI exited with $code: $command');
        return (code, output.toString());
      }
    }
    if (artifact != null && !File('$clone/$artifact').existsSync()) {
      output.writeln('The commands of CI left no file $artifact.');
      return (1, output.toString());
    }
    return (0, output.toString());
  } finally {
    await temp.delete(recursive: true);
  }
}