在Git Push后创建Pull Request

5

我有一个 GitHub 仓库,有两个分支:"master" 和 "develop"。

我们的工作流程是将任何代码提交到 "develop" 分支,然后推送到 GitHub,接着应该创建一个 Pull Request 将这些提交合并到 "master" 分支。

我正在尝试编写一个 Action,在开发者将提交推送到 "develop" 分支时自动创建一个 Pull Request,下面是相应的脚本:

name: Create pull request
on:
  push:
    branches:
      - develop
jobs:
  prForMasterBranch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: master
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v2
        with:
          commit-message: update master branch
          title: Update master branch
          branch: develop

我看到"Push"事件已经成功在"develop"分支上执行,但我却看不到任何新的拉取请求!

我检查了该操作的日志,并在创建拉取请求时发现以下行:

将拉取请求分支推送到“origin/develop”
分支'develop'不再与基本分支“master”有差异
关闭拉取请求并删除分支“develop”

看起来我错过了什么,但没能找出原因。

感激您提供的任何帮助。


1
请参见以下链接:https://github.com/peter-evans/create-pull-request/blob/master/docs/examples.md#keep-a-branch-up-to-date-with-another - riQQ
谢谢您的评论,但是如果我删除“with: ref: master”,那么创建拉取请求将如何知道我需要将“develop”分支与哪个分支合并?我的意思是,我认为我们应该在某处提到“master”分支,对吗? - user13602154
你是对的,我误解了它。 - riQQ
2个回答

7
如果您查看创建拉取请求操作的文档,它会提到:
创建拉取请求操作将会: - 检查 Actions 工作区中的仓库更改。包括: - 未跟踪(新)文件 - 跟踪(修改)文件 - 在工作流期间进行但尚未推送的提交 - 将所有更改提交到一个新分支或更新现有的拉取请求分支。 - 创建拉取请求以将新分支合并到基础分支——工作流中检出的分支。
它总是需要一个中介分支来提交更改。
如果您按照下面的方式修改工作流配置,添加“重置主分支”步骤以从远程“develop”分支获取最新更改并重置“master”分支,并为操作指定“branch: temp”,则工作流会创建一个名为“temp”的分支,其中包含您推送到“develop”分支的相同提交,并打开从“temp”分支到“master”分支的PR。在对develop进行后续提交时,它将继续对“temp”分支进行相同的更改,并类似地打开PR或更新现有PR。
name: Create pull request
on:
  push:
    branches:
      - develop
jobs:
  prForMasterBranch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: master
      - name: Reset master branch
        run: |
          git fetch origin develop:develop
          git reset --hard develop
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          commit-message: update master branch
          title: Update master branch
          branch: temp
          delete-branch: true
          assignees: user-you-want-to
          reviewers: user-you-want-to

请注意,temp 分支将包含推送到 develop 分支的确切提交。

完美地工作了,谢谢! - user13602154
1
@user13602154 为什么需要一个临时分支?为什么不能直接从develop分支创建PR到master分支? - Omkar Kulkarni
这是同样的问题。你找到了不需要任何中间分支的解决方法吗? - Anand

2

无中间分支的PR

替换

你想要的团队

或者

你想要指定的用户

如果需要,用你想要分配的团队或用户替换它们,如果不需要,请注释掉它们。

name: Create pull request
on:
  push:
    branches:
      - develop
jobs:
  reatePullRequest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: master
      - name: Get latest changes
        run: |
          git fetch origin develop:develop
          git reset --hard develop
      - name: Create Pull Request
        id: cpr
        uses: peter-evans/create-pull-request@v4
        with:
          commit-message: Update master
          committer: GitHub <noreply@github.com>
          author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
          signoff: false
          branch: develop
          title: 'Updating master'
          labels: |
            update
          reviewers: user-you-want-to
          team-reviewers: |
            team-you-want-to
          draft: false

1
感谢您提供的 git fetch 命令,它们解决了我的 PR 自动化工作流问题。 - VICTOR IWUOHA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接