From 7c9206d446ef04f9cbb6b12cbb6713a4a3dc8806 Mon Sep 17 00:00:00 2001 From: Nadav Ben Ami Date: Thu, 10 Oct 2024 11:58:35 +0300 Subject: [PATCH] fixing checkout by trigger --- .github/workflows/test-CI.yml | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test-CI.yml b/.github/workflows/test-CI.yml index c207ddad6..da2e8b4a4 100644 --- a/.github/workflows/test-CI.yml +++ b/.github/workflows/test-CI.yml @@ -34,16 +34,35 @@ jobs: # Check out the branch from the pull request, whether it's from a fork or not - name: Checkout the pull request's branch run: | - if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then - echo "PR from a fork detected. Checking out the fork's branch." - git remote add fork https://github.com/${{ github.event.pull_request.head.repo.full_name }}.git - git fetch fork ${{ github.event.pull_request.head.ref }} - git checkout -B ci-testing-branch FETCH_HEAD + set -ex + # debug + echo "==== before" + git status + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then + echo "PR from a fork detected. Checking out the fork's branch." + git remote add fork https://github.com/${{ github.event.pull_request.head.repo.full_name }}.git + git fetch fork ${{ github.event.pull_request.head.ref }} + git checkout -B ci-testing-branch FETCH_HEAD + else + echo "PR from the same repository detected. Checking out the branch." + git fetch origin ${{ github.event.pull_request.head.ref }} + git checkout ${{ github.event.pull_request.head.ref }} + fi + elif [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then + # we belive that `actions/checkout` will checkout to the branch that you chose in the `dispatch` tab + # so we do nothing + echo "we're in the right branch. no need to checkout (dispatch)" + elif [[ "${{ github.event_name }}" == 'push' ]]; then + # we belive that `actions/checkout` will checkout to the branch that we push into (i.e. either `dev` or `main`) + echo "we're in the right branch. no need to checkout (push)" else - echo "PR from the same repository detected. Checking out the branch." - git fetch origin ${{ github.event.pull_request.head.ref }} - git checkout ${{ github.event.pull_request.head.ref }} + echo "we're not it push/pull/dispatch. error." + exit 1 fi + # debug + echo "==== after" + git status - name: Set up Python uses: actions/setup-python@v5