-
-
Notifications
You must be signed in to change notification settings - Fork 96
[feature] Add reusable backport workflow #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nemesifier
merged 8 commits into
openwisp:master
from
atif09:issues/501-automate-backporting-fixes
Feb 21, 2026
+285
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c3122f5
[feature] Add reusable backport workflow #501
atif09 c904ac6
[fix] Prevent script injection in backport workflow #501
atif09 c444280
[docs] Add docs for reusable backport workflow #501
atif09 b8b7f70
[fix] Address review comments #501
atif09 546e72a
[fix] Address review comments to tighten the security of the workflow…
atif09 5c82093
[chores] Removing dead code and improving branch name sanitization #501
atif09 8f9fd97
[fix] Correct cherry-pick exit code and docs update #501
atif09 5cb3f7a
[fix] Use null check for merged_at validation #501
atif09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| name: Backport fixes to stable branch | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| - main | ||
| issue_comment: | ||
| types: [created] | ||
|
|
||
| concurrency: | ||
| group: backport-${{ github.workflow }}-${{ github.ref }} | ||
| cancel-in-progress: false | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| backport-on-push: | ||
| if: github.event_name == 'push' | ||
| uses: ./.github/workflows/reusable-backport.yml | ||
| with: | ||
| commit_sha: ${{ github.sha }} | ||
| secrets: | ||
| app_id: ${{ secrets.OPENWISP_BOT_APP_ID }} | ||
| private_key: ${{ secrets.OPENWISP_BOT_PRIVATE_KEY }} | ||
|
|
||
| backport-on-comment: | ||
| if: > | ||
| github.event_name == 'issue_comment' && | ||
| github.event.issue.pull_request && | ||
| github.event.issue.pull_request.merged_at != null && | ||
| github.event.issue.state == 'closed' && | ||
| contains(fromJSON('["MEMBER", "OWNER"]'), github.event.comment.author_association) && | ||
| startsWith(github.event.comment.body, '/backport') | ||
| uses: ./.github/workflows/reusable-backport.yml | ||
| with: | ||
| pr_number: ${{ github.event.issue.number }} | ||
| comment_body: ${{ github.event.comment.body }} | ||
| secrets: | ||
| app_id: ${{ secrets.OPENWISP_BOT_APP_ID }} | ||
| private_key: ${{ secrets.OPENWISP_BOT_PRIVATE_KEY }} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| name: Backport fixes to stable branch | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| commit_sha: | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| pr_number: | ||
| required: false | ||
| type: number | ||
| default: 0 | ||
| comment_body: | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| secrets: | ||
| app_id: | ||
| required: true | ||
| private_key: | ||
| required: true | ||
|
|
||
| jobs: | ||
| parse: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| branches: ${{ steps.extract.outputs.branches }} | ||
| sha: ${{ steps.extract.outputs.sha }} | ||
| steps: | ||
| - name: Extract backport targets | ||
| id: extract | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| COMMIT_SHA: ${{ inputs.commit_sha }} | ||
| PR_NUMBER: ${{ inputs.pr_number }} | ||
| COMMENT_BODY: ${{ inputs.comment_body }} | ||
| REPO: ${{ github.repository }} | ||
| run: | | ||
| if [ -n "$COMMIT_SHA" ]; then | ||
| SHA="$COMMIT_SHA" | ||
| COMMIT_MSG=$(gh api "repos/$REPO/git/commits/$SHA" --jq '.message') | ||
| BRANCHES=$(echo "$COMMIT_MSG" | sed -n 's/.*\[backport[:[:space:]]\+\([^]]*\)\].*/\1/p' | tr '\n' ',' | sed 's/,$//') | ||
atif09 marked this conversation as resolved.
Show resolved
Hide resolved
atif09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else | ||
| PR_DATA=$(gh pr view "$PR_NUMBER" --repo "$REPO" --json state,mergeCommit) | ||
| PR_STATE=$(echo "$PR_DATA" | jq -r '.state') | ||
| if [ "$PR_STATE" != "MERGED" ]; then | ||
| echo "PR #$PR_NUMBER is not merged, skipping" | ||
| echo "branches=[]" >> $GITHUB_OUTPUT | ||
| echo "sha=" >> $GITHUB_OUTPUT | ||
| exit 0 | ||
| fi | ||
| SHA=$(echo "$PR_DATA" | jq -r '.mergeCommit.oid') | ||
| BRANCHES=$(echo "$COMMENT_BODY" | sed -n 's|.*/backport[[:space:]]\+\([^[:space:]]*\).*|\1|p' | tr '\n' ',' | sed 's/,$//') | ||
| fi | ||
|
|
||
| if [ -z "$BRANCHES" ]; then | ||
| echo "branches=[]" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "branches=$(echo "$BRANCHES" | tr ',' '\n' | jq -R . | jq -sc .)" >> $GITHUB_OUTPUT | ||
| fi | ||
| echo "sha=$SHA" >> $GITHUB_OUTPUT | ||
|
|
||
| backport: | ||
| needs: parse | ||
| if: needs.parse.outputs.branches != '' && needs.parse.outputs.branches != '[]' && needs.parse.outputs.sha != '' | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| branch: ${{ fromJSON(needs.parse.outputs.branches) }} | ||
| steps: | ||
| - name: Generate GitHub App Token | ||
| id: generate-token | ||
| uses: actions/create-github-app-token@v1 | ||
| with: | ||
| app-id: ${{ secrets.app_id }} | ||
| private-key: ${{ secrets.private_key }} | ||
|
|
||
| - uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| token: ${{ steps.generate-token.outputs.token }} | ||
|
|
||
| - name: Configure Git | ||
| run: | | ||
| git config user.name 'OpenWISP Companion' | ||
| git config user.email 'support@openwisp.io' | ||
|
|
||
| - name: Cherry-pick and create PR | ||
| env: | ||
| GH_TOKEN: ${{ steps.generate-token.outputs.token }} | ||
| SHA: ${{ needs.parse.outputs.sha }} | ||
| BRANCH: ${{ matrix.branch }} | ||
| REPO: ${{ github.repository }} | ||
| run: | | ||
| SHA="${{ needs.parse.outputs.sha }}" | ||
| if [ -z "$SHA" ]; then | ||
| echo "No SHA to cherry-pick, skipping" | ||
| exit 0 | ||
| fi | ||
| if ! git check-ref-format --branch "$BRANCH" > /dev/null 2>&1; then | ||
| echo "::error::Invalid branch name '$BRANCH'" | ||
| exit 1 | ||
| fi | ||
| if ! git ls-remote --exit-code --heads origin "$BRANCH" >/dev/null 2>&1; then | ||
| echo "::error::Target branch '$BRANCH' does not exist" | ||
| exit 1 | ||
| fi | ||
| if ! PR_DATA=$(gh api "repos/$REPO/commits/$SHA/pulls" --jq '.[0] | {number, title}'); then | ||
| echo "::warning::Could not fetch PR data for $SHA, proceeding with commit-based backport branch" | ||
| PR_DATA="null" | ||
| fi | ||
| PR_NUMBER=$(echo "$PR_DATA" | jq -r '.number // empty') | ||
| PR_TITLE=$(echo "$PR_DATA" | jq -r '.title // "Fix from master"') | ||
| if [ -z "$PR_NUMBER" ]; then | ||
| BACKPORT_BRANCH="backport/commit-${SHA:0:7}-to-${BRANCH}" | ||
| BODY="Backport of commit $SHA to \`$BRANCH\`." | ||
| PR_TITLE="Backport commit ${SHA:0:7} to $BRANCH" | ||
| else | ||
| BACKPORT_BRANCH="backport/${PR_NUMBER}-to-${BRANCH}" | ||
|
|
||
| BODY="Backport of #$PR_NUMBER to \`$BRANCH\`." | ||
| EXISTING=$(gh pr list --base "$BRANCH" --state open --json number,headRefName \ | ||
| --jq ".[] | select(.headRefName | startswith(\"backport/${PR_NUMBER}-to-${BRANCH}-\")) | .number" | head -1) | ||
| if [ -n "$EXISTING" ]; then | ||
| echo "Backport PR #$EXISTING already exists" | ||
| gh pr comment "$PR_NUMBER" --body "Backport to \`$BRANCH\` already exists: #$EXISTING" | ||
| exit 0 | ||
| fi | ||
| fi | ||
| BACKPORT_BRANCH="${BACKPORT_BRANCH}-$(date +%s)" | ||
atif09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| git checkout -b "$BACKPORT_BRANCH" "origin/$BRANCH" | ||
| CHERRY_PICK_STATUS=0 | ||
| git cherry-pick -x "$SHA" || CHERRY_PICK_STATUS=$? | ||
| if [ $CHERRY_PICK_STATUS -eq 0 ]; then | ||
| if ! git push origin "$BACKPORT_BRANCH"; then | ||
| echo "::error::Failed to push $BACKPORT_BRANCH" | ||
| exit 1 | ||
| fi | ||
| if ! gh pr create \ | ||
| --base "$BRANCH" \ | ||
| --head "$BACKPORT_BRANCH" \ | ||
| --title "[backport] #${PR_NUMBER}: $PR_TITLE (to $BRANCH)" \ | ||
| --body "$BODY"; then | ||
| echo "::error::Failed to create PR" | ||
| git push origin --delete "$BACKPORT_BRANCH" || true | ||
| exit 1 | ||
| fi | ||
| echo "## ✅ Backport Successful" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Source**: #$PR_NUMBER" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Target Branch**: $BRANCH" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Backport Branch**: $BACKPORT_BRANCH" >> $GITHUB_STEP_SUMMARY | ||
| elif git diff --cached --quiet; then | ||
| echo "Commit $SHA appears to already be in $BRANCH (empty cherry-pick)" | ||
| git cherry-pick --abort || true | ||
| exit 0 | ||
| else | ||
| git cherry-pick --abort || true | ||
| { | ||
| echo "❌ Cherry-pick to \`$BRANCH\` failed due to conflicts. Please backport manually:" | ||
| echo "" | ||
| echo "\`\`\`bash" | ||
| echo "git fetch origin $BRANCH" | ||
| echo "git checkout -b $BACKPORT_BRANCH origin/$BRANCH" | ||
| echo "git cherry-pick -x $SHA" | ||
| echo "# resolve conflicts" | ||
| echo "git cherry-pick --continue" | ||
| echo "git push origin $BACKPORT_BRANCH" | ||
| echo "\`\`\`" | ||
| } > /tmp/backport-comment.md | ||
| [ -n "$PR_NUMBER" ] && gh pr comment "$PR_NUMBER" --body-file /tmp/backport-comment.md | ||
| echo "## ❌ Backport Failed" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Source**: #$PR_NUMBER" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Target Branch**: $BRANCH" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Error**: Cherry-pick failed due to conflicts" >> $GITHUB_STEP_SUMMARY | ||
| exit 1 | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.