fix(ci): stream update feed squash push
Release / release (push) Successful in 7m10s

Split the squashed updates feed upload through a temporary branch so large installers are pushed in smaller packs, then publish the completed updates branch.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Ivan Fontosh
2026-05-12 14:14:54 +08:00
parent af4c2616f2
commit 8fa8467db7
4 changed files with 113 additions and 28 deletions
+108 -23
View File
@@ -18,7 +18,8 @@
* DND_FEED_TMP_ROOT — каталог для временного клона feed (по умолчанию GITHUB_WORKSPACE / TMPDIR / os.tmpdir); не используйте узкий /tmp на раннере
* DND_GIT_FETCH_RETRIES — число попыток git fetch (1–6, по умолчанию 6); между попытками — git gc --prune=now (освобождение после обрыва TLS/unpack)
* DND_FEED_SKIP_DISK_CHECK — если "1", не проверять свободное место на томе DND_FEED_TMP_ROOT перед clone
* DND_UPDATES_SQUASH_HISTORY — если "1", после каждого релиза ветка updates в UPDATES_REPO — один корневой коммит (orphan + force-with-lease push), история не копится (меньше места на раннере и на сервере после GC)
* DND_UPDATES_SQUASH_HISTORY — если "1", после каждого релиза ветка updates в UPDATES_REPO переписывается на историю только текущего релиза (orphan + временная ветка + force-with-lease)
* DND_FEED_LARGE_FILE_BYTES — порог "большого" файла для дробления push в squash-режиме (по умолчанию 64 MiB)
*/
import { execFileSync, spawnSync } from 'node:child_process';
import fs from 'node:fs';
@@ -180,16 +181,6 @@ function feedSquashSingleCommitEnabled() {
return process.env.DND_UPDATES_SQUASH_HISTORY?.trim() === '1';
}
/** После merge и add: одна линия без истории (только репо UPDATES_REPO / ветка updates). */
function squashUpdatesBranchToSingleCommit(work, message) {
const orphan = 'dnd-feed-squash-tmp';
runGit(['checkout', '--orphan', orphan], work);
runGit(['add', '-A'], work);
runGit(['commit', '-m', message], work);
runGit(['branch', '-D', 'updates'], work);
runGit(['branch', '-m', 'updates'], work);
}
function hadOriginUpdatesRemoteRef(work) {
try {
execFileSync('git', ['show-ref', '--verify', '--quiet', 'refs/remotes/origin/updates'], {
@@ -202,21 +193,24 @@ function hadOriginUpdatesRemoteRef(work) {
}
}
/** После squash не делаем merge перед push — вернёт старую историю с remote. */
function pushUpdatesBranchAfterSquash(work) {
function sanitizeRefPart(s) {
return s
.replace(/[^0-9A-Za-z._-]+/gu, '-')
.replace(/^-+|-+$/gu, '')
.slice(0, 80);
}
function pushWithRetries(work, args, label) {
const retries = Math.max(1, Math.min(5, Number.parseInt(process.env.DND_GIT_PUSH_RETRIES || '3', 10) || 3));
const args = hadOriginUpdatesRemoteRef(work)
? ['push', '--force-with-lease', '-u', 'origin', 'updates']
: ['push', '-u', 'origin', 'updates'];
let lastError;
for (let attempt = 1; attempt <= retries; attempt += 1) {
try {
console.log(`[sync-update-feed] push updates (squash, attempt ${attempt}/${retries})`);
console.log(`[sync-update-feed] ${label} (attempt ${attempt}/${retries})`);
execFileSync('git', args, { cwd: work, stdio: 'inherit' });
return;
} catch (err) {
lastError = err;
console.warn(`[sync-update-feed] push failed (attempt ${attempt}/${retries})`);
console.warn(`[sync-update-feed] ${label} failed (attempt ${attempt}/${retries})`);
if (attempt < retries) {
sleepSyncSeconds(20);
}
@@ -225,6 +219,101 @@ function pushUpdatesBranchAfterSquash(work) {
throw lastError;
}
function listFeedFiles(work) {
return fs
.readdirSync(work)
.filter((name) => name !== '.git' && fs.statSync(path.join(work, name)).isFile())
.sort((a, b) => a.localeCompare(b));
}
function gitAddFiles(work, files) {
if (files.length === 0) return;
runGit(['add', '--', ...files], work);
}
function commitStagedIfAny(work, message) {
try {
execFileSync('git', ['diff', '--cached', '--quiet'], { cwd: work, stdio: 'ignore' });
return false;
} catch {
runGit(['commit', '-m', message], work);
return true;
}
}
/**
* История старых релизов удаляется, но загрузка идёт маленькими pack'ами:
* временная ветка получает small files + каждый большой файл отдельным push,
* а `updates` передвигается на готовый коммит только в конце.
*/
function publishSquashedUpdatesBranchStreamed(work, message, tag) {
const tempBranch = `updates-upload-${sanitizeRefPart(tag || 'ci')}-${String(Date.now())}`;
const files = listFeedFiles(work);
const largeFileBytes = Math.max(
8_000_000,
Math.min(
256_000_000,
Number.parseInt(process.env.DND_FEED_LARGE_FILE_BYTES || '64000000', 10) || 64_000_000,
),
);
const smallFiles = [];
const largeFiles = [];
for (const file of files) {
const size = fs.statSync(path.join(work, file)).size;
if (size >= largeFileBytes) {
largeFiles.push(file);
} else {
smallFiles.push(file);
}
}
console.warn(
`[sync-update-feed] DND_UPDATES_SQUASH_HISTORY=1: переписываем только UPDATES_REPO/updates; ` +
`push дробится через временную ветку ${tempBranch} (${smallFiles.length} small, ${largeFiles.length} large)`,
);
runGit(['checkout', '--orphan', 'dnd-feed-upload-tmp'], work);
execFileSync('git', ['rm', '-r', '--cached', '--ignore-unmatch', '.'], { cwd: work, stdio: 'inherit' });
let pushedTemp = false;
gitAddFiles(work, smallFiles);
if (commitStagedIfAny(work, `${message} (metadata)`)) {
pushWithRetries(
work,
['push', '--force', '-u', 'origin', `HEAD:refs/heads/${tempBranch}`],
`push temp feed branch ${tempBranch}`,
);
pushedTemp = true;
}
for (const file of largeFiles) {
gitAddFiles(work, [file]);
if (commitStagedIfAny(work, `${message}: ${file}`)) {
pushWithRetries(
work,
['push', ...(pushedTemp ? [] : ['--force', '-u']), 'origin', `HEAD:refs/heads/${tempBranch}`],
`push temp feed object ${file}`,
);
pushedTemp = true;
}
}
if (!pushedTemp) {
throw new Error('[sync-update-feed] no feed files staged for streamed squash push');
}
const updateArgs = hadOriginUpdatesRemoteRef(work)
? ['push', '--force-with-lease', '-u', 'origin', 'HEAD:updates']
: ['push', '-u', 'origin', 'HEAD:updates'];
pushWithRetries(work, updateArgs, 'publish complete feed branch updates');
try {
execFileSync('git', ['push', 'origin', `:refs/heads/${tempBranch}`], { cwd: work, stdio: 'inherit' });
} catch {
console.warn(`[sync-update-feed] temp branch cleanup failed: ${tempBranch}`);
}
}
function mergeOriginUpdates(work) {
const fetchRetries = Math.max(
1,
@@ -342,11 +431,7 @@ function main() {
if (st) {
const msg = `update feed ${tag}`;
if (squash) {
console.warn(
'[sync-update-feed] DND_UPDATES_SQUASH_HISTORY=1: одна линия в UPDATES_REPO (orphan + force-with-lease при наличии origin/updates)',
);
squashUpdatesBranchToSingleCommit(work, msg);
pushUpdatesBranchAfterSquash(work);
publishSquashedUpdatesBranchStreamed(work, msg, tag);
} else {
runGit(['commit', '-m', msg], work);
pushUpdatesBranch(work);