Skip to content

Commit ca77bbb

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

File tree

2 files changed

+92
-20
lines changed

2 files changed

+92
-20
lines changed

.github/workflows/integration-test.yml

Lines changed: 60 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,69 @@ 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+
steps:
169+
- name: Checkout split-tests-java-action
170+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
171+
172+
- name: Checkout JUnit reports (split-index 1)
173+
uses: ./checkout-junit-reports
174+
with:
175+
split-index: 1
176+
git-branch: junit-reports-it-${{ github.sha }}-nonexistent
177+
path: junit-reports-first-index-1
178+
179+
- name: Checkout JUnit reports (split-index 0a)
180+
uses: ./checkout-junit-reports
181+
with:
182+
split-index: 0
183+
git-branch: junit-reports-it-${{ github.sha }}-nonexistent
184+
path: junit-reports-first-index-0
185+
186+
- name: Checkout JUnit reports (split-index 2)
187+
uses: ./checkout-junit-reports
188+
with:
189+
split-index: 2
190+
git-branch: junit-reports-it-${{ github.sha }}-nonexistent
191+
path: junit-reports-first-index-2
192+
193+
- name: Checkout JUnit reports (split-index 0b)
194+
uses: ./checkout-junit-reports
195+
with:
196+
split-index: 0
197+
git-branch: junit-reports-it-${{ github.sha }}-nonexistent
198+
path: junit-reports-first-index-3
199+
200+
- name: Checkout JUnit reports (split-index 0c)
201+
uses: ./checkout-junit-reports
202+
with:
203+
split-index: 0
204+
git-branch: junit-reports-it-${{ github.sha }}-nonexistent
205+
path: junit-reports-first-index-4
206+
upload-artifact: false
207+
208+
- name: Assert JUnit reports
209+
run: |
210+
for SPLIT_INDEX in {0..3}; do
211+
echo "Checking JUnit reports for split-index $SPLIT_INDEX"
212+
cd junit-reports-first-index-$SPLIT_INDEX
213+
FILE_COUNT=$(ls -1 | wc -l)
214+
if [[ "$FILE_COUNT" -ne 0 ]]; then
215+
echo "Error: Expected empty directory, but found $FILE_COUNT files!"
216+
ls -l
217+
exit 1
218+
fi
219+
cd ..
220+
done
221+
165222
generate-split-index-json:
166223
name: Generate split indexes
167224
runs-on: ubuntu-latest
168225
needs:
169-
checkout-junit-reports-test
226+
- checkout-junit-reports
227+
- checkout-junit-reports-with-nonexistent-branch
170228
outputs:
171229
json: ${{ steps.generate.outputs.split-index-json }}
172230
steps:

checkout-junit-reports/action.yml

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,53 @@ 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 "${{ inputs.path }}"
85+
mkdir -p "${{ inputs.path }}"
86+
UPLOAD_SHA_ARTIFACT=false
87+
fi
7288
fi
7389
# we can only upload the artifact once, so only do this on the first split
7490
if [ "${{ inputs.split-index }}" != "0" ]; then
7591
echo "Skipping upload of JUnit reports SHA on this index"
7692
UPLOAD_SHA_ARTIFACT=false
93+
elif [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then
94+
echo "Will upload JUnit reports SHA for branch $CHECKOUT_REF"
95+
else
96+
echo "Will not upload JUnit reports SHA"
7797
fi
7898
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 }}
8699
87100
- name: Save JUnit reports SHA
88101
if: ${{ steps.junit-reports-sha.outputs.upload-artifact == 'true' }}
@@ -91,7 +104,8 @@ runs:
91104
run: |
92105
SHA=$(git rev-parse HEAD)
93106
echo "Saving JUnit reports SHA $SHA"
94-
cd ../${{ inputs.artifact-path }}
107+
mkdir -p "../${{ inputs.artifact-path }}"
108+
cd "../${{ inputs.artifact-path }}"
95109
echo -n "$SHA" > junit-reports-sha.txt
96110
97111
- name: Upload JUnit report SHA

0 commit comments

Comments
 (0)