WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions workflow-steps/checkout/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ const maxRetries = 3;

async function main() {
if (process.platform != 'win32') {
execSync(`git config --global --add safe.directory $PWD`);
runWithRetries(
`git config --global --add safe.directory $PWD`,
'set safe directory',
1,
);
}
execSync('git init .');
execSync(`git remote add origin ${repoUrl}`);
execSync(`echo "GIT_REPOSITORY_URL=''" >> $NX_CLOUD_ENV`);
runWithRetries('git init .', 'git init', 1);
runWithRetries(`git remote add origin ${repoUrl}`, 'git remote add', 1);
runWithRetries(
`echo "GIT_REPOSITORY_URL=''" >> $NX_CLOUD_ENV`,
'persist git url',
1,
);

let fetchCommand: string;
if (commitSha.startsWith('origin/')) {
Expand All @@ -28,34 +36,32 @@ async function main() {
}
}

await runWithRetries(() => execSync(fetchCommand), 'git fetch', maxRetries);
await runWithRetries(fetchCommand, 'git fetch', maxRetries);

const checkoutCommand = `git checkout --progress --force -B ${nxBranch} ${commitSha}`;
await runWithRetries(
() => execSync(checkoutCommand),
'git checkout',
maxRetries,
);
await runWithRetries(checkoutCommand, 'git checkout', maxRetries);
}

async function runWithRetries(
fn: () => void,
command: string,
label: string,
maxRetriesLocal: number,
) {
let attempt = 0;
while (attempt < maxRetriesLocal) {
try {
fn();
console.log(`\n--- ${command} attempt ${attempt + 1} ---`);
execSync(command, { stdio: 'inherit' });
return;
} catch (e) {
attempt++;

if (attempt >= maxRetriesLocal) {
throw e;
}
const jitter = Math.floor(Math.random() * 1_000);
const delayMs = (attempt === 1 ? 10_000 : 60_000) + jitter;

const delayMs = attempt === 1 ? 10_000 : 60_000;
const stderr = (e as any)?.stderr?.toString?.() || '';
const stdout = (e as any)?.stdout?.toString?.() || '';
if (stderr) {
Expand Down
33 changes: 20 additions & 13 deletions workflow-steps/checkout/output/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ var fetchTags = process.env.GIT_FETCH_TAGS === "true";
var maxRetries = 3;
async function main() {
if (process.platform != "win32") {
(0, import_child_process.execSync)(`git config --global --add safe.directory $PWD`);
runWithRetries(
`git config --global --add safe.directory $PWD`,
"set safe directory",
1
);
}
(0, import_child_process.execSync)("git init .");
(0, import_child_process.execSync)(`git remote add origin ${repoUrl}`);
(0, import_child_process.execSync)(`echo "GIT_REPOSITORY_URL=''" >> $NX_CLOUD_ENV`);
runWithRetries("git init .", "git init", 1);
runWithRetries(`git remote add origin ${repoUrl}`, "git remote add", 1);
runWithRetries(
`echo "GIT_REPOSITORY_URL=''" >> $NX_CLOUD_ENV`,
"persist git url",
1
);
let fetchCommand;
if (commitSha.startsWith("origin/")) {
fetchCommand = `git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin ${nxBranch}`;
Expand All @@ -24,26 +32,25 @@ async function main() {
fetchCommand = `git fetch ${tagsArg} --prune --progress --no-recurse-submodules --depth=${depth} origin ${commitSha}`;
}
}
await runWithRetries(() => (0, import_child_process.execSync)(fetchCommand), "git fetch", maxRetries);
await runWithRetries(fetchCommand, "git fetch", maxRetries);
const checkoutCommand = `git checkout --progress --force -B ${nxBranch} ${commitSha}`;
await runWithRetries(
() => (0, import_child_process.execSync)(checkoutCommand),
"git checkout",
maxRetries
);
await runWithRetries(checkoutCommand, "git checkout", maxRetries);
}
async function runWithRetries(fn, label, maxRetriesLocal) {
async function runWithRetries(command, label, maxRetriesLocal) {
let attempt = 0;
while (attempt < maxRetriesLocal) {
try {
fn();
console.log(`
--- ${command} attempt ${attempt + 1} ---`);
(0, import_child_process.execSync)(command, { stdio: "inherit" });
return;
} catch (e) {
attempt++;
if (attempt >= maxRetriesLocal) {
throw e;
}
const delayMs = attempt === 1 ? 1e4 : 6e4;
const jitter = Math.floor(Math.random() * 1e3);
const delayMs = (attempt === 1 ? 1e4 : 6e4) + jitter;
const stderr = e?.stderr?.toString?.() || "";
const stdout = e?.stdout?.toString?.() || "";
if (stderr) {
Expand Down