Raul Naupari
raulnq

raulnq

Automatic cascading branch merging in GitHub

Automatic cascading branch merging in GitHub

Raul Naupari's photo
Raul Naupari
·Nov 7, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Features
  • Usage
  • Scenarios

If we are using Gitflow in our projects, once the release is ready to be shipped, sooner or later, it will be merged with the develop branch. The task is simple and could be handled manually, but what if we have something a bit more complex, like two or more active release branches? A change in a previous release has to be cascaded to the next one until it reaches the develop branch. And to all this, add a merge conflict during the process. Gitflow automerge is a GitHub Actions to help us to automate those cases.

Features

  • Merge a release branch to the next one until reaching the develop branch. The branches need to follow the Semantic Versioning guidelines.
  • Open a pull request if there is a conflict during the merging.

Usage

name: automerge
on:
  push:
    branches:
      - 'release/**'

  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2        
      - name: AutoMerge
        uses: raulnq/git-flow-automerge@v0.4.1
        with:
          github_token: ${{ secrets.AUTOMERGE_TOKEN }}
          release_branch_type: 'release'
          develop_branch: 'develop'
  • release_branch_type: Release branch prefix, default value: release
  • develop_branch: Develop branch name, default value: develop
  • github_token: We can use the ${{ secrets.GITHUB_TOKEN }}, but there is a limitation explained here about creating recursive workflow runs. Instead, we can create a Personal Access Token (PAT) in an account with write access to the repository and then add a secret in our repository with that value.

Scenarios

To test the action, we created a repository with a secret on it (from the PAT):

image.png

git clone https://github.com/raulnq/github-actions-sandbox.git
cd github-actions-sandbox

Add a file .github\workflows\automerge.yml with the following content:

name: automerge
on:
  push:
    branches:
      - 'release/**'
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2        
      - name: AutoMerge
        uses: raulnq/git-flow-automerge@v0.4.1
        with:
          github_token: ${{ secrets.AUTOMERGE_TOKEN }}
          release_branch_type: 'release'
          develop_branch: 'develop'

Commit and push the changes:

git add .
git commit -m 'adding workflow'
git push origin main

Automerge from a release to the develop branch

Create and push the develop branch:

git checkout -b develop
git push origin develop

Create and push the release/1.0.0 branch:

git checkout -b release/1.0.0 develop
git push origin release/1.0.0

Create, make a commit and push the feature/myFeatureA branch:

git checkout -b feature/myFeatureA 
git commit -m 'featurea A: change 1' --allow-empty
git push origin feature/myFeatureA

Create a pull request from feature/myFeatureA to release/1.0.0:

first-pr.PNG

Merge the pull request and go to the Actions tab:

first-automerge.PNG

Pull request created because merge conflict

Switch to develop and pull the last changes:

git checkout develop
git pull origin develop

Create a file and push the changes:

"Hi" >> Hi.txt
git add .
git commit -m 'new file' 
git push origin develop

Switch to feature/myFeatureA and pull the last changes:

git checkout feature/myFeatureA 
git pull origin feature/myFeatureA

Create a file and push the changes:

"Hello" >> Hi.txt
git add .
git commit -m 'new file' 
git push origin feature/myFeatureA

Create a pull request from feature/myFeatureA to release/1.0.0, merge it, and go to the Actions tab, now a pull request was created:

second-merge.PNG

Fix the merge conflict in the pull request before continuing.

Cascading automerge until reaching develop branch

Switch to develop and pull the last changes:

git checkout  develop
git pull origin develop

Create and push the multiple release branches:

git checkout -b release/1.0.1 develop
git push origin release/1.0.1
git checkout -b release/1.1.0 develop
git push origin release/1.1.0
git checkout -b release/2.0.0 develop
git push origin release/2.0.0

Create, make a commit and push the feature/myFeatureA branch:

git checkout feature/myFeatureA 
git commit -m 'featurea A: change 2' --allow-empty
git push origin feature/myFeatureA

Create a pull request from feature/myFeatureA to release/1.0.0, merge it and go to the Actions tab:

cascade.PNG

Thanks, and happy coding.

 
Share this