@@ -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