Skip to content

Commit 9e28d1f

Browse files
committed
f
1 parent 6e2e489 commit 9e28d1f

2 files changed

Lines changed: 86 additions & 86 deletions

File tree

.github/workflows/build_master.yml

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,6 @@ jobs:
5757
exit 1
5858
fi
5959
60-
# Clone the searchindex repo
61-
git clone https://x-access-token:${TOKEN}@github.com/${TARGET_REPO}.git /tmp/searchindex-repo
62-
63-
cd /tmp/searchindex-repo
64-
git config user.name "GitHub Actions"
65-
git config user.email "github-actions@github.com"
66-
67-
# Save all current files from main branch to temp directory
68-
mkdir -p /tmp/searchindex-backup
69-
cp -r * /tmp/searchindex-backup/ 2>/dev/null || true
70-
71-
# Create a fresh orphan branch (no history)
72-
git checkout --orphan new-main
73-
74-
# Remove all files from git index (but keep working directory)
75-
git rm -rf . 2>/dev/null || true
76-
77-
# Restore all the files from backup (keeps all language files)
78-
cp -r /tmp/searchindex-backup/* . 2>/dev/null || true
79-
80-
# Now update/add our English searchindex file
8160
# First, compress the original file (in the build directory)
8261
cd "${GITHUB_WORKSPACE}"
8362
gzip -9 -k -f "$ASSET"
@@ -107,21 +86,57 @@ jobs:
10786
EOF
10887
python3 /tmp/xor_encrypt.py "$KEY" "${ASSET}.gz" "${ASSET}.gz.enc"
10988
110-
# Copy ONLY the encrypted .gz version to the searchindex repo (no uncompressed .js)
111-
cd /tmp/searchindex-repo
112-
cp "${GITHUB_WORKSPACE}/${ASSET}.gz.enc" "${FILENAME}.gz"
113-
114-
# Stage all files
115-
git add -A
116-
117-
# Commit with timestamp
118-
TIMESTAMP=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
119-
git commit -m "Update searchindex files - ${TIMESTAMP}" --allow-empty
120-
121-
# Force push to replace master branch (deletes history, keeps all files)
122-
git push -f origin new-main:master
123-
124-
echo "Successfully reset repository history and pushed all searchindex files"
89+
# Rebuild and force-push with retries to handle concurrent updates.
90+
MAX_RETRIES=20
91+
RETRY_COUNT=0
92+
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
93+
RETRY_COUNT=$((RETRY_COUNT + 1))
94+
echo "Push attempt ${RETRY_COUNT}/${MAX_RETRIES}"
95+
96+
rm -rf /tmp/searchindex-repo /tmp/searchindex-backup
97+
git clone https://x-access-token:${TOKEN}@github.com/${TARGET_REPO}.git /tmp/searchindex-repo
98+
99+
cd /tmp/searchindex-repo
100+
git config user.name "GitHub Actions"
101+
git config user.email "github-actions@github.com"
102+
103+
# Save all current files from master branch to temp directory.
104+
mkdir -p /tmp/searchindex-backup
105+
cp -r * /tmp/searchindex-backup/ 2>/dev/null || true
106+
107+
# Create a fresh orphan branch (no history).
108+
git checkout --orphan new-main
109+
110+
# Remove all files from git index (but keep working directory).
111+
git rm -rf . 2>/dev/null || true
112+
113+
# Restore all files from backup (keeps all language files).
114+
cp -r /tmp/searchindex-backup/* . 2>/dev/null || true
115+
116+
# Update English searchindex artifact.
117+
cp "${GITHUB_WORKSPACE}/${ASSET}.gz.enc" "${FILENAME}.gz"
118+
119+
git add -A
120+
TIMESTAMP=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
121+
git commit -m "Update searchindex files - ${TIMESTAMP}" --allow-empty
122+
123+
if git push -f origin new-main:master 2>&1 | tee /tmp/push_output.txt; then
124+
echo "Successfully reset repository history and pushed all searchindex files"
125+
break
126+
fi
127+
128+
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
129+
if grep -q "cannot lock ref 'refs/heads/master'" /tmp/push_output.txt; then
130+
echo "Concurrent update detected on remote master. Retrying..."
131+
else
132+
echo "Force push failed. Retrying..."
133+
fi
134+
sleep 1
135+
else
136+
echo "Failed to push after ${MAX_RETRIES} attempts"
137+
exit 1
138+
fi
139+
done
125140
126141
# Login in AWs
127142
- name: Configure AWS credentials using OIDC

.github/workflows/translate_all.yml

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -206,62 +206,47 @@ jobs:
206206
# Copy ONLY the encrypted .gz version to the searchindex repo (no uncompressed .js)
207207
cp "${ASSET}.gz.enc" "/tmp/searchindex-repo/${FILENAME}.gz"
208208
209-
# Commit and push with retry logic
209+
# Commit and push with retry logic resilient to concurrent writers/history rewrites
210210
cd /tmp/searchindex-repo
211211
git config user.name "GitHub Actions"
212212
git config user.email "github-actions@github.com"
213-
git add "${FILENAME}.gz"
214-
215-
if git diff --staged --quiet; then
216-
echo "No changes to commit"
217-
else
213+
214+
MAX_RETRIES=20
215+
RETRY_COUNT=0
216+
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
217+
RETRY_COUNT=$((RETRY_COUNT + 1))
218+
echo "Push attempt ${RETRY_COUNT}/${MAX_RETRIES}"
219+
220+
# Rebuild commit from latest remote state each attempt.
221+
git fetch origin master
222+
git checkout -B master origin/master
223+
cp "${GITHUB_WORKSPACE}/${ASSET}.gz.enc" "${FILENAME}.gz"
224+
git add "${FILENAME}.gz"
225+
226+
if git diff --staged --quiet; then
227+
echo "No changes to commit"
228+
break
229+
fi
230+
218231
git commit -m "Update ${FILENAME} from hacktricks-cloud build"
219-
220-
# Retry push up to 20 times with pull --rebase between attempts
221-
MAX_RETRIES=20
222-
RETRY_COUNT=0
223-
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
224-
if git push origin master; then
225-
echo "Successfully pushed on attempt $((RETRY_COUNT + 1))"
226-
break
232+
233+
if git push origin master 2>&1 | tee /tmp/push_output.txt; then
234+
echo "Successfully pushed on attempt ${RETRY_COUNT}"
235+
break
236+
fi
237+
238+
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
239+
if grep -q "cannot lock ref 'refs/heads/master'" /tmp/push_output.txt; then
240+
echo "Concurrent update detected on remote master. Retrying..."
227241
else
228-
RETRY_COUNT=$((RETRY_COUNT + 1))
229-
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
230-
echo "Push failed, attempt $RETRY_COUNT/$MAX_RETRIES. Pulling and retrying..."
231-
232-
# Try normal rebase first
233-
if git pull --rebase origin master 2>&1 | tee /tmp/pull_output.txt; then
234-
echo "Rebase successful, retrying push..."
235-
else
236-
# If rebase fails due to divergent histories (orphan branch reset), re-clone
237-
if grep -q "unrelated histories\|refusing to merge\|fatal: invalid upstream\|couldn't find remote ref" /tmp/pull_output.txt; then
238-
echo "Detected history rewrite, re-cloning repository..."
239-
cd /tmp
240-
rm -rf searchindex-repo
241-
git clone https://x-access-token:${TOKEN}@github.com/${TARGET_REPO}.git searchindex-repo
242-
cd searchindex-repo
243-
git config user.name "GitHub Actions"
244-
git config user.email "github-actions@github.com"
245-
246-
# Re-copy ONLY the encrypted .gz version (no uncompressed .js)
247-
cp "${ASSET}.gz.enc" "${FILENAME}.gz"
248-
249-
git add "${FILENAME}.gz"
250-
git commit -m "Update ${FILENAME}.gz from hacktricks-cloud build"
251-
echo "Re-cloned and re-committed, will retry push..."
252-
else
253-
echo "Rebase failed for unknown reason, retrying anyway..."
254-
fi
255-
fi
256-
257-
sleep 1
258-
else
259-
echo "Failed to push after $MAX_RETRIES attempts"
260-
exit 1
261-
fi
242+
echo "Push failed. Retrying with fresh remote state..."
262243
fi
263-
done
264-
fi
244+
sleep 1
245+
else
246+
echo "Failed to push after ${MAX_RETRIES} attempts"
247+
exit 1
248+
fi
249+
done
265250
266251
# Login in AWs
267252
- name: Configure AWS credentials using OIDC

0 commit comments

Comments
 (0)