Automatically merge one branch into another basis on some conditions using hook?

て烟熏妆下的殇ゞ 提交于 2020-12-06 15:57:27

问题


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:

  • 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.

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?

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.

Update

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

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?


回答1:


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)

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

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

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)

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)

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.



来源:https://stackoverflow.com/questions/64759535/automatically-merge-one-branch-into-another-basis-on-some-conditions-using-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!