Streamline Your Dev Workflow: Branch Merge Automation with GitHub Actions
3 min readJun 20, 2024
In this blog, we’ll walk through a GitHub Actions workflow designed to automate the process of merging all branches into the master branch. This is particularly useful for keeping your master branch up-to-date and ensuring that all changes are consolidated regularly.
Overview
The GitHub Actions workflow described here runs every time there is a push to the master branch. It fetches all branches, attempts to merge each one into the master, and deletes the branch if the merge is successful.
Workflow Configuration
Here’s the complete configuration for our workflow:
on:
push:
branches:
- master
jobs:
merge-branches:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Fetch all branches
run: git fetch --all
- name: Merge branches into master
run: |
BRANCHES=$(git branch -r | grep -v '\->' | grep -v 'master' | sed 's/origin\///')
for branch in $BRANCHES; do
CURRENT_BRANCH=$(git branch…