Skip to content

Commit

Permalink
fixing prevent_merge_when_branch_behind.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
lujzi05 authored and smellycloud committed Dec 3, 2024
1 parent 002f5b7 commit 75a2853
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions .github/workflows/prevent_merge_when_branch_behind.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
name: Require Branch to Be Up-to-Date with Main
name: Require Branch to Be Up-to-Date

# Trigger this workflow on pull request events targeting a specific branch.
on:
pull_request:
branches:
- main
- development

workflow_dispatch: # enables manual triggering

jobs:
check-branch:
runs-on: ubuntu-latest
steps:
- name: Determine Comparison Branch
id: comparison
run: |
if [[ "${{ github.event.pull_request.base.ref }}" == "main" && "${{ github.event.pull_request.head.ref }}" == "development" ]]; then
echo "branch=main" >> $GITHUB_ENV
else
echo "branch=development" >> $GITHUB_ENV
fi
- name: Checkout pull request branch
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- name: Fetch main branch

- name: Fetch comparison branch
run: |
git fetch --unshallow
git fetch origin main
git fetch origin ${{ env.branch }}
- name: Compare branch with main
- name: Compare branch with ${{ env.branch }}
run: |
if git merge-base --is-ancestor origin/main HEAD; then
echo "::notice ::Branch is up-to-date with main."
if git merge-base --is-ancestor origin/${{ env.branch }} HEAD; then
echo "::notice ::Branch is up-to-date with ${{ env.branch }}."
else
echo "::error ::Merge Blocked: Your branch is behind the latest commits on main. Please update your branch with the latest changes from main before attempting to merge."
echo "Merge base: $(git merge-base HEAD origin/main)"
exit 1
fi
echo "::error ::Merge Blocked: Your branch is behind the latest commits on ${{ env.branch }}. Please update your branch before attempting to merge."
exit 1
fi

0 comments on commit 75a2853

Please sign in to comment.