Skip to content

Commit e08933c

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

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

.github/workflows/integration-test.yml

Lines changed: 31 additions & 0 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

checkout-junit-reports/action.yml

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ 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
@@ -57,32 +62,47 @@ runs:
5762
UPLOAD_SHA_ARTIFACT=${{ inputs.upload-artifact }}
5863
CHECKOUT_REF=${{ inputs.git-branch }}
5964
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
6266
UPLOAD_SHA_ARTIFACT=false
63-
CHECKOUT_REF=$(cat junit-reports-sha.txt)
67+
CHECKOUT_REF=$(cat ${{ inputs.artifact-path }}/junit-reports-sha.txt)
68+
cd ${{ inputs.path }}
69+
echo "Checking out specific SHA: $CHECKOUT_REF"
70+
git fetch --quiet
71+
git checkout $CHECKOUT_REF
72+
else
73+
cd ${{ inputs.path }}
74+
if git ls-remote --exit-code --heads origin $CHECKOUT_REF; then
75+
echo "Switching to existing branch: $CHECKOUT_REF"
76+
git fetch --quiet
77+
git switch $CHECKOUT_REF
78+
else
79+
echo "Branch $CHECKOUT_REF does not exist, staying on default branch"
80+
UPLOAD_SHA_ARTIFACT=false
81+
fi
6482
fi
6583
else
6684
mkdir ${{ inputs.artifact-path }} || true
85+
cd ${{ inputs.path }}
86+
if git ls-remote --exit-code --heads origin $CHECKOUT_REF; then
87+
echo "Switching to existing branch: $CHECKOUT_REF"
88+
git fetch --quiet
89+
git switch $CHECKOUT_REF
90+
else
91+
echo "Branch $CHECKOUT_REF does not exist, staying on default branch"
92+
UPLOAD_SHA_ARTIFACT=false
93+
fi
6794
fi
6895
if [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then
69-
echo "Checking out JUnit reports from branch $CHECKOUT_REF"
96+
echo "Will upload JUnit reports SHA for branch $CHECKOUT_REF"
7097
else
71-
echo "Checking out JUnit reports from previously used SHA $CHECKOUT_REF"
98+
echo "Will not upload JUnit reports SHA"
7299
fi
73100
# we can only upload the artifact once, so only do this on the first split
74101
if [ "${{ inputs.split-index }}" != "0" ]; then
75102
echo "Skipping upload of JUnit reports SHA on this index"
76103
UPLOAD_SHA_ARTIFACT=false
77104
fi
78105
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 }}
86106
87107
- name: Save JUnit reports SHA
88108
if: ${{ steps.junit-reports-sha.outputs.upload-artifact == 'true' }}

0 commit comments

Comments
 (0)