From b5741e1e57cf9033fd32b5c86b1e8a339db0236d Mon Sep 17 00:00:00 2001 From: aTheo Date: Tue, 19 Nov 2024 12:28:33 +0100 Subject: [PATCH] Update sync workflow to handle specific conflicts (#459) * Update sync workflow to hadle specific conflicts * Externalize KF_TAG variable to dedicated file outside of the makefile * Update makefile logic handling --- .../workflows/sync-branches-through-pr.yaml | 103 ++++++++++++------ components/odh-notebook-controller/Makefile | 4 +- .../odh-notebook-controller/makefile-vars.mk | 1 + 3 files changed, 72 insertions(+), 36 deletions(-) create mode 100644 components/odh-notebook-controller/makefile-vars.mk diff --git a/.github/workflows/sync-branches-through-pr.yaml b/.github/workflows/sync-branches-through-pr.yaml index f412be13f7f..6923981dc88 100644 --- a/.github/workflows/sync-branches-through-pr.yaml +++ b/.github/workflows/sync-branches-through-pr.yaml @@ -1,47 +1,80 @@ --- -name: Sync branches through Pull Request +name: Sync Branches -on: # yamllint disable-line rule:truthy +on: workflow_dispatch: inputs: source: - description: Source branch + description: "From:" required: true + type: string target: - description: Target branch + description: "To:" required: true + type: string jobs: - sync: - permissions: - contents: write - pull-requests: write + sync-branches: runs-on: ubuntu-latest + steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - ref: ${{ github.event.inputs.target }} - fetch-depth: 0 - - - name: Prepare sync branch - id: prepare - run: | - git fetch origin ${{ github.event.inputs.source }} - git reset --hard origin/${{ github.event.inputs.source }} - - TIMESTAMP=$(date +'%Y%m%d%H%M%S') - SYNC_BRANCH=sync__${{ github.event.inputs.source }}__${{ github.event.inputs.target }}__${TIMESTAMP} - echo "branch=$SYNC_BRANCH" >> $GITHUB_OUTPUT - - - name: Create pull request - uses: peter-evans/create-pull-request@5e914681df9dc83aa4e4905692ca88beb2f9e91f # v7.0.5 - with: - branch: ${{ steps.prepare.outputs.branch }} - title: "Sync `${{ github.event.inputs.target }}` branch with `${{ github.event.inputs.source }}` branch" - body: | - :robot: This is an automated Pull Request created by `/.github/workflows/sync-branches-through-pr.yml`. - - It merges all commits from `${{ github.event.inputs.source }}` branch into `${{ github.event.inputs.target }}` branch. - - :warning: **IMPORTANT NOTE**: Remember to delete the `${{ steps.prepare.outputs.branch }}` branch after merging the changes. + - name: Checkout repository + uses: actions/checkout@v4 + 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" + "components/odh-notebook-controller/makefile-vars.mk" + ) + + 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 + + # Commit the merge changes + git commit -m "Merge ${SOURCE_BRANCH} into ${TARGET_BRANCH} with resolved conflicts" || 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" \ + --base "${{ github.event.inputs.target }}" \ + --head "${SYNC_BRANCH}" diff --git a/components/odh-notebook-controller/Makefile b/components/odh-notebook-controller/Makefile index e92d704bf3b..31ef73c671f 100644 --- a/components/odh-notebook-controller/Makefile +++ b/components/odh-notebook-controller/Makefile @@ -1,10 +1,12 @@ +include makefile-vars.mk + # Image URL to use all building/pushing image targets IMG ?= quay.io/opendatahub/odh-notebook-controller TAG ?= $(shell git describe --tags --always) KF_IMG ?= quay.io/opendatahub/kubeflow-notebook-controller -KF_TAG ?= main-363bcdb +KF_TAG ?= $(KF_TAG) CONTAINER_ENGINE ?= podman diff --git a/components/odh-notebook-controller/makefile-vars.mk b/components/odh-notebook-controller/makefile-vars.mk new file mode 100644 index 00000000000..827d0ea7820 --- /dev/null +++ b/components/odh-notebook-controller/makefile-vars.mk @@ -0,0 +1 @@ +KF_TAG ?= main-363bcdb