Skip to content
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

Create centralised backport workflow #17

Merged
merged 14 commits into from
Nov 27, 2024
87 changes: 87 additions & 0 deletions .github/actions/backport/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Backport

# A composite action that allows Backporting via GitHub Actions (e.g. to be driven based on labels etc)

inputs:
GITHUB_TOKEN:
required: true
TARGET_BRANCH:
JackPGreen marked this conversation as resolved.
Show resolved Hide resolved
description: The branch in the repository to backport changes into
required: true
REF_TO_BACKPORT:
description: The reference of the commit to be backported
required: true
BACKPORT_OPTIONS:
description: Additional options to pass through to the tool
required: false

env:
# Not possible to set this as a default
# https://github.com/orgs/community/discussions/46670
shell: bash

runs:
using: composite
steps:
- name: Assert branch exists
id: assert-branch-exists
shell: ${{ env.shell }}
run: |
if git ls-remote --exit-code --heads origin "${{ inputs.TARGET_BRANCH }}"; then
echo "::debug::Branch ${{ inputs.TARGET_BRANCH }} exists"
else
echo "::error::Branch ${{ inputs.TARGET_BRANCH }} does not exist"
gh pr comment "${{ github.event.pull_request.number }}" \
--repo ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} \
--body "❌ The backport branch \`${{ inputs.TARGET_BRANCH }}\` doesn't exist."
echo "failure-already-reported=true" >> ${GITHUB_OUTPUT}
exit 1
fi
env:
GH_TOKEN: ${{ github.token }}

- name: Backport
shell: ${{ env.shell }}
run: |
# Git metadata is required but not available out-of-the-box, inherit from the action
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"

# Add "upstream" remote as checkout action doesn't include by default
git remote add upstream "${{ github.event.repository.clone_url }}"
nishaatr marked this conversation as resolved.
Show resolved Hide resolved
git fetch --all

backport_target_branch=upstream/"${{ inputs.TARGET_BRANCH }}"
echo "::debug::Running backport script to backport "${{ inputs.REF_TO_BACKPORT }}" into \"${backport_target_branch}\""
nishaatr marked this conversation as resolved.
Show resolved Hide resolved

${GITHUB_ACTION_PATH}/../../../backport \
"${{ inputs.REF_TO_BACKPORT }}" \
"${backport_target_branch}" \
--non-interactive \
${{ inputs.BACKPORT_OPTIONS }}
env:
GH_TOKEN: ${{ github.token }}

- name: Report errors
shell: ${{ env.shell }}
if: failure() && steps.assert-branch-exists.outputs.failure-already-reported != 'true'
run: |
echo ":error::Error running action"
echo "::group::Troubleshooting Information:"
echo "- Repositories' GitHub action configuration - ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/settings/actions"
echo "- Action does not run when triggered from a forked repo's PR?"
nishaatr marked this conversation as resolved.
Show resolved Hide resolved
echo " Enable \"Run workflows from fork pull requests\""
echo "- Backport fails with \"GraphQL: GitHub Actions is not permitted to create or approve pull requests (createPullRequest)\"?"
echo " Either:"
echo " * Enable \"Allow GitHub Actions to create and approve pull requests\""
echo " * Use a different \"GITHUB_TOKEN\" with appropriate permissions"
echo "- \"GraphQL: Resource not accessible by integration (addComment)\"?"
echo " Either:"
echo " * Enable \"Send write tokens to workflows from fork pull requests\""
echo " * Use a different \"GITHUB_TOKEN\" with appropriate permissions"
echo "::endgroup::"
gh pr comment "${{ github.event.pull_request.number }}" \
--repo ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} \
--body "❌ [Failed to backport](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}), change must be manually backported."
env:
GH_TOKEN: ${{ github.token }}