Skip to content

Commit 7c3961d

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

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

.github/workflows/integration-test.yml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,49 @@ jobs:
162162
name: junit-xml-reports-sha
163163
failOnError: false
164164

165+
checkout-junit-reports-nonexistent-branch-test:
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 (non-existent branch)
173+
uses: ./checkout-junit-reports
174+
with:
175+
split-index: 0
176+
git-branch: junit-reports-it-${{ github.sha }}-nonexistent
177+
path: junit-reports-nonexistent-branch
178+
upload-artifact: false
179+
180+
- name: Assert non-existent branch checkout
181+
run: |
182+
echo "Verifying that checkout with non-existent branch succeeded"
183+
if [ ! -d "junit-reports-nonexistent-branch" ]; then
184+
echo "Error: Directory junit-reports-nonexistent-branch was not created!"
185+
exit 1
186+
fi
187+
cd junit-reports-nonexistent-branch
188+
# Should be on the default branch, verify we have the action files
189+
if [ ! -f "action.yml" ]; then
190+
echo "Error: action.yml not found - checkout didn't work properly!"
191+
ls -la
192+
exit 1
193+
fi
194+
# Verify we're not on the non-existent branch
195+
CURRENT_BRANCH=$(git branch --show-current)
196+
if [ "$CURRENT_BRANCH" == "junit-reports-it-${{ github.sha }}-nonexistent" ]; then
197+
echo "Error: Should not be on the non-existent branch!"
198+
exit 1
199+
fi
200+
echo "Successfully verified checkout with non-existent branch stays on default branch"
201+
165202
generate-split-index-json:
166203
name: Generate split indexes
167204
runs-on: ubuntu-latest
168205
needs:
169-
checkout-junit-reports-test
206+
- checkout-junit-reports-test
207+
- checkout-junit-reports-nonexistent-branch-test
170208
outputs:
171209
json: ${{ steps.generate.outputs.split-index-json }}
172210
steps:

checkout-junit-reports/action.yml

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,47 @@ 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+
git checkout $SAVED_SHA
70+
UPLOAD_SHA_ARTIFACT=false
71+
else
72+
# no SHA artifact exists, handle branch checkout and potentially create new SHA artifact
73+
if git ls-remote --exit-code --heads origin $CHECKOUT_REF; then
74+
echo "Switching to existing branch: $CHECKOUT_REF"
75+
git fetch --quiet
76+
git switch $CHECKOUT_REF
77+
else
78+
echo "Branch $CHECKOUT_REF does not exist, staying on default branch"
6279
UPLOAD_SHA_ARTIFACT=false
63-
CHECKOUT_REF=$(cat junit-reports-sha.txt)
6480
fi
65-
else
66-
mkdir ${{ inputs.artifact-path }} || true
6781
fi
6882
if [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then
69-
echo "Checking out JUnit reports from branch $CHECKOUT_REF"
83+
echo "Will upload JUnit reports SHA for branch $CHECKOUT_REF"
7084
else
71-
echo "Checking out JUnit reports from previously used SHA $CHECKOUT_REF"
85+
echo "Will not upload JUnit reports SHA"
7286
fi
7387
# we can only upload the artifact once, so only do this on the first split
7488
if [ "${{ inputs.split-index }}" != "0" ]; then
7589
echo "Skipping upload of JUnit reports SHA on this index"
7690
UPLOAD_SHA_ARTIFACT=false
7791
fi
7892
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 }}
8693
8794
- name: Save JUnit reports SHA
8895
if: ${{ steps.junit-reports-sha.outputs.upload-artifact == 'true' }}
@@ -91,6 +98,7 @@ runs:
9198
run: |
9299
SHA=$(git rev-parse HEAD)
93100
echo "Saving JUnit reports SHA $SHA"
101+
mkdir -p ../${{ inputs.artifact-path }}
94102
cd ../${{ inputs.artifact-path }}
95103
echo -n "$SHA" > junit-reports-sha.txt
96104

0 commit comments

Comments
 (0)