From eb93d4178712563c01b6d07d3742be673d5c9053 Mon Sep 17 00:00:00 2001 From: David Sondermann Date: Mon, 29 Sep 2025 14:57:14 +0200 Subject: [PATCH] Add support for checking out non-existent branches in JUnit reports workflow --- .github/workflows/integration-test.yml | 73 +++++++++++++++++++++++++- checkout-junit-reports/action.yml | 51 +++++++++++------- 2 files changed, 104 insertions(+), 20 deletions(-) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index eea1fdb..70e6003 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -12,7 +12,7 @@ env: split-total: 4 jobs: - checkout-junit-reports-test: + checkout-junit-reports: name: Checkout JUnit reports runs-on: ubuntu-latest steps: @@ -75,6 +75,11 @@ jobs: run: | for SPLIT_INDEX in {0..3}; do echo "Checking JUnit reports for split-index $SPLIT_INDEX" + if [[ ! -d "junit-reports-first-index-$SPLIT_INDEX" ]]; then + echo "Error: JUnit report directory not found!" + ls -l + exit 1 + fi cd junit-reports-first-index-$SPLIT_INDEX REPORT_FILE="first.xml" if [[ ! -f "$REPORT_FILE" ]]; then @@ -162,11 +167,75 @@ jobs: name: junit-xml-reports-sha failOnError: false + checkout-junit-reports-with-nonexistent-branch: + name: Checkout JUnit reports with non-existent branch + runs-on: ubuntu-latest + needs: + - checkout-junit-reports + steps: + - name: Checkout split-tests-java-action + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 + + - name: Checkout JUnit reports (split-index 1) + uses: ./checkout-junit-reports + with: + split-index: 1 + git-branch: junit-reports-it-nonexistent + path: junit-reports-first-index-1 + + - name: Checkout JUnit reports (split-index 0a) + uses: ./checkout-junit-reports + with: + split-index: 0 + git-branch: junit-reports-it-nonexistent + path: junit-reports-first-index-0 + + - name: Checkout JUnit reports (split-index 2) + uses: ./checkout-junit-reports + with: + split-index: 2 + git-branch: junit-reports-it-nonexistent + path: junit-reports-first-index-2 + + - name: Checkout JUnit reports (split-index 0b) + uses: ./checkout-junit-reports + with: + split-index: 0 + git-branch: junit-reports-it-nonexistent + path: junit-reports-first-index-3 + + - name: Checkout JUnit reports (split-index 0c) + uses: ./checkout-junit-reports + with: + split-index: 0 + git-branch: junit-reports-it-nonexistent + path: junit-reports-first-index-4 + upload-artifact: false + + - name: Assert JUnit reports + run: | + for SPLIT_INDEX in {0..3}; do + echo "Checking JUnit reports for split-index $SPLIT_INDEX" + if [[ ! -d "junit-reports-first-index-$SPLIT_INDEX" ]]; then + echo "Error: JUnit report directory not found!" + ls -l + exit 1 + fi + cd junit-reports-first-index-$SPLIT_INDEX || exit 1 + FILE_COUNT=$(ls -1 | wc -l) + if [[ "$FILE_COUNT" -ne 0 ]]; then + echo "Error: Expected empty directory, but found $FILE_COUNT files!" + ls -l + exit 1 + fi + cd .. + done + generate-split-index-json: name: Generate split indexes runs-on: ubuntu-latest needs: - checkout-junit-reports-test + - checkout-junit-reports-with-nonexistent-branch outputs: json: ${{ steps.generate.outputs.split-index-json }} steps: diff --git a/checkout-junit-reports/action.yml b/checkout-junit-reports/action.yml index fd2cd4b..77ce3c5 100644 --- a/checkout-junit-reports/action.yml +++ b/checkout-junit-reports/action.yml @@ -49,40 +49,54 @@ runs: name: ${{ inputs.artifact-name }} path: ${{ inputs.artifact-path }} + - name: Checkout repository + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 + with: + path: ${{ inputs.path }} + - name: Check JUnit reports SHA id: junit-reports-sha shell: bash + working-directory: ${{ inputs.path }} run: | echo "Checking JUnit reports SHA" UPLOAD_SHA_ARTIFACT=${{ inputs.upload-artifact }} CHECKOUT_REF=${{ inputs.git-branch }} - if [ -d ${{ inputs.artifact-path }} ]; then - cd ${{ inputs.artifact-path }} - if [ -f junit-reports-sha.txt ]; then + if [ -f "../${{ inputs.artifact-path }}/junit-reports-sha.txt" ]; then + SAVED_SHA=$(cat "../${{ inputs.artifact-path }}/junit-reports-sha.txt") + echo "Checking out saved SHA '$SAVED_SHA' for repeatable test distribution" + git fetch --quiet + if git rev-parse --verify "$SAVED_SHA^{commit}" >/dev/null 2>&1; then + git checkout --quiet "$SAVED_SHA" UPLOAD_SHA_ARTIFACT=false - CHECKOUT_REF=$(cat junit-reports-sha.txt) + else + echo "Error: Saved SHA '$SAVED_SHA' is not a valid commit. Aborting checkout." >&2 + exit 1 fi else - mkdir ${{ inputs.artifact-path }} || true - fi - if [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then - echo "Checking out JUnit reports from branch $CHECKOUT_REF" - else - echo "Checking out JUnit reports from previously used SHA $CHECKOUT_REF" + # no SHA artifact exists, handle branch checkout and potentially create new SHA artifact + if git ls-remote --exit-code --heads origin "$CHECKOUT_REF"; then + echo "Switching to existing branch: $CHECKOUT_REF" + git fetch --quiet + git switch "$CHECKOUT_REF" + else + echo "Branch $CHECKOUT_REF does not exist, no JUnit test reports available" + cd .. + rm -rf "${{ inputs.path }}" + mkdir -p "${{ inputs.path }}" + UPLOAD_SHA_ARTIFACT=false + fi fi # we can only upload the artifact once, so only do this on the first split if [ "${{ inputs.split-index }}" != "0" ]; then echo "Skipping upload of JUnit reports SHA on this index" UPLOAD_SHA_ARTIFACT=false + elif [ "$UPLOAD_SHA_ARTIFACT" == "true" ]; then + echo "Will upload JUnit reports SHA for branch $CHECKOUT_REF" + else + echo "Will not upload JUnit reports SHA" fi echo "upload-artifact=${UPLOAD_SHA_ARTIFACT}" >> "$GITHUB_OUTPUT" - echo "ref=${CHECKOUT_REF}" >> "$GITHUB_OUTPUT" - - - name: Checkout repository - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 - with: - path: ${{ inputs.path }} - ref: ${{ steps.junit-reports-sha.outputs.ref }} - name: Save JUnit reports SHA if: ${{ steps.junit-reports-sha.outputs.upload-artifact == 'true' }} @@ -91,7 +105,8 @@ runs: run: | SHA=$(git rev-parse HEAD) echo "Saving JUnit reports SHA $SHA" - cd ../${{ inputs.artifact-path }} + mkdir -p "../${{ inputs.artifact-path }}" + cd "../${{ inputs.artifact-path }}" echo -n "$SHA" > junit-reports-sha.txt - name: Upload JUnit report SHA