Skip to content

Commit b203a70

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

File tree

2 files changed

+92
-20
lines changed

2 files changed

+92
-20
lines changed

.github/workflows/integration-test.yml

Lines changed: 61 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:
@@ -162,11 +162,70 @@ jobs:
162162
name: junit-xml-reports-sha
163163
failOnError: false
164164

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

checkout-junit-reports/action.yml

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,52 @@ 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+
rm -rf .
85+
UPLOAD_SHA_ARTIFACT=false
86+
fi
7287
fi
7388
# we can only upload the artifact once, so only do this on the first split
7489
if [ "${{ inputs.split-index }}" != "0" ]; then
7590
echo "Skipping upload of JUnit reports SHA on this index"
7691
UPLOAD_SHA_ARTIFACT=false
92+
elif [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then
93+
echo "Will upload JUnit reports SHA for branch $CHECKOUT_REF"
94+
else
95+
echo "Will not upload JUnit reports SHA"
7796
fi
7897
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 }}
8698
8799
- name: Save JUnit reports SHA
88100
if: ${{ steps.junit-reports-sha.outputs.upload-artifact == 'true' }}
@@ -91,7 +103,8 @@ runs:
91103
run: |
92104
SHA=$(git rev-parse HEAD)
93105
echo "Saving JUnit reports SHA $SHA"
94-
cd ../${{ inputs.artifact-path }}
106+
mkdir -p "../${{ inputs.artifact-path }}"
107+
cd "../${{ inputs.artifact-path }}"
95108
echo -n "$SHA" > junit-reports-sha.txt
96109
97110
- name: Upload JUnit report SHA

0 commit comments

Comments
 (0)