使用钩子在某些条件下自动将一个分支合并到另一个基础中? [英] Automatically merge one branch into another basis on some conditions using hook?

查看:130
本文介绍了使用钩子在某些条件下自动将一个分支合并到另一个基础中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在github存储库中有两个分支-masterdev分支.我有一个需要在以下情况下将master分支合并到dev分支的要求:

I have two branches in my github repository - master and dev branch. I have a requirement where I need to merge master branch into dev branch under below conditions:

  • 一旦PR直接合并到master分支中,那么我需要将master分支自动合并回dev分支.
  • 每当有人将提交直接添加到master分支中时,我都需要将master分支自动合并回dev分支.
  • Once a PR is merged into master branch directly then I need to merge master branch back to dev branch automatically.
  • Whenever someone adds a commit into master branch directly then I need to merge master branch back to dev branch automatically.

这有可能吗?我相信我们可以使用git Hooks来完成这项工作,但是我不确定如何做到这一点.有人可以提供有关如何实现此目标的示例吗?

Is this possible to do by any chance? I believe we can make this work with git Hooks but I am not sure how to do it. Can someone provide an example on how to achieve this?

我在线阅读它,看起来可以在其中包含以下内容使用post-receive钩子,但仅当有人向master分支添加提交或任何PR合并到master分支时,我对如何执行此操作感到困惑?这也是正确的方法吗?

I read it online and looks like I can use post-receive hook with below content in it but I am confuse on how to do this only if someone adds a commit to master branch or any PR gets merged to master branch? Also is this even the right way to do it?

  git checkout master
  git pull

  git checkout dev
  git pull

  git merge master --no-ff --no-edit
  git push

我理解由于合并冲突,并非总是可能的,但是如果可能的话,我们希望它会自动发生.

I appreciate that it may not always be possible, owing to merge conflicts, but if it is possible, we'd like it to happen automatically.

更新

在阅读了有关Github Actions的更多信息后-我在git存储库的根文件夹中创建了一个像.github/workflows/merge-back-to-dev.yml这样的文件,内容如下.这看起来正确吗?我是否需要所有这些字段,例如runs-on?

After reading more about Github Actions - I created a file like this .github/workflows/merge-back-to-dev.yml in my root folder of git repository with below content. Does this look right? Do I need all these fields like runs-on?

  name: 'Nightly Merge'

  on:
    push:
      branches:
        - master

  jobs:
    nightly-merge:

      runs-on: ubuntu-latest

      steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Nightly Merge
        uses: robotology/gh-action-nightly-merge@v1.3.1
        with:
          stable_branch: 'master'
          development_branch: 'dev'
          allow_ff: false

所以现在有了此更改,每当我直接向master分支添加提交或任何PR直接合并到master分支时,master分支都会自动合并到dev分支吗?

So now with this change, whenever I add a commit to master branch directly or any PR gets merged to master branch directly then master branch will be merged to dev branch automatically right?

推荐答案

对于私有GHE(GitHub Enterprise)服务器,仅支持接收钩子,这不是您所需要的(后接收钩子就可以了)

For a private GHE (GitHub Enterprise) server, only pre-receive hooks are supported, which is not what you need (post-receive hooks would be fine)

由于OP也具有GitLab服务器,因此gitab-ci.yml可以完成操作并执行合并.
有点像此要点:

Since the OP also have a GitLab server, a gitab-ci.yml could do the trick and perform the merge.
A bit like this gist:

merge_back:
  stage: merge back master to dev
  only:
      - master
  script:
    - git config --global user.email "$GIT_USER_EMAIL"
    - git config --global user.name "$GIT_USER_NAME"
    - git switch dev
    - git merge $CI_COMMIT_SHA --no-commit --no-ff


原始答案(当我以为是关于github.com的时候)


Original answer (when I thought it was about github.com)

那不是使用钩子,而是 GitHub Actions ,它可以帮助自动化在GitHub上执行的流程端(与在客户端执行的钩子相反)

That would not be using hooks, but GitHub Actions, which helps automate a process executed on GitHub side (as opposed to hooks, which are executed on client side)

您可以使用 on.<push|pull_request>.<branches|tags> 放入您的.github/workflows/myscript.yml中(您可以用更专业的名称替换"myscript",例如merge-back-to-dev.yml)

You can using the on.<push|pull_request>.<branches|tags> into your .github/workflows/myscript.yml (you can replace "myscript" by a more expessive name, like merge-back-to-dev.yml)

您可以使用诸如 nightly-merge 之类的操作,该操作会自动合并马stable分支(mastermain)进入开发之一.除非您有这种情况,否则您将在push事件中显式调用它.

You can use an action like nightly-merge, which automatically merge the stable branch (master or main)into the development one. Except in your case, you would call it explicitly on push event.

这篇关于使用钩子在某些条件下自动将一个分支合并到另一个基础中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆