gitgithubmergegithub-actionscontinuous-deployment

Running Github Action to keep a branch up-to-date with main


We have a test environment that runs on the test branch, I would like to run an Github Action that keeps test up to date when pushes are made to main, but I just cant seem to get it. When I run the code through the CLI, it works, but when I run the action, it does not work...

name: Update Test Branch

on:
  push:
    branches: [main]


jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        
      - name: Setup Git User
        run: |
          git config user.name "GitHub Action"
          git config user.email "<EMAIL>"
      

      - name: Update Test Branch
        run: |
          git fetch origin
          git checkout test
          git pull
          git merge --allow-unrelated-histories origin/main
          git push origin test

At first, I got fatal: refusing to merge unrelated histories, so I added the --allow-unrelated-histories tag. Now, I get an error indicating some merge conflict. However, the error does not throw when I run the commands manually.

There have never been any manual commits to the test. It has only and always should depend on main, meaning it should always have a related history. There may be future exceptions to this, but we have yet to reach this.

Please help me understand what I am doing wrong!


Solution

  • Because the GitHub Actions Runner performs a shallow clone for performance reasons, the full history isn't available on the runner when you try to do the merge. This is what causes the error about "unrelated histories".

    To solve this, you'll want to add fetch-depth: 0 to the checkout step:

          - name: Checkout
            uses: actions/checkout@v3
            with:
              fetch-depth: 0
    

    That should remove the need for --allow-unrelated-histories in your commit block:

          - name: Update Test Branch
            run: |
              git checkout main
              git fetch origin
              git checkout test
              git pull
              git merge origin/main
              git push origin test
    

    Finally, you'll need to add the permissions to push back to the repo, the new defaults are that your runner will be configured with read-only permissions. Granting contents: write should suffice here. And it's best to provide the minimum set required from, a security perspective:

    permissions:
        contents: write
    

    Which ends up with the following workflow:

    name: Update Test Branch
    
    on:
      push:
        branches: [main]
        
    permissions: 
      contents: write
    
    jobs:
      sync:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout
            uses: actions/checkout@v3
            with:
              fetch-depth: 0
    
          - name: Setup Git User
            run: |
              git config user.name "GitHub Action"
              git config user.email "<EMAIL>"
    
          - name: Update Test Branch
            run: |
              git checkout main
              git fetch origin
              git checkout test
              git pull
              git merge origin/main
              git push origin test