ci: trigger backport workflow via workflow_dispatch (#11695)

This commit is contained in:
DmitriyLewen authored and GitHub committed 2026-11-23 07:57:58 +00:00
1 parent edf9f5c13d
commit 427c319c4a
2 files changed
+47 -41

No files matched your search

+46 -40
View File
@@ -1,39 +1,55 @@
name: Automatic Backporting
run-name: "Backport #${{ inputs.pr-number }} to ${{ inputs.branch }}"
# Only users with write access to the repository can run a workflow_dispatch workflow,
# so no separate permission check is needed.
on:
issue_comment:
types: [created]
workflow_dispatch:
inputs:
pr-number:
description: Number of the merged PR to backport
required: true
type: number
branch:
description: Target release branch (e.g. release/v0.50)
required: true
type: string
# github.token is only used to read the PR; checkout and backport.sh use the GitHub App token.
permissions:
contents: read
pull-requests: read
jobs:
check_permission:
name: Check comment author permissions
runs-on: ubuntu-2404-2core
outputs:
is_maintainer: ${{ steps.check_permission.outputs.is_maintainer }}
steps:
- name: Check permission
id: check_permission
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PERMISSION=$(gh api /repos/$GITHUB_REPOSITORY/collaborators/$GITHUB_ACTOR/permission --jq '.permission')
if [ "$PERMISSION" == "admin" ] || [ "$PERMISSION" == "write" ]; then
echo "is_maintainer=true" >> $GITHUB_OUTPUT
else
echo "is_maintainer=false" >> $GITHUB_OUTPUT
fi
backport:
name: Backport PR
needs: check_permission # run this job after checking permissions
if: |
needs.check_permission.outputs.is_maintainer == 'true' &&
github.event.issue.pull_request &&
github.event.issue.pull_request.merged_at != null &&
startsWith(github.event.comment.body, '@aqua-bot backport release/')
runs-on: ubuntu-2404-2core
steps:
- name: Validate inputs
env:
GH_TOKEN: ${{ github.token }} # required by gh api
PR_NUMBER: ${{ inputs.pr-number }}
BRANCH_NAME: ${{ inputs.branch }}
run: |
# Always run backport.sh from main, regardless of the ref selected in "Use workflow from".
if [[ "$GITHUB_REF" != "refs/heads/main" ]]; then
echo "Error: the workflow must be run from main, got $GITHUB_REF" >&2
exit 1
fi
if [[ ! "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
echo "Error: invalid PR number: $PR_NUMBER" >&2
exit 1
fi
if [[ "$BRANCH_NAME" == *".."* || ! "$BRANCH_NAME" =~ ^release/[A-Za-z0-9._-]+$ ]]; then
echo "Error: invalid release branch name: $BRANCH_NAME" >&2
exit 1
fi
MERGED=$(gh api "/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.merged and .base.ref == "main"')
if [[ "$MERGED" != "true" ]]; then
echo "Error: PR #$PR_NUMBER is not merged into main" >&2
exit 1
fi
# GITHUB_TOKEN cannot trigger workflows on PRs it creates, so the backport
# PR would not run CI — generate a GitHub App installation token instead.
- name: Generate token
@@ -52,17 +68,6 @@ jobs:
persist-credentials: true # backport.sh runs git push
token: ${{ steps.app-token.outputs.token }}
- name: Extract branch name
env:
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
BRANCH_NAME=$(echo "$COMMENT_BODY" | grep -oE '@aqua-bot backport\s+(\S+)' | awk '{print $3}')
if [[ -z "$BRANCH_NAME" || "$BRANCH_NAME" == *".."* || ! "$BRANCH_NAME" =~ ^[A-Za-z0-9._-]+(/[A-Za-z0-9._-]+)*$ ]]; then
echo "Error: Invalid branch name extracted (unsafe characters detected)." >&2
exit 1
fi
echo "BRANCH_NAME=$BRANCH_NAME" >> "$GITHUB_ENV"
- name: Set up Git user
run: |
git config --global user.email "actions@github.com"
@@ -71,5 +76,6 @@ jobs:
- name: Run backport script
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
run: ./misc/backport/backport.sh "$BRANCH_NAME" "$ISSUE_NUMBER"
PR_NUMBER: ${{ inputs.pr-number }}
BRANCH_NAME: ${{ inputs.branch }}
run: ./misc/backport/backport.sh "$BRANCH_NAME" "$PR_NUMBER"
+1 -1
View File
@@ -23,7 +23,7 @@ For example, if a fix is being distributed for v0.50.0, the patch release would
1. A release branch (e.g., `release/v0.50`) is automatically created when a new minor version is released.
1. Create a pull request (PR) against the main branch with the necessary fixes. If the fixes are already merged into the main branch, skip this step.
1. Once the PR with the fixes is merged, comment `@aqua-bot backport <release-branch>` on the PR (e.g., `@aqua-bot backport release/v0.50`). This will trigger the automated backporting process using GitHub Actions.
1. Once the PR with the fixes is merged, run the [Automatic Backporting](https://github.com/aquasecurity/trivy/actions/workflows/backport.yaml) workflow with the PR number and the release branch from the Actions tab ("Run workflow"). This will trigger the automated backporting process using GitHub Actions.
1. The automated process will create a new PR with the backported changes. Ensure that all tests pass for this PR.
1. Once the tests pass, merge the automatically created PR into the release branch.
1. Merge [a release PR](release-flow.md) on the release branch and release the patch version.