Fetch origin/master with pull_request_target

Since GitHub actions allows triggering workflows from forks there is a new event type “pull_request_target”. How is it now possible to fetch the actual origin master? Before we switch to the PRs from fork workflow (with pull_request_target) we just did this:

- name: Checkout PR
        uses: actions/checkout@v2
- name: Fetch origin/master
        run: |
          git fetch --no-tags --prune --depth=1 origin +refs/heads/master:refs/remotes/origin/master

I see why this doesn’t work anymore. Is there a new solution to this problem?

@bobaaaaa,

According to the git fetch command you set in the workflow, it only fetches the latest commit from the remote master branch history. It will create a local master branch for the remote master branch.

From https://github.com/owner/repos
 * [new branch]      master     -> origin/master

The few points below may be able to help you understand:

  1. When you trigger your workflow runs on ‘pull_request’ (NOT ‘pull_request_target’), the default branch ref (GITHUB_REF or github.ref) of this run is the PR merge branch, and the commit ref (GITHUB_SHA or github.sha) is the last merge commit on the merge branch. The checkout action in the workflow will only fetch the last merge commit by default.
    In this situation, the master branch is not checked out to the local by default. When you use the git fetch command to fetch the master branch from remote, it can work as expected to create the local master branch.

  2. When you trigger the workflow runs on a branch which is not the master branch, suppose push a new commit to the develop branch to trigger the workflow. The default branch ref is develop, and the commit ref is the latest commit that triggered the workflow run.
    This situation is similar to above 1, the git fetch command can work as expected to create the local master branch.

  3. When you trigger your workflow runs on ‘pull_request_target’, the default branch ref is the base branch (target branch) of the PR, and the commit ref is the latest commit on the base branch.

    • If the base branch of the PR is not master branch, the situation is similar to above 1 or 2.

    • If the base branch of the PR is master branch, due to the checkout action has fetched the latest commit from the remote master branch, the local master branch has been created. The subsequent git fetch command will not create local master branch, it just updates the local master branch with the latest commit from the remote master branch.

  4. When you trigger the workflow runs on the master branch, such as push a new commit to the master branch, the default branch ref is master, and the commit ref is the latest commit that triggered the workflow run.
    This situation is similar to the second situation of above 3.

You can use the command “git show-branch -a” to view all the local branches and the corresponding remote-tracking branches.

@bobaaaaa ,

As I explained above, git fetch command you set in the workflow will fetch the latest commit from the remote master branch history. It will create a local master branch for the remote master branch if local master branch does not exist.

If the master has been checked out, the local master branch has exited. So the git fetch command will not create a new local master branch, it just updates the local master branch with the latest commit from the remote master branch.
More details, see git-fetch Documentation.

Is my explanation helpful to you to understand?
If you have any questions about this topic, feel free to tell us.