From 1db110c138d8f43a87f2e530fee00312e9c4c980 Mon Sep 17 00:00:00 2001 From: Alex Prudhomme <78121423+alexprudhomme@users.noreply.github.com> Date: Mon, 5 Aug 2024 09:50:22 -0400 Subject: [PATCH] ci: fix error in publish-pr-review-site action (#4242) https://coveord.atlassian.net/browse/KIT-3451 --- .../actions/publish-pr-review-site/action.yml | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/.github/actions/publish-pr-review-site/action.yml b/.github/actions/publish-pr-review-site/action.yml index be4993cbc9f..0cd3f8b7ce0 100644 --- a/.github/actions/publish-pr-review-site/action.yml +++ b/.github/actions/publish-pr-review-site/action.yml @@ -19,12 +19,29 @@ runs: token: ${{inputs.token}} - name: 'Setup branch' run: | - if [[ -z $(git ls-remote --heads origin refs/heads/${{github.event.pull_request.number}} | tr -s '[:blank:]') ]]; then - git switch -c "${{github.event.pull_request.number}}" + BRANCH_NAME="${{ github.event.pull_request.number }}" + + git fetch origin + + # Check if the branch exists on the remote + if git show-ref --verify --quiet "refs/remotes/origin/$BRANCH_NAME"; then + echo "Branch $BRANCH_NAME exists remotely. Checking out..." + + # If the branch exists locally, switch to it; otherwise, create a tracking branch + if git show-ref --verify --quiet "refs/heads/$BRANCH_NAME"; then + git switch "$BRANCH_NAME" + else + git switch --track "origin/$BRANCH_NAME" + fi + + # Reset the branch to match the latest commit from the main branch + git reset --hard origin/main else - git fetch origin "refs/heads/${{github.event.pull_request.number}}" - git switch "${{github.event.pull_request.number}}" - git reset --hard main + echo "Branch $BRANCH_NAME does not exist remotely. Creating a new branch..." + + # Create a new branch locally and push it to the remote + git switch -c "$BRANCH_NAME" + git push -u origin "$BRANCH_NAME" fi working-directory: prs shell: bash