Search code examples
github-actions

How can I update all branches when main/master has a push?


I've tried a few things. This is what I'm using right now.

name: Update Branches

on:
  push:
    branches:
      - main

jobs:
  update-branches:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Get branch names
        id: branches
        run: |
          git ls-remote --heads origin | awk -F'/' '{print $3}' | grep -vE 'main$' > ${{ github.workspace }}/branches.txt

      - name: Update branches
        run: |
          while read -r branch; do
            git checkout $branch
            git merge origin/main --no-edit
            git push origin $branch
          done < ${{ github.workspace }}/branches.txt

I am getting an error if I use this:

error: pathspec 'reddit-fix' did not match any file(s) known to git
Error: Process completed with exit code 1.

reddit-fix is a branch that exists.

I'd like to know if there is any way to achieve this. I've tried using an action I found on the GitHub marketplace this one, but I couldn't make this work.


Solution

  • Azeem answered this. I'm posting this as an answer so that I can mark the question as solved.

    With actions/checkout@v3, use fetch-depth: 0 to fetch all the branches. Also add git config --global user.email "" git config --global user.name "<my_username>".

    This is a repo with the entire answer i.e folder structure and yml file. You can see it working in this repo