如何在 GitHub 工作流程步骤中获得 PR 的差异?

问题描述

我有一个 GitHub 工作流程,应该将单个差异发送到不同的服务。

差异应该是运行工作流的 PR 的差异。

我知道有 actions/checkout@v2 操作但是

       # Checkout pull request HEAD commit instead of merge commit
       # https://github.com/actions/checkout#checkout-pull-request-head-commit-instead-of-merge-commit
       - name: Checkout pull request HEAD
         id: checkout_pr_head
         uses: actions/checkout@v2
         with:
           ref: ${{ github.event.pull_request.head.sha }}
           fetch-depth: 0

现在,在工作流程的下一步中,我尝试打印差异,但它不起作用:差异为空:

      - name: print-diff
        shell: bash
        run: |
          git diff origin/main..HEAD > mydiff
          echo "mydiff = "
          cat mydiff

我做错了什么?

解决方法

您需要获取您所针对的分支,因为 checkout 操作不会获取所有分支:

      - name: print-diff
        shell: bash
        run: |
          git fetch origin main
          git diff origin/main..HEAD > mydiff
          echo "mydiff = "
          cat mydiff