Skip to content

Commit

Permalink
Create centralised backport workflow (#17)
Browse files Browse the repository at this point in the history
To support `backport` being used in multiple GitHub Actions:
- hazelcast/hazelcast-mono#3642
- hazelcast/hz-docs#1396

A composite GitHub Action has been created to centralise it's usage, to
make callers simpler.

Implementation is a sliced from `hazelcast-mono`, with some parameters
now explicit inputs.

Tested via [dummy PR](JackPGreen2/hz-docs#14) in
[test repo](https://github.com/JackPGreen2/hz-docs) that [backported
successfully](https://github.com/JackPGreen2/hz-docs/actions/runs/12029584772/job/33536129458)
(via JackPGreen2/hz-docs#26).
  • Loading branch information
JackPGreen authored Nov 27, 2024
1 parent a71029f commit dc54f4c
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .github/actions/backport/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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:
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

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 }}"
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}\""
${GITHUB_ACTION_PATH}/../../../backport \
"${{ inputs.REF_TO_BACKPORT }}" \
"${backport_target_branch}" \
--non-interactive
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?"
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 }}

0 comments on commit dc54f4c

Please sign in to comment.