Skip to content

Sync Branches

Sync Branches #41

---
name: Sync Branches
on:
workflow_dispatch:
inputs:
source:
description: "From:"
required: true
type: string
target:
description: "To:"
required: true
type: string
jobs:
sync-branches:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Ensure full history is fetched for merging
- name: Set up Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Merge source branch into target with conflict resolution
id: merge
run: |
set -e
SOURCE_BRANCH="${{ github.event.inputs.source }}"
TARGET_BRANCH="${{ github.event.inputs.target }}"
git fetch origin ${TARGET_BRANCH}:${TARGET_BRANCH}
git checkout ${TARGET_BRANCH}
git fetch origin ${SOURCE_BRANCH}:${SOURCE_BRANCH}
git merge --no-commit origin/${SOURCE_BRANCH} || true
# Resolve conflicts for specific files
FILES=(
"components/notebook-controller/config/overlays/openshift/params.env"
"components/odh-notebook-controller/config/base/params.env"
)
for FILE in "${FILES[@]}"; do
if [[ -f "$FILE" && "$(git status --porcelain=v1 2>/dev/null | grep -c "$FILE")" -gt 0 ]]; then
echo "Resolving conflict for $FILE by keeping target branch version."
git checkout --ours "$FILE"
git add "$FILE"
fi
done
# Handle the Makefile manually
MAKEFILE="components/odh-notebook-controller/Makefile"
if [[ -f "$MAKEFILE" && "$(git status --porcelain=v1 2>/dev/null | grep -c "$MAKEFILE")" -gt 0 ]]; then
echo "Resolving conflicts in $MAKEFILE while preserving KF_TAG."
# Extract the KF_TAG line from the target branch
KF_TAG_TARGET=$(git show :2:"$MAKEFILE" | grep '^KF_TAG \?=')
if [[ -z "$KF_TAG_TARGET" ]]; then
echo "KF_TAG not found in target branch. Exiting with an error."
exit 1
fi
# Process the conflicting Makefile
awk -v kf_tag="$KF_TAG_TARGET" '
BEGIN { conflict = 0 }
/^<<<<<<< / { conflict = 1; next } # Start of conflict
/^=======/ { conflict = 2; next } # Separator in conflict
/^>>>>>>> / { conflict = 0; next } # End of conflict
conflict == 1 && /^KF_TAG \?=/ { next } # Ignore incoming KF_TAG
conflict == 2 && /^KF_TAG \?=/ { print kf_tag; next } # Use target KF_TAG
{ print $0 } # Print other lines
' "$MAKEFILE" > "$MAKEFILE.new"
# Replace the original Makefile with the resolved version
mv "$MAKEFILE.new" "$MAKEFILE"
echo "Preserved KF_TAG as: $KF_TAG_TARGET"
# Stage the resolved Makefile
git add "$MAKEFILE"
fi
# Commit the merge changes
git commit -m "Merge ${SOURCE_BRANCH} into ${TARGET_BRANCH} with resolved conflicts, preserving KF_TAG in Makefile" || echo "Nothing to commit"
# Create a new branch for the sync
TIMESTAMP=$(date +'%Y%m%d%H%M%S')
SYNC_BRANCH="sync__${SOURCE_BRANCH}__${TARGET_BRANCH}__${TIMESTAMP}"
git checkout -b $SYNC_BRANCH
git push origin $SYNC_BRANCH
echo "branch=$SYNC_BRANCH" >> $GITHUB_OUTPUT
- name: Create a Pull Request
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SOURCE_BRANCH="${{ github.event.inputs.source }}"
TARGET_BRANCH="${{ github.event.inputs.target }}"
SYNC_BRANCH=$(echo "${{ steps.merge.outputs.branch }}")
gh pr create \
--title "Sync ${SOURCE_BRANCH} into ${TARGET_BRANCH}" \
--body ":robot: This is an automated PR generated by `/.github/workflows/sync-branches-through-pr.yaml`" \
--base "${{ github.event.inputs.target }}" \
--head "${SYNC_BRANCH}"