0
votes

I have a workflow where i auto merge prod branch into master branch on push in prod branch using Github Actions

Yml file for github action

name: Merge Prod -> master

on:
  push:
    branches: [ prod ]
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Merge prod->master ff_only
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config user.name "test"
          git config user.email "[email protected]"
          git fetch origin master
          git fetch origin prod
          git checkout master
          git pull --rebase
          git merge prod --ff-only --allow-unrelated-histories
          git push origin master

Logs:

Run git config user.name "test"
  git config user.name "test"
  git config user.email "[email protected]"
  git fetch origin master
  git fetch origin prod
  git checkout master
  git pull --rebase
  git merge prod --ff-only --allow-unrelated-histories
  git push origin master
  shell: /usr/bin/bash -e {0}
  env:
    GITHUB_TOKEN: ***
From https://github.com/luvk1412/demo
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
From https://github.com/luvk1412/demo
 * branch            prod       -> FETCH_HEAD
Switched to a new branch 'master'
Branch 'master' set up to track remote branch 'master' from 'origin'.
From https://github.com/luvk1412/demo
Already up to date.
 * [new branch]      f2          -> origin/f2
 * [new branch]      f3          -> origin/f3
 * [new branch]      test_branch -> origin/test_branch
fatal: Not possible to fast-forward, aborting.
Error: Process completed with exit code 128.

Example commit : https://github.com/luvk1412/demo/commit/790d824111d0c0c5b28157b9108e7b97b6b6cdc8

Commit generated through PR : https://github.com/luvk1412/demo/pull/38

Test branch : https://github.com/luvk1412/demo

After PR merge into prod, my prod branch fully contains master branch and one extra commit(due to PR), hence fast forward condition is met and fast forward should work, I am not sure what i am missing here. the fast forward works on my local(if i don't automate this and do manually through local)

2
Side note: --ff-only and --allow-unrelated-histories make no sense in combination. If you want a fast-forward merge, leave out --allow-unrelated-histories. The extra option isn't harmful, it's just nonsense. - torek
@torek actually when i was writing this, i had faced an error of unrelated histories and at that time i was not using --ff-only and just didnt remove it after adding --ff-only, i understand what you are saying - Luv

2 Answers

4
votes

Although I have never used these, I suspect that this is the culprit:

Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags. Refer here to learn which commit $GITHUB_SHA points to for different events.

(boldface mine). With the single commit, Git cannot tell whether the merge would be a fast-forward; with the single branch, Git does not have the necessary branch names. The extra fetches you run will fix some of these problems, but not enough of them. Setting the fetch depth, as recommended right up front here, would thus fix the problem.

0
votes

Script using checkout@V1 which works

name: Auto Merge prod->master
on:
  push:
    branches: [ prod ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v1

    - name: Merge prod->master
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/<github_user_name>/<repo_name>
        git config user.name "test"
        git config user.email "[email protected]"
        git fetch origin master
        git checkout -b master origin/master
        git merge origin/prod --ff-only
        git push origin master