Skip to content

Commit 0e1e013

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

File tree

2 files changed

+81
-17
lines changed

2 files changed

+81
-17
lines changed

.github/workflows/integration-test.yml

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ jobs:
7171
path: junit-reports-first-index-4
7272
upload-artifact: false
7373

74+
- name: Checkout JUnit reports (non-existent branch)
75+
uses: ./checkout-junit-reports
76+
with:
77+
split-index: 0
78+
git-branch: junit-reports-it-${{ github.sha }}-nonexistent
79+
path: junit-reports-nonexistent-branch
80+
upload-artifact: false
81+
82+
- name: Assert non-existent branch checkout
83+
run: |
84+
echo "Verifying that checkout with non-existent branch succeeded"
85+
if [ ! -d "junit-reports-nonexistent-branch" ]; then
86+
echo "Error: Directory junit-reports-nonexistent-branch was not created!"
87+
exit 1
88+
fi
89+
cd junit-reports-nonexistent-branch
90+
# Should be on the default branch, verify we have the action files
91+
if [ ! -f "action.yml" ]; then
92+
echo "Error: action.yml not found - checkout didn't work properly!"
93+
ls -la
94+
exit 1
95+
fi
96+
# Verify we're not on the non-existent branch
97+
CURRENT_BRANCH=$(git branch --show-current)
98+
if [ "$CURRENT_BRANCH" == "junit-reports-it-${{ github.sha }}-nonexistent" ]; then
99+
echo "Error: Should not be on the non-existent branch!"
100+
exit 1
101+
fi
102+
echo "Successfully verified checkout with non-existent branch stays on default branch"
103+
cd ..
104+
74105
- name: Assert JUnit reports
75106
run: |
76107
for SPLIT_INDEX in {0..3}; do
@@ -108,7 +139,7 @@ jobs:
108139
git-branch: junit-reports-it-${{ github.sha }}-download
109140
path: junit-reports-second
110141

111-
- name: Assert JUnit reports
142+
- name: Assert JUnit reports (updated branch)
112143
working-directory: junit-reports-second
113144
run: |
114145
REPORT_FILE="first.xml"
@@ -117,12 +148,19 @@ jobs:
117148
ls -l
118149
exit 1
119150
fi
151+
SECOND_REPORT_FILE="second.xml"
152+
if [[ ! -f "$SECOND_REPORT_FILE" ]]; then
153+
echo "Error: JUnit report $SECOND_REPORT_FILE not found!"
154+
ls -l
155+
exit 1
156+
fi
120157
FILE_COUNT=$(ls -1 | wc -l)
121-
if [[ "$FILE_COUNT" -ne 1 ]]; then
122-
echo "Error: Expected 1 JUnit reports, but found $FILE_COUNT files!"
158+
if [[ "$FILE_COUNT" -ne 2 ]]; then
159+
echo "Error: Expected 2 JUnit reports, but found $FILE_COUNT files!"
123160
ls -l
124161
exit 1
125162
fi
163+
echo "Successfully verified updated JUnit reports branch contains both files"
126164
127165
- name: Download JUnit reports SHA artifact
128166
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5

checkout-junit-reports/action.yml

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,66 @@ 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
5560
run: |
5661
echo "Checking JUnit reports SHA"
5762
UPLOAD_SHA_ARTIFACT=${{ inputs.upload-artifact }}
5863
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
64+
# Check if we have a SHA artifact AND it's for the same branch we're requesting
65+
if [ -d ${{ inputs.artifact-path }} ] && [ -f ${{ inputs.artifact-path }}/junit-reports-sha.txt ]; then
66+
# We have a SHA artifact, but only use it if we're requesting the same branch
67+
# For non-existent branches, we should not use the cached SHA
68+
SAVED_SHA=$(cat ${{ inputs.artifact-path }}/junit-reports-sha.txt)
69+
cd ${{ inputs.path }}
70+
# Check if the requested branch exists
71+
if git ls-remote --exit-code --heads origin $CHECKOUT_REF; then
72+
echo "Branch $CHECKOUT_REF exists, checking if saved SHA is from this branch"
73+
git fetch --quiet
74+
git switch $CHECKOUT_REF
75+
CURRENT_SHA=$(git rev-parse HEAD)
76+
if [ "$CURRENT_SHA" == "$SAVED_SHA" ]; then
77+
echo "Using saved SHA $SAVED_SHA for branch $CHECKOUT_REF"
78+
UPLOAD_SHA_ARTIFACT=false
79+
else
80+
echo "Saved SHA $SAVED_SHA doesn't match current branch HEAD $CURRENT_SHA, staying on branch"
81+
fi
82+
else
83+
echo "Branch $CHECKOUT_REF does not exist, ignoring saved SHA and staying on default branch"
6284
UPLOAD_SHA_ARTIFACT=false
63-
CHECKOUT_REF=$(cat junit-reports-sha.txt)
6485
fi
6586
else
66-
mkdir ${{ inputs.artifact-path }} || true
87+
# No SHA artifact, handle branch checkout normally
88+
if [ ! -d ${{ inputs.artifact-path }} ]; then
89+
mkdir ${{ inputs.artifact-path }} || true
90+
fi
91+
cd ${{ inputs.path }}
92+
if git ls-remote --exit-code --heads origin $CHECKOUT_REF; then
93+
echo "Switching to existing branch: $CHECKOUT_REF"
94+
git fetch --quiet
95+
git switch $CHECKOUT_REF
96+
else
97+
echo "Branch $CHECKOUT_REF does not exist, staying on default branch"
98+
UPLOAD_SHA_ARTIFACT=false
99+
fi
67100
fi
68101
if [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then
69-
echo "Checking out JUnit reports from branch $CHECKOUT_REF"
102+
echo "Will upload JUnit reports SHA for branch $CHECKOUT_REF"
70103
else
71-
echo "Checking out JUnit reports from previously used SHA $CHECKOUT_REF"
104+
echo "Will not upload JUnit reports SHA"
72105
fi
73106
# we can only upload the artifact once, so only do this on the first split
74107
if [ "${{ inputs.split-index }}" != "0" ]; then
75108
echo "Skipping upload of JUnit reports SHA on this index"
76109
UPLOAD_SHA_ARTIFACT=false
77110
fi
78111
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 }}
86112
87113
- name: Save JUnit reports SHA
88114
if: ${{ steps.junit-reports-sha.outputs.upload-artifact == 'true' }}

0 commit comments

Comments
 (0)