1
0
mirror of git://f0xx.org/android_cast synced 2026-07-29 07:20:00 +03:00

sync BE/builders

This commit is contained in:
Anton Afanasyeu
2026-06-05 16:46:31 +02:00
parent b47e05490f
commit 7dc992fb19
6 changed files with 168 additions and 24 deletions

View File

@@ -5,6 +5,8 @@ Auth::check();
$id = (int) ($_GET['id'] ?? 0); $id = (int) ($_GET['id'] ?? 0);
$offset = (int) ($_GET['offset'] ?? 0); $offset = (int) ($_GET['offset'] ?? 0);
$full = isset($_GET['full']) && $_GET['full'] === '1';
$download = isset($_GET['download']) && $_GET['download'] === '1';
if ($id <= 0) { if ($id <= 0) {
json_out(['ok' => false, 'error' => 'id required'], 400); json_out(['ok' => false, 'error' => 'id required'], 400);
} }
@@ -12,7 +14,20 @@ $build = BuildRepository::getById($id);
if (!$build) { if (!$build) {
json_out(['ok' => false, 'error' => 'not found'], 404); json_out(['ok' => false, 'error' => 'not found'], 404);
} }
$tail = BuildRunner::readLogTail($build['log_path'] ?? null, $offset); $logPath = $build['log_path'] ?? null;
if ($download) {
if (!$logPath || !is_file($logPath)) {
json_out(['ok' => false, 'error' => 'log not found'], 404);
}
$code = preg_replace('/[^a-zA-Z0-9._-]+/', '_', (string) ($build['build_code'] ?? 'build'));
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $code . '.log"');
readfile($logPath);
exit;
}
$tail = $full
? BuildRunner::readLogFull($logPath)
: BuildRunner::readLogTail($logPath, $offset);
if ($build['status'] === 'running' && $tail['eof'] && is_file(BuildRunner::artifactDir($id) . '/BUILD_INFO.json')) { if ($build['status'] === 'running' && $tail['eof'] && is_file(BuildRunner::artifactDir($id) . '/BUILD_INFO.json')) {
BuildRunner::finalizeFromArtifacts($id); BuildRunner::finalizeFromArtifacts($id);
$build = BuildRepository::getById($id); $build = BuildRepository::getById($id);

View File

@@ -142,31 +142,67 @@
} }
var buildId = document.body.getAttribute('data-build-id'); var buildId = document.body.getAttribute('data-build-id');
var buildStatus = document.body.getAttribute('data-build-status') || '';
var logEl = document.getElementById('build-log-tail'); var logEl = document.getElementById('build-log-tail');
if (buildId && logEl) { if (buildId && logEl) {
var offset = 0; var offset = 0;
var full = ''; var full = '';
var showFull = false;
var DISPLAY_TAIL = 50000;
function renderLog() {
logEl.textContent = showFull ? full : full.slice(-DISPLAY_TAIL);
logEl.scrollTop = logEl.scrollHeight;
}
function poll() { function poll() {
fetch(base + '/api/build_log.php?id=' + encodeURIComponent(buildId) + '&offset=' + offset) var url = base + '/api/build_log.php?id=' + encodeURIComponent(buildId) + '&offset=' + offset;
fetch(url)
.then(function (r) { return r.json(); }) .then(function (r) { return r.json(); })
.then(function (j) { .then(function (j) {
if (!j.ok) return; if (!j.ok) return;
if (j.log && j.log.text) { if (j.log && j.log.text) {
full += j.log.text; full += j.log.text;
offset = j.log.offset || offset; offset = j.log.offset || offset;
logEl.textContent = full.slice(-12000); renderLog();
logEl.scrollTop = logEl.scrollHeight;
} }
if (j.build && j.build.status === 'running') { var running = j.build && j.build.status === 'running';
setTimeout(poll, 1000); var eof = j.log && j.log.eof;
if (running || !eof) {
setTimeout(poll, running ? 1000 : 250);
} }
}).catch(function () { setTimeout(poll, 2000); }); }).catch(function () { setTimeout(poll, 2000); });
} }
function loadFullLog() {
return fetch(base + '/api/build_log.php?id=' + encodeURIComponent(buildId) + '&full=1')
.then(function (r) { return r.json(); })
.then(function (j) {
if (j.ok && j.log && j.log.text) {
full = j.log.text;
offset = j.log.offset || 0;
renderLog();
}
});
}
if (buildStatus && buildStatus !== 'running' && buildStatus !== 'queued') {
loadFullLog().catch(function () { poll(); });
} else {
poll(); poll();
}
var expand = document.getElementById('build-log-expand'); var expand = document.getElementById('build-log-expand');
if (expand) { if (expand) {
expand.addEventListener('click', function () { expand.addEventListener('click', function () {
logEl.textContent = full; showFull = true;
if (full === '') {
loadFullLog().then(function () { renderLog(); });
} else {
renderLog();
}
expand.textContent = 'Showing full log';
expand.disabled = true;
}); });
} }
} }

View File

@@ -138,6 +138,11 @@ YAML;
$runner = (string) cfg('build.runner_script', '/workspace/scripts/docker-build-runner.sh'); $runner = (string) cfg('build.runner_script', '/workspace/scripts/docker-build-runner.sh');
$repo = (string) cfg('build.repo_root', '/workspace'); $repo = (string) cfg('build.repo_root', '/workspace');
$gitRemote = trim((string) shell_exec(
'git -c safe.directory=' . escapeshellarg($repo)
. ' -C ' . escapeshellarg($repo)
. ' remote get-url origin 2>/dev/null'
));
$env = [ $env = [
'BUILD_ID=' . $id, 'BUILD_ID=' . $id,
'BUILD_LOG_FILE=' . escapeshellarg($logPath), 'BUILD_LOG_FILE=' . escapeshellarg($logPath),
@@ -149,6 +154,7 @@ YAML;
'ANDROIDCAST_CI_VERSION=' . escapeshellarg((string) cfg('build.ci_version', '00.01.00.1000')), 'ANDROIDCAST_CI_VERSION=' . escapeshellarg((string) cfg('build.ci_version', '00.01.00.1000')),
'GIT_REF=' . escapeshellarg((string) ($params['git_ref'] ?? '')), 'GIT_REF=' . escapeshellarg((string) ($params['git_ref'] ?? '')),
'GIT_SHA=' . escapeshellarg((string) ($params['git_sha'] ?? '')), 'GIT_SHA=' . escapeshellarg((string) ($params['git_sha'] ?? '')),
'GIT_REMOTE=' . escapeshellarg($gitRemote),
'GRADLE_TASK=' . escapeshellarg((string) ($params['gradle_task'] ?? 'assembleDebug')), 'GRADLE_TASK=' . escapeshellarg((string) ($params['gradle_task'] ?? 'assembleDebug')),
'RUN_UNIT_TESTS=' . (!empty($params['run_tests']) ? '1' : '0'), 'RUN_UNIT_TESTS=' . (!empty($params['run_tests']) ? '1' : '0'),
'RUN_NATIVE=' . (!empty($params['run_native']) ? '1' : '0'), 'RUN_NATIVE=' . (!empty($params['run_native']) ? '1' : '0'),
@@ -260,28 +266,78 @@ YAML;
} elseif (str_contains($tail, '[runner] finished')) { } elseif (str_contains($tail, '[runner] finished')) {
self::finalizeFromArtifacts($id); self::finalizeFromArtifacts($id);
} elseif ($tail !== '') { } elseif ($tail !== '') {
self::failBuild($id, 'Build runner exited unexpectedly', $logPath); $hint = self::summarizeLogFailure($logPath);
$msg = $hint !== '' ? $hint : 'Build runner exited unexpectedly';
self::failBuild($id, $msg, $logPath);
} else { } else {
self::failBuild($id, 'Build runner never started', $logPath); self::failBuild($id, 'Build runner never started', $logPath);
} }
} }
} }
/** Last lines / markers from build.log for reconcile error_message (max 512 chars). */
public static function summarizeLogFailure(?string $logPath): string {
if (!$logPath || !is_file($logPath)) {
return '';
}
$size = filesize($logPath) ?: 0;
$chunk = (string) file_get_contents($logPath, false, null, max(0, $size - 32768));
if ($chunk === '') {
return '';
}
$lines = preg_split('/\r\n|\r|\n/', $chunk) ?: [];
$markers = [];
foreach ($lines as $line) {
$trim = trim($line);
if ($trim === '') {
continue;
}
if (preg_match('/\[runner\] ERROR:|\[builder\] ERROR:|fatal:|BUILD FAILED|Cannot connect to the Docker daemon/i', $trim)) {
$markers[] = $trim;
}
}
$tailLines = array_values(array_filter($lines, static fn(string $l): bool => trim($l) !== ''));
$last = array_slice($tailLines, -4);
$parts = $markers !== [] ? array_slice($markers, -2) : $last;
$summary = implode(' | ', $parts);
if ($summary === '') {
return '';
}
if (strlen($summary) > 480) {
$summary = '…' . substr($summary, -477);
}
return 'Build runner exited unexpectedly: ' . $summary;
}
public static function readLogTail(?string $logPath, int $offset = 0, int $maxBytes = 65536): array { public static function readLogTail(?string $logPath, int $offset = 0, int $maxBytes = 65536): array {
if (!$logPath || !is_file($logPath)) { if (!$logPath || !is_file($logPath)) {
return ['offset' => 0, 'eof' => true, 'text' => '']; return ['offset' => 0, 'eof' => true, 'text' => '', 'size' => 0];
} }
$size = filesize($logPath) ?: 0; $size = filesize($logPath) ?: 0;
$offset = max(0, min($offset, $size)); $offset = max(0, min($offset, $size));
$fh = fopen($logPath, 'rb'); $fh = fopen($logPath, 'rb');
if (!$fh) { if (!$fh) {
return ['offset' => $offset, 'eof' => true, 'text' => '']; return ['offset' => $offset, 'eof' => true, 'text' => '', 'size' => $size];
} }
fseek($fh, $offset); fseek($fh, $offset);
$chunk = fread($fh, $maxBytes) ?: ''; $chunk = fread($fh, $maxBytes) ?: '';
$newOffset = $offset + strlen($chunk); $newOffset = $offset + strlen($chunk);
fclose($fh); fclose($fh);
return ['offset' => $newOffset, 'eof' => $newOffset >= $size, 'text' => $chunk]; return ['offset' => $newOffset, 'eof' => $newOffset >= $size, 'text' => $chunk, 'size' => $size];
}
public static function readLogFull(?string $logPath, int $maxBytes = 2097152): array {
if (!$logPath || !is_file($logPath)) {
return ['offset' => 0, 'eof' => true, 'text' => '', 'size' => 0, 'truncated' => false];
}
$size = filesize($logPath) ?: 0;
$truncated = $size > $maxBytes;
$start = $truncated ? $size - $maxBytes : 0;
$text = (string) file_get_contents($logPath, false, null, $start);
if ($truncated) {
$text = "[log truncated: showing last {$maxBytes} of {$size} bytes]\n" . $text;
}
return ['offset' => $size, 'eof' => true, 'text' => $text, 'size' => $size, 'truncated' => $truncated];
} }
public static function finalizeFromArtifacts(int $buildId): void { public static function finalizeFromArtifacts(int $buildId): void {

View File

@@ -84,6 +84,10 @@ if ($status === 'failed' && $dockerFail && $healthDocker):
<section class="card card--lift"> <section class="card card--lift">
<h2>Build log</h2> <h2>Build log</h2>
<p class="muted graphs-footnote">Live tail shows the last ~50&nbsp;KB while running. Failed builds load the full log automatically. Per-step files (when present): <code>docker-build.log</code>, <code>docker-run.log</code> under this builds artifact dir.</p>
<pre id="build-log-tail" class="log-preview log-preview--live" aria-live="polite"></pre> <pre id="build-log-tail" class="log-preview log-preview--live" aria-live="polite"></pre>
<div class="build-log-actions">
<button type="button" class="btn" id="build-log-expand">Show full log</button> <button type="button" class="btn" id="build-log-expand">Show full log</button>
<a class="btn" id="build-log-download" href="<?= h(Auth::basePath()) ?>/api/build_log.php?id=<?= (int) $build['id'] ?>&amp;download=1">Download log</a>
</div>
</section> </section>

View File

@@ -22,7 +22,8 @@ $links = cfg('links', []);
</head> </head>
<body data-base-path="<?= h(Auth::basePath()) ?>" <body data-base-path="<?= h(Auth::basePath()) ?>"
data-view="<?= h($view ?? 'home') ?>" data-view="<?= h($view ?? 'home') ?>"
<?= (($view ?? '') === 'build' && !empty($build['id'])) ? ' data-build-id="' . (int) $build['id'] . '"' : '' ?>> <?= (($view ?? '') === 'build' && !empty($build['id'])) ? ' data-build-id="' . (int) $build['id'] . '"' : '' ?>
<?= (($view ?? '') === 'build' && !empty($build['status'])) ? ' data-build-status="' . h((string) $build['status']) . '"' : '' ?>>
<div class="shell"> <div class="shell">
<nav class="nav-pane" id="nav-pane" aria-label="Builder navigation"> <nav class="nav-pane" id="nav-pane" aria-label="Builder navigation">
<button type="button" class="nav-handle" id="nav-handle" aria-label="Toggle navigation"> <button type="button" class="nav-handle" id="nav-handle" aria-label="Toggle navigation">

View File

@@ -9,6 +9,7 @@
# #
# Parallel builds: set BUILD_ISOLATE_SOURCE=1 (default from builder) to shallow-clone into # Parallel builds: set BUILD_ISOLATE_SOURCE=1 (default from builder) to shallow-clone into
# $OUT_DIR/src instead of mutating the shared deploy checkout. # $OUT_DIR/src instead of mutating the shared deploy checkout.
# Per-build step logs: $OUT_DIR/docker-build.log, $OUT_DIR/docker-run.log (also echoed to build.log).
set -euo pipefail set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
@@ -23,6 +24,7 @@ ISOLATE_SOURCE="${BUILD_ISOLATE_SOURCE:-1}"
CLONE_DEPTH="${BUILD_GIT_DEPTH:-1}" CLONE_DEPTH="${BUILD_GIT_DEPTH:-1}"
SOURCE_DIR="${OUT_DIR}/src" SOURCE_DIR="${OUT_DIR}/src"
DOCKER_WORKSPACE="${WORK_DIR}" DOCKER_WORKSPACE="${WORK_DIR}"
CONTAINER_NAME="androidcast-bld-${BUILD_ID}"
mkdir -p "$(dirname "$LOG_FILE")" "$OUT_DIR" mkdir -p "$(dirname "$LOG_FILE")" "$OUT_DIR"
@@ -42,6 +44,36 @@ run_logged() {
fi fi
} }
# Mirror command output to build.log (stdout) and a per-build step file for parallel debugging.
run_step() {
local step_name="$1"
shift
local step_log="${OUT_DIR}/${step_name}.log"
log "[runner] step ${step_name} -> ${step_log}"
"$@" 2>&1 | tee -a "${step_log}"
}
on_runner_err() {
local ec=$?
log "[runner] ERROR: command failed (exit ${ec}) at ${BASH_SOURCE[0]}:${LINENO}: ${BASH_COMMAND}"
if command -v docker >/dev/null 2>&1; then
if docker ps -a --format '{{.Names}}' 2>/dev/null | grep -qx "${CONTAINER_NAME}"; then
log "[runner] docker logs ${CONTAINER_NAME} (last 100 lines):"
docker logs --tail 100 "${CONTAINER_NAME}" 2>&1 | while IFS= read -r line; do log "$line"; done
fi
fi
echo "${ec}" > "${OUT_DIR}/runner.exit"
exit "${ec}"
}
on_runner_exit() {
local ec=$?
echo "${ec}" > "${OUT_DIR}/runner.exit"
}
trap on_runner_err ERR
trap on_runner_exit EXIT
# Git 2.35+ refuses repos owned by another uid (PHP-FPM nginx vs deploy user). # Git 2.35+ refuses repos owned by another uid (PHP-FPM nginx vs deploy user).
git_safe() { git_safe() {
git -c safe.directory="${WORK_DIR}" "$@" git -c safe.directory="${WORK_DIR}" "$@"
@@ -69,24 +101,24 @@ prepare_isolated_source() {
rm -rf "${SOURCE_DIR}" rm -rf "${SOURCE_DIR}"
mkdir -p "${SOURCE_DIR}" mkdir -p "${SOURCE_DIR}"
log "[runner] isolate: shallow clone depth=${CLONE_DEPTH} ref=${ref} -> ${SOURCE_DIR}" log "[runner] isolate: shallow clone depth=${CLONE_DEPTH} ref=${ref} remote=${remote} -> ${SOURCE_DIR}"
if git clone --depth "${CLONE_DEPTH}" --recurse-submodules --shallow-submodules \ if git clone --depth "${CLONE_DEPTH}" --recurse-submodules --shallow-submodules \
--single-branch --branch "${ref}" "${remote}" "${SOURCE_DIR}" 2>/dev/null; then --single-branch --branch "${ref}" "${remote}" "${SOURCE_DIR}"; then
: :
else else
log "[runner] isolate: branch clone failed; clone default branch then checkout ${ref}" log "[runner] isolate: branch clone failed; clone default branch then checkout ${ref}"
run_logged git clone --depth "${CLONE_DEPTH}" --recurse-submodules --shallow-submodules \ run_step git-clone git clone --depth "${CLONE_DEPTH}" --recurse-submodules --shallow-submodules \
"${remote}" "${SOURCE_DIR}" "${remote}" "${SOURCE_DIR}"
run_logged git -c safe.directory="${SOURCE_DIR}" -C "${SOURCE_DIR}" fetch --depth "${CLONE_DEPTH}" origin "${ref}" run_step git-fetch git -c safe.directory="${SOURCE_DIR}" -C "${SOURCE_DIR}" fetch --depth "${CLONE_DEPTH}" origin "${ref}"
run_logged git -c safe.directory="${SOURCE_DIR}" -C "${SOURCE_DIR}" checkout "${ref}" run_step git-checkout git -c safe.directory="${SOURCE_DIR}" -C "${SOURCE_DIR}" checkout "${ref}"
fi fi
if [[ -n "${GIT_SHA:-}" ]]; then if [[ -n "${GIT_SHA:-}" ]]; then
log "[runner] isolate: checkout pin GIT_SHA=${GIT_SHA}" log "[runner] isolate: checkout pin GIT_SHA=${GIT_SHA}"
run_logged git -c safe.directory="${SOURCE_DIR}" -C "${SOURCE_DIR}" checkout "${GIT_SHA}" run_step git-pin git -c safe.directory="${SOURCE_DIR}" -C "${SOURCE_DIR}" checkout "${GIT_SHA}"
fi fi
run_logged git -c safe.directory="${SOURCE_DIR}" -C "${SOURCE_DIR}" rev-parse HEAD run_step git-rev-parse git -c safe.directory="${SOURCE_DIR}" -C "${SOURCE_DIR}" rev-parse HEAD
DOCKER_WORKSPACE="${SOURCE_DIR}" DOCKER_WORKSPACE="${SOURCE_DIR}"
} }
@@ -104,7 +136,7 @@ sync_shared_checkout() {
run_logged git_safe -C "${WORK_DIR}" rev-parse HEAD run_logged git_safe -C "${WORK_DIR}" rev-parse HEAD
} }
log "[runner] image=${IMAGE} ci_version=${CI_VERSION} build_id=${BUILD_ID}" log "[runner] image=${IMAGE} ci_version=${CI_VERSION} build_id=${BUILD_ID} container=${CONTAINER_NAME}"
log "[runner] workdir=${WORK_DIR} out=${OUT_DIR} isolate=${ISOLATE_SOURCE}" log "[runner] workdir=${WORK_DIR} out=${OUT_DIR} isolate=${ISOLATE_SOURCE}"
if [[ "$ISOLATE_SOURCE" == "1" ]]; then if [[ "$ISOLATE_SOURCE" == "1" ]]; then
@@ -115,13 +147,13 @@ fi
log "[runner] docker workspace=${DOCKER_WORKSPACE}" log "[runner] docker workspace=${DOCKER_WORKSPACE}"
run_logged docker build \ run_step docker-build docker build \
--build-arg ANDROIDCAST_CI_VERSION="${CI_VERSION}" \ --build-arg ANDROIDCAST_CI_VERSION="${CI_VERSION}" \
-t "$IMAGE" \ -t "$IMAGE" \
-f "${ROOT}/Dockerfile" \ -f "${ROOT}/Dockerfile" \
"${ROOT}" "${ROOT}"
run_logged docker run --rm \ run_step docker-run docker run --rm --name "${CONTAINER_NAME}" \
-v "${DOCKER_WORKSPACE}:/workspace" \ -v "${DOCKER_WORKSPACE}:/workspace" \
-v "${ROOT}/out:/workspace/out" \ -v "${ROOT}/out:/workspace/out" \
-w /workspace \ -w /workspace \