Skip to content

Commit d1f7864

Browse files
committed
Add support for checking out non-existent branches in JUnit reports workflow
1 parent a4f7794 commit d1f7864

File tree

2 files changed

+104
-20
lines changed

2 files changed

+104
-20
lines changed

.github/workflows/integration-test.yml

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ env:
1212
split-total: 4
1313

1414
jobs:
15-
checkout-junit-reports-test:
15+
checkout-junit-reports:
1616
name: Checkout JUnit reports
1717
runs-on: ubuntu-latest
1818
steps:
@@ -75,6 +75,11 @@ jobs:
7575
run: |
7676
for SPLIT_INDEX in {0..3}; do
7777
echo "Checking JUnit reports for split-index $SPLIT_INDEX"
78+
if [[ ! -d "junit-reports-first-index-$SPLIT_INDEX" ]]; then
79+
echo "Error: JUnit report directory not found!"
80+
ls -l
81+
exit 1
82+
fi
7883
cd junit-reports-first-index-$SPLIT_INDEX
7984
REPORT_FILE="first.xml"
8085
if [[ ! -f "$REPORT_FILE" ]]; then
@@ -162,11 +167,75 @@ jobs:
162167
name: junit-xml-reports-sha
163168
failOnError: false
164169

170+
checkout-junit-reports-with-nonexistent-branch:
171+
name: Checkout JUnit reports with non-existent branch
172+
runs-on: ubuntu-latest
173+
needs:
174+
- checkout-junit-reports
175+
steps:
176+
- name: Checkout split-tests-java-action
177+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
178+
179+
- name: Checkout JUnit reports (split-index 1)
180+
uses: ./checkout-junit-reports
181+
with:
182+
split-index: 1
183+
git-branch: junit-reports-it-nonexistent
184+
path: junit-reports-first-index-1
185+
186+
- name: Checkout JUnit reports (split-index 0a)
187+
uses: ./checkout-junit-reports
188+
with:
189+
split-index: 0
190+
git-branch: junit-reports-it-nonexistent
191+
path: junit-reports-first-index-0
192+
193+
- name: Checkout JUnit reports (split-index 2)
194+
uses: ./checkout-junit-reports
195+
with:
196+
split-index: 2
197+
git-branch: junit-reports-it-nonexistent
198+
path: junit-reports-first-index-2
199+
200+
- name: Checkout JUnit reports (split-index 0b)
201+
uses: ./checkout-junit-reports
202+
with:
203+
split-index: 0
204+
git-branch: junit-reports-it-nonexistent
205+
path: junit-reports-first-index-3
206+
207+
- name: Checkout JUnit reports (split-index 0c)
208+
uses: ./checkout-junit-reports
209+
with:
210+
split-index: 0
211+
git-branch: junit-reports-it-nonexistent
212+
path: junit-reports-first-index-4
213+
upload-artifact: false
214+
215+
- name: Assert JUnit reports
216+
run: |
217+
for SPLIT_INDEX in {0..3}; do
218+
echo "Checking JUnit reports for split-index $SPLIT_INDEX"
219+
if [[ ! -d "junit-reports-first-index-$SPLIT_INDEX" ]]; then
220+
echo "Error: JUnit report directory not found!"
221+
ls -l
222+
exit 1
223+
fi
224+
cd junit-reports-first-index-$SPLIT_INDEX || exit 1
225+
FILE_COUNT=$(ls -1 | wc -l)
226+
if [[ "$FILE_COUNT" -ne 0 ]]; then
227+
echo "Error: Expected empty directory, but found $FILE_COUNT files!"
228+
ls -l
229+
exit 1
230+
fi
231+
cd ..
232+
done
233+
165234
generate-split-index-json:
166235
name: Generate split indexes
167236
runs-on: ubuntu-latest
168237
needs:
169-
checkout-junit-reports-test
238+
- checkout-junit-reports-with-nonexistent-branch
170239
outputs:
171240
json: ${{ steps.generate.outputs.split-index-json }}
172241
steps:

checkout-junit-reports/action.yml

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,54 @@ runs:
4949
name: ${{ inputs.artifact-name }}
5050
path: ${{ inputs.artifact-path }}
5151

52+
- name: Checkout repository
53+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
54+
with:
55+
path: ${{ inputs.path }}
56+
5257
- name: Check JUnit reports SHA
5358
id: junit-reports-sha
5459
shell: bash
60+
working-directory: ${{ inputs.path }}
5561
run: |
5662
echo "Checking JUnit reports SHA"
5763
UPLOAD_SHA_ARTIFACT=${{ inputs.upload-artifact }}
5864
CHECKOUT_REF=${{ inputs.git-branch }}
59-
if [ -d ${{ inputs.artifact-path }} ]; then
60-
cd ${{ inputs.artifact-path }}
61-
if [ -f junit-reports-sha.txt ]; then
65+
if [ -f "../${{ inputs.artifact-path }}/junit-reports-sha.txt" ]; then
66+
SAVED_SHA=$(cat "../${{ inputs.artifact-path }}/junit-reports-sha.txt")
67+
echo "Checking out saved SHA '$SAVED_SHA' for repeatable test distribution"
68+
git fetch --quiet
69+
if git rev-parse --verify "$SAVED_SHA^{commit}" >/dev/null 2>&1; then
70+
git checkout --quiet "$SAVED_SHA"
6271
UPLOAD_SHA_ARTIFACT=false
63-
CHECKOUT_REF=$(cat junit-reports-sha.txt)
72+
else
73+
echo "Error: Saved SHA '$SAVED_SHA' is not a valid commit. Aborting checkout." >&2
74+
exit 1
6475
fi
6576
else
66-
mkdir ${{ inputs.artifact-path }} || true
67-
fi
68-
if [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then
69-
echo "Checking out JUnit reports from branch $CHECKOUT_REF"
70-
else
71-
echo "Checking out JUnit reports from previously used SHA $CHECKOUT_REF"
77+
# no SHA artifact exists, handle branch checkout and potentially create new SHA artifact
78+
if git ls-remote --exit-code --heads origin "$CHECKOUT_REF"; then
79+
echo "Switching to existing branch: $CHECKOUT_REF"
80+
git fetch --quiet
81+
git switch "$CHECKOUT_REF"
82+
else
83+
echo "Branch $CHECKOUT_REF does not exist, no JUnit test reports available"
84+
cd ..
85+
rm -rf "${{ inputs.path }}"
86+
mkdir -p "${{ inputs.path }}"
87+
UPLOAD_SHA_ARTIFACT=false
88+
fi
7289
fi
7390
# we can only upload the artifact once, so only do this on the first split
7491
if [ "${{ inputs.split-index }}" != "0" ]; then
7592
echo "Skipping upload of JUnit reports SHA on this index"
7693
UPLOAD_SHA_ARTIFACT=false
94+
elif [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then
95+
echo "Will upload JUnit reports SHA for branch $CHECKOUT_REF"
96+
else
97+
echo "Will not upload JUnit reports SHA"
7798
fi
7899
echo "upload-artifact=${UPLOAD_SHA_ARTIFACT}" >> "$GITHUB_OUTPUT"
79-
echo "ref=${CHECKOUT_REF}" >> "$GITHUB_OUTPUT"
80-
81-
- name: Checkout repository
82-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
83-
with:
84-
path: ${{ inputs.path }}
85-
ref: ${{ steps.junit-reports-sha.outputs.ref }}
86100
87101
- name: Save JUnit reports SHA
88102
if: ${{ steps.junit-reports-sha.outputs.upload-artifact == 'true' }}
@@ -91,7 +105,8 @@ runs:
91105
run: |
92106
SHA=$(git rev-parse HEAD)
93107
echo "Saving JUnit reports SHA $SHA"
94-
cd ../${{ inputs.artifact-path }}
108+
mkdir -p "../${{ inputs.artifact-path }}"
109+
cd "../${{ inputs.artifact-path }}"
95110
echo -n "$SHA" > junit-reports-sha.txt
96111
97112
- name: Upload JUnit report SHA

0 commit comments

Comments
 (0)