Skip to content

Commit d7e49a2

Browse files
committed
Fix concurrent pushes in merge-junit-reports sub-action
1 parent 5ca2ae5 commit d7e49a2

2 files changed

Lines changed: 141 additions & 15 deletions

File tree

‎.github/workflows/integration-test.yml‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,126 @@ jobs:
418418
run: |
419419
git push origin --delete junit-reports-it-${{ github.sha }}-existing || true
420420
421+
merge-junit-reports-with-concurrent-push:
422+
name: Merge JUnit reports with concurrent push
423+
runs-on: ubuntu-latest
424+
needs:
425+
- integration-test
426+
permissions:
427+
contents: write
428+
env:
429+
BRANCH: junit-reports-it-${{ github.sha }}-concurrent
430+
steps:
431+
- name: Checkout split-tests-java-action
432+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
433+
434+
- name: Create JUnit reports branch
435+
run: |
436+
echo "Creating branch $BRANCH"
437+
git switch --orphan $BRANCH
438+
git config user.name "${{ github.actor }}"
439+
git config user.email "${{ github.actor }}@users.noreply.github.com"
440+
git commit --allow-empty -m "Initial commit for JUnit reports branch"
441+
git push origin --set-upstream $BRANCH
442+
git switch ${{ github.ref_name }}
443+
444+
- name: Checkout JUnit reports for concurrent push
445+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
446+
with:
447+
path: junit-reports-concurrent
448+
ref: ${{ env.BRANCH }}
449+
450+
- name: Prepare concurrent commit
451+
working-directory: junit-reports-concurrent
452+
run: |
453+
git config user.name "${{ github.actor }}"
454+
git config user.email "${{ github.actor }}@users.noreply.github.com"
455+
echo '<testsuite name="de.donnerbart.example.ConcurrentTest"/>' > TEST-de.donnerbart.example.ConcurrentTest.xml
456+
git add TEST-de.donnerbart.example.ConcurrentTest.xml
457+
git commit -m "Concurrent JUnit reports update"
458+
echo "concurrent-sha=$(git rev-parse HEAD)" >> "$GITHUB_ENV"
459+
460+
- name: Install pre-push hook that pushes the concurrent commit first
461+
run: |
462+
HOOKS_DIR="$RUNNER_TEMP/git-hooks"
463+
mkdir -p "$HOOKS_DIR"
464+
cat > "$HOOKS_DIR/pre-push" <<'HOOK'
465+
#!/usr/bin/env bash
466+
MARKER="$RUNNER_TEMP/concurrent-push-done"
467+
if [[ -f "$MARKER" ]]; then
468+
exit 0
469+
fi
470+
touch "$MARKER"
471+
env -u GIT_DIR -u GIT_WORK_TREE -u GIT_INDEX_FILE \
472+
git -C "$GITHUB_WORKSPACE/junit-reports-concurrent" push --quiet origin HEAD:$BRANCH
473+
HOOK
474+
chmod +x "$HOOKS_DIR/pre-push"
475+
git config --global core.hooksPath "$HOOKS_DIR"
476+
477+
- name: Merge JUnit reports
478+
uses: ./merge-junit-reports
479+
with:
480+
git-branch: ${{ env.BRANCH }}
481+
artifact-name: junit-xml-report-concurrent
482+
split-artifact-pattern: junit-xml-reports-*
483+
split-artifact-delete-merged: false
484+
485+
- name: Remove pre-push hook
486+
if: always()
487+
run: git config --global --unset core.hooksPath || true
488+
489+
- name: Checkout JUnit reports
490+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
491+
with:
492+
path: junit-reports-assertion
493+
ref: ${{ env.BRANCH }}
494+
fetch-depth: 0
495+
496+
- name: Assert JUnit reports
497+
working-directory: junit-reports-assertion
498+
run: |
499+
if [[ ! -f "$RUNNER_TEMP/concurrent-push-done" ]]; then
500+
echo "Error: The pre-push hook did not push the concurrent commit!"
501+
exit 1
502+
fi
503+
if ! git merge-base --is-ancestor ${{ env.concurrent-sha }} HEAD; then
504+
echo "Error: The concurrent commit ${{ env.concurrent-sha }} is not part of $BRANCH!"
505+
git log --oneline
506+
exit 1
507+
fi
508+
for SPLIT_INDEX in {0..3}; do
509+
REPORT_FILE="TEST-de.donnerbart.example.ActionSplit${SPLIT_INDEX}Test.xml"
510+
SEARCH_STRING='name="de.donnerbart.example.ActionSplit'${SPLIT_INDEX}'Test"'
511+
if [[ ! -f "$REPORT_FILE" ]]; then
512+
echo "Error: JUnit report $REPORT_FILE not found!"
513+
ls -l
514+
exit 1
515+
fi
516+
if ! grep -q "$SEARCH_STRING" "$REPORT_FILE"; then
517+
echo "Error: JUnit report $REPORT_FILE does not contain the required string '$SEARCH_STRING'!"
518+
cat $REPORT_FILE
519+
exit 1
520+
fi
521+
echo "JUnit report $REPORT_FILE is valid"
522+
done
523+
FILE_COUNT=$(ls -1 | wc -l)
524+
if [[ "$FILE_COUNT" -ne 4 ]]; then
525+
echo "Error: Expected 4 JUnit reports, but found $FILE_COUNT files!"
526+
ls -l
527+
exit 1
528+
fi
529+
530+
- name: Clean up JUnit reports branch
531+
if: always()
532+
run: |
533+
git push origin --delete $BRANCH || true
534+
421535
merge-junit-reports-with-new-branch:
422536
name: Merge JUnit reports with new branch
423537
runs-on: ubuntu-latest
424538
needs:
425539
- merge-junit-reports-with-existing-branch
540+
- merge-junit-reports-with-concurrent-push
426541
permissions:
427542
contents: write
428543
steps:

‎merge-junit-reports/action.yml‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,30 @@ runs:
8989
echo -n "JUnit report size after cleanup: "
9090
du -sch *.xml | tail -n 1 | cut -f1
9191
92-
- name: Delete old JUnit reports
93-
if: ${{ inputs.delete-old-junit-reports }}
94-
working-directory: junit-reports
95-
shell: bash
96-
run: git rm *.xml || true
97-
98-
- name: Copy new JUnit reports
99-
shell: bash
100-
run: cp junit-reports-artifact/*.xml junit-reports/
101-
10292
- name: Update JUnit reports
103-
uses: EndBug/add-and-commit@cc9c08ba6c8df3b93a8f2db63e89b98368ae2ae8 # v11
104-
with:
105-
message: 'Update JUnit reports'
106-
add: '*.xml'
107-
cwd: junit-reports
93+
shell: bash
94+
working-directory: junit-reports
95+
run: |
96+
git config user.name "${{ github.actor }}"
97+
git config user.email "${{ github.actor }}@users.noreply.github.com"
98+
for ATTEMPT in {1..5}; do
99+
git fetch --quiet origin ${{ inputs.git-branch }}
100+
git reset --quiet --hard FETCH_HEAD
101+
if [[ "${{ inputs.delete-old-junit-reports }}" == "true" ]]; then
102+
git rm --quiet *.xml || true
103+
fi
104+
cp ../junit-reports-artifact/*.xml .
105+
git add *.xml
106+
if git diff --cached --quiet; then
107+
echo "JUnit reports are unchanged"
108+
exit 0
109+
fi
110+
git commit --quiet -m "Update JUnit reports"
111+
if git push origin HEAD:${{ inputs.git-branch }}; then
112+
exit 0
113+
fi
114+
echo "Push of JUnit reports failed (attempt $ATTEMPT), retrying with the latest ${{ inputs.git-branch }}"
115+
sleep $((ATTEMPT * 5))
116+
done
117+
echo "::error::Could not push JUnit reports to ${{ inputs.git-branch }}"
118+
exit 1

0 commit comments

Comments
 (0)