Skip to content

Apache Lucene open-source search software

feat: add automated backport workflow (#14738)

Abdeslam Yassine Agmar5a2119293859
diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml
index d0fecbd22fe..9f376396824 100644
--- a/.github/actionlint.yaml
+++ b/.github/actionlint.yaml
@@ -5,7 +5,8 @@ self-hosted-runner:
# Configuration variables in array of strings defined in your repository or
# organization. `null` means disabling configuration variables check.
# Empty array means no configuration variable is allowed.
-config-variables: []
+config-variables:
+ - BACKPORT_DRY_RUN
# Configuration for file paths. The keys are glob patterns to match to file
# paths relative to the repository root. The values are the configurations for
diff --git a/.github/workflows/backport.sh b/.github/workflows/backport.sh
new file mode 100755
index 00000000000..a151eac5bfb
--- /dev/null
+++ b/.github/workflows/backport.sh
@@ -0,0 +1,228 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+echo "πŸ“Š Fetching PR data..."
+PR_DATA=$(gh pr view "$PR_NUMBER" --json labels,milestone,title)
+PR_LABELS=$(echo "$PR_DATA" | jq -r '.labels[].name | select(test("^backport/"; "i"))')
+PR_MILESTONE=$(echo "$PR_DATA" | jq -r '.milestone.title // ""')
+PR_TITLE=$(echo "$PR_DATA" | jq -r '.title // "Unknown title"')
+PR_MERGE_COMMIT_SHA="${PR_MERGE_COMMIT_SHA:-unknown}"
+DRY_RUN="${BACKPORT_DRY_RUN:-true}"
+
+normalize_version() {
+ local raw="$1"
+ local trimmed
+ local normalized=""
+ trimmed=$(echo "$raw" | sed -E 's/^[[:space:]]+|[[:space:]]+$//g')
+ if [[ $trimmed =~ ([0-9]+(\.[0-9]+)*) ]]; then
+ normalized="${BASH_REMATCH[1]}"
+ fi
+ echo "$normalized"
+}
+
+is_valid_sha() {
+ local sha="$1"
+ [[ "$sha" =~ ^[0-9a-f]{7,40}$ ]]
+}
+
+create_comment() {
+ if [ "$DRY_RUN" = "true" ]; then
+ echo "[dry-run] Would comment on PR #$PR_NUMBER:"
+ echo "$1"
+ return 0
+ fi
+ gh pr comment "$PR_NUMBER" --body "$1"
+}
+
+add_label() {
+ if [ "$DRY_RUN" = "true" ]; then
+ echo "[dry-run] Would add label '$1' on PR #$PR_NUMBER"
+ return 0
+ fi
+ gh pr edit "$PR_NUMBER" --add-label "$1" 2>/dev/null || true
+}
+
+publish_outputs() {
+ local has_targets="$1"
+ local targets_json="$2"
+
+ if [ -n "${GITHUB_OUTPUT:-}" ]; then
+ {
+ echo "dry_run=$DRY_RUN"
+ echo "has_targets=$has_targets"
+ echo "targets<<EOF"
+ echo "$targets_json"
+ echo "EOF"
+ } >> "$GITHUB_OUTPUT"
+ fi
+}
+
+if [ -z "$PR_LABELS" ] && [ -z "$PR_MILESTONE" ]; then
+ echo "ℹ️ No backport labels or milestone found. Nothing to do."
+ publish_outputs "false" "[]"
+ exit 0
+fi
+
+if [ "$DRY_RUN" != "true" ] && ! is_valid_sha "$PR_MERGE_COMMIT_SHA"; then
+ create_comment "❌ **Automatic backports skipped**
+
+Invalid merge commit SHA was provided by workflow context: \`$PR_MERGE_COMMIT_SHA\`.
+
+Backports are skipped for safety."
+ add_label "backport-failed"
+ publish_outputs "false" "[]"
+ exit 0
+fi
+
+echo "🏷️ Ensuring backport labels exist..."
+if [ "$DRY_RUN" = "true" ]; then
+ echo "[dry-run] Skipping label creation"
+else
+ gh label create "backport" --description "Automated backport workflow" --color "0366d6" 2>/dev/null || true
+ gh label create "backport-failed" --description "Backport failed" --color "d73a49" 2>/dev/null || true
+fi
+
+echo "🌿 Caching branch information..."
+ALL_BRANCHES=$(git for-each-ref --format='%(refname:strip=3)' refs/remotes/origin | grep -v '^HEAD$' | sort)
+declare -ra TARGET_BRANCH_TEMPLATES=(
+ "branch_{{major}}x"
+ "branch_{{major}}_{{minor}}"
+)
+
+find_target_branch() {
+ local version="$1"
+ local major
+ local minor=""
+
+ major=$(echo "$version" | sed -E 's/^([0-9]+).*/\1/')
+ if [[ "$version" =~ ^[0-9]+\.([0-9]+) ]]; then
+ minor="${BASH_REMATCH[1]}"
+ fi
+
+ for template in "${TARGET_BRANCH_TEMPLATES[@]}"; do
+ local candidate="${template//\{\{version\}\}/$version}"
+ candidate="${candidate//\{\{major\}\}/$major}"
+ candidate="${candidate//\{\{minor\}\}/$minor}"
+
+ local target
+ target=$(echo "$ALL_BRANCHES" | grep -Fx "$candidate" | head -1 || true)
+ if [ -n "$target" ]; then
+ echo "$target"
+ return 0
+ fi
+ done
+}
+
+declare -a requested_versions=()
+if [ -n "$PR_MILESTONE" ]; then
+ normalized_milestone=$(normalize_version "$PR_MILESTONE")
+ if [ -n "$normalized_milestone" ]; then
+ requested_versions+=("$normalized_milestone")
+ fi
+fi
+
+while IFS= read -r label; do
+ [ -z "$label" ] && continue
+ normalized_label=$(normalize_version "${label#*/}")
+ if [ -n "$normalized_label" ]; then
+ requested_versions+=("$normalized_label")
+ fi
+done <<< "$PR_LABELS"
+
+declare -a deduped_requested_versions=()
+declare -A seen_versions=()
+for version in "${requested_versions[@]}"; do
+ [ -n "$version" ] || continue
+ if [ -z "${seen_versions[$version]:-}" ]; then
+ seen_versions[$version]=1
+ deduped_requested_versions+=("$version")
+ fi
+done
+
+echo "πŸ“‹ Requested versions: ${deduped_requested_versions[*]}"
+
+declare -a valid_backports=()
+declare -a failed_versions=()
+
+echo "πŸ” Pre-validating target branches..."
+for version in "${deduped_requested_versions[@]}"; do
+ target=$(find_target_branch "$version")
+
+ if [ -n "$target" ]; then
+ valid_backports+=("$version:$target")
+ echo "βœ… $version -> $target"
+ else
+ failed_versions+=("$version")
+ echo "❌ $version -> NOT FOUND"
+ fi
+done
+
+if [ ${#failed_versions[@]} -gt 0 ]; then
+ available_sample=$(echo "$ALL_BRANCHES" | head -5 | tr '\n' ', ' | sed 's/,$//')
+ failed_list=""
+ attempted_patterns=""
+ for version in "${failed_versions[@]}"; do
+ failed_list="${failed_list}- \`${version}\`"$'\n'
+ major=$(echo "$version" | sed -E 's/^([0-9]+).*/\1/')
+ if [[ "$version" =~ ^[0-9]+\.([0-9]+) ]]; then
+ minor="${BASH_REMATCH[1]}"
+ attempted_patterns="${attempted_patterns}- \`branch_${major}x\`, \`branch_${major}_${minor}\`"$'\n'
+ else
+ attempted_patterns="${attempted_patterns}- \`branch_${major}x\`"$'\n'
+ fi
+ done
+
+ create_comment "❌ **Backport failed for some versions - Missing target branches**
+
+Could not find target branches for:
+$failed_list
+
+**Sample available branches:** $available_sample
+
+**Expected patterns:**
+- \`branch_{major}x\` (Lucene style)
+- \`branch_{major}_{minor}\` (Lucene release branch style)
+
+**Attempted branch names per missing version:**
+$attempted_patterns
+
+Please create the missing branches or update the backport labels/milestone.
+
+**Note:** Valid backports will still be processed."
+ add_label "backport-failed"
+fi
+
+if [ ${#valid_backports[@]} -eq 0 ]; then
+ echo "❌ No valid backports found."
+ publish_outputs "false" "[]"
+ exit 0
+fi
+
+targets_json=$(printf '%s\n' "${valid_backports[@]}" \
+ | jq -R 'select(length > 0) | split(":") | {version: .[0], target: .[1]}' \
+ | jq -cs '.')
+
+echo "βœ… Prepared ${#valid_backports[@]} valid backport target(s)."
+ if [ "$DRY_RUN" = "true" ]; then
+ echo "[dry-run] Planned targets: $targets_json"
+ for backport in "${valid_backports[@]}"; do
+ IFS=':' read -r version target <<< "$backport"
+ backport_branch="backport-${PR_NUMBER}-to-${target}"
+ echo "[dry-run] -----------------------------------------------------------------"
+ echo "[dry-run] target_branch=${target}, version=${version}, merge_commit=${PR_MERGE_COMMIT_SHA}"
+ echo "[dry-run] Would run equivalent git operations:"
+ echo "[dry-run] git checkout -b ${backport_branch} origin/${target}"
+ echo "[dry-run] git cherry-pick -x ${PR_MERGE_COMMIT_SHA}"
+ echo "[dry-run] git push origin ${backport_branch}"
+ echo "[dry-run] Would run equivalent PR creation:"
+ echo "[dry-run] gh pr create --base ${target} --head ${backport_branch} --title \"[Backport ${target}] ${PR_TITLE}\" --label backport"
+ echo "[dry-run] PR body payload:"
+ echo "[dry-run] ## πŸ”„ Automatic Backport"
+ echo "[dry-run] Backport of #${PR_NUMBER} to \`${target}\`."
+ echo "[dry-run] **Target:** \`${target}\`"
+ echo "[dry-run] **Version:** \`${version}\`"
+ done
+fi
+
+publish_outputs "true" "$targets_json"
diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml
new file mode 100644
index 00000000000..8e71072ccfc
--- /dev/null
+++ b/.github/workflows/backport.yml
@@ -0,0 +1,106 @@
+name: Backport PR
+
+on:
+ pull_request:
+ types: [closed]
+
+permissions: {}
+
+jobs:
+ prepare:
+ name: Prepare Backport Targets
+ if: github.event.pull_request.merged == true
+ timeout-minutes: 15
+ runs-on: ubuntu-latest
+ permissions:
+ contents: read # Reads repository workflow script and remote branch refs.
+ pull-requests: write # Adds comments/labels to the source pull request.
+ outputs:
+ dry_run: ${{ steps.plan.outputs.dry_run }}
+ has_targets: ${{ steps.plan.outputs.has_targets }}
+ targets: ${{ steps.plan.outputs.targets }}
+
+ steps:
+ - name: Checkout
+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ fetch-depth: 0
+ token: ${{ secrets.GITHUB_TOKEN }}
+ persist-credentials: false
+
+ - name: Plan backports
+ id: plan
+ env:
+ GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ PR_NUMBER: ${{ github.event.pull_request.number }}
+ PR_MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }}
+ BACKPORT_DRY_RUN: ${{ vars.BACKPORT_DRY_RUN || 'true' }}
+ run: ./.github/workflows/backport.sh
+
+ backport:
+ name: Create Backport Pull Requests
+ needs: prepare
+ if: needs.prepare.outputs.has_targets == 'true' && needs.prepare.outputs.dry_run != 'true'
+ timeout-minutes: 30
+ runs-on: ubuntu-latest
+ permissions:
+ contents: write # Pushes generated backport branches.
+ pull-requests: write # Creates backport pull requests and comments on failures.
+ strategy:
+ fail-fast: false
+ matrix:
+ include: ${{ fromJSON(needs.prepare.outputs.targets) }}
+
+ steps:
+ - name: Checkout
+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ fetch-depth: 0
+ token: ${{ secrets.GITHUB_TOKEN }}
+ persist-credentials: false
+
+ - name: Cherry-pick with approved action
+ id: cherry_pick
+ continue-on-error: true
+ uses: carloscastrojumo/github-cherry-pick-action@503773289f4a459069c832dc628826685b75b4b3 # v1.0.10
+ with:
+ token: ${{ secrets.GITHUB_TOKEN }}
+ branch: ${{ matrix.target }}
+ cherry-pick-branch: backport-${{ github.event.pull_request.number }}-to-${{ matrix.target }}
+ title: '[Backport ${{ matrix.target }}] {old_title}'
+ body: |
+ ## πŸ”„ Automatic Backport
+
+ Backport of #{old_pull_request_id} to `${{ matrix.target }}`.
+
+ **Target:** `${{ matrix.target }}`
+ **Version:** `${{ matrix.version }}`
+ labels: |
+ backport
+ inherit_labels: 'false'
+
+ - name: Handle failed action backport
+ if: steps.cherry_pick.outcome != 'success'
+ env:
+ GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ PR_NUMBER: ${{ github.event.pull_request.number }}
+ TARGET_BRANCH: ${{ matrix.target }}
+ run: |
+ body="❌ **Backport to \`${TARGET_BRANCH}\` failed**"$'\n\n'"Automatic cherry-pick action failed for this target. Please inspect workflow logs and backport manually if needed."
+ gh pr comment "$PR_NUMBER" --body "$body"
+ gh pr edit "$PR_NUMBER" --add-label "backport-failed" 2>/dev/null || true
+
+ dry-run-report:
+ name: Report Dry-Run Plan
+ needs: prepare
+ if: needs.prepare.outputs.has_targets == 'true' && needs.prepare.outputs.dry_run == 'true'
+ timeout-minutes: 5
+ runs-on: ubuntu-latest
+ permissions: {}
+ steps:
+ - name: Report dry-run targets
+ env:
+ PLANNED_TARGETS: ${{ needs.prepare.outputs.targets }}
+ run: |
+ echo "Backport dry-run mode is enabled."
+ printf 'Planned targets: %s\n' "$PLANNED_TARGETS"