7

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?

AndyP
  • 527
  • 1
  • 14
  • 36
  • Does this answer your question? [Github - merging commits pull requests to several branches](https://stackoverflow.com/questions/58398268/github-merging-commits-pull-requests-to-several-branches) – β.εηοιτ.βε Nov 09 '20 at 21:41
  • In the link also they are recommending to use git hook to do this process. We don't use bitbucket unfortunately. – AndyP Nov 09 '20 at 21:45
  • Well, as you see, this has been asked before [multiple times, actually](https://stackoverflow.com/questions/51021689/whats-the-practice-for-github-cascading-merge), and the answer still seems to be "not possible". – β.εηοιτ.βε Nov 09 '20 at 21:48
  • That I understand but can we not do it using Hook and some sort of script which can trigger the script whenever any commit is added or any PR is merged to master branch? – AndyP Nov 09 '20 at 22:33
  • @AndyP yes, you have the right idea, you only need to test it. – VonC Nov 11 '20 at 04:36
  • @VonC yeah I tested out just now by making `branch-A` as master branch and `branch-B` as dev branch where I made changes on `branch-A` branch by adding a commit but it didn't merged `branch-A` branch to `branch-B` branch at all. I modified my yml file accordingly and I made sure both the branches has this yml file in them. Not sure what wrong I am doing? Any idea? – AndyP Nov 11 '20 at 16:10
  • @AndyP Do you have a link of the execution of your GitHub workflow/action? – VonC Nov 11 '20 at 16:12
  • I am doing this in our internal company github. Also where I can see the execution of our Github workflow/action? – AndyP Nov 11 '20 at 16:13
  • Btw I don't see `Actions` tab next to `Pull Requests` in my git repo. Do I need to enable something then it will show up? – AndyP Nov 11 '20 at 16:23
  • yeah looks like public github has `Actions` tab but my internal github doesn't have it so maybe we aren't running latest github? So now are there any other ways by which we can do this since we dont have `Actions` supported in our company? – AndyP Nov 11 '20 at 16:26
  • @AndyP What is the version of your GitHub Enterprise server? 2.22+ (https://docs.github.com/en/enterprise-server@2.22/admin/github-actions/about-using-githubcom-actions-on-github-enterprise-server) – VonC Nov 11 '20 at 16:40

1 Answers1

3

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Sorry I stepped out for a while. Thanks for your suggestion on using Github Actions as I wasn't aware of it. I read more about it and I updated my question with the details I understood. Can you let me know if I got it right for my use case and if it looks good to you? – AndyP Nov 11 '20 at 01:37