Skip to content

Commit d4f8c56

Browse files
committed
add workflow for creating backport PRs
1 parent a85fd7c commit d4f8c56

4 files changed

Lines changed: 207 additions & 1 deletion

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: On Command
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
permissions:
8+
contents: read
9+
actions: write
10+
pull-requests: write
11+
12+
jobs:
13+
parse:
14+
name: Check Permissions and Parse Command ⌨️
15+
runs-on: ubuntu-latest
16+
outputs:
17+
command: ${{ steps.command.outputs.command }}
18+
steps:
19+
- name: Get actor permissions
20+
id: check-permissions
21+
continue-on-error: true
22+
uses: prince-chrismc/check-actor-permissions-action@v3.0.2
23+
with:
24+
permission: write
25+
26+
- name: Log actor permissions
27+
run: |
28+
echo Actor has permission: ${{ steps.check-permissions.outputs.permitted && ' true' || 'false' }}
29+
echo Actor is bot user: ${{ github.actor == 'nixos-wsl-bot' && ' true' || 'false' }}
30+
31+
- name: Post error message if not permitted
32+
if: ${{ !steps.check-permissions.outputs.permitted && github.actor != 'nixos-wsl-bot' }}
33+
uses: thollander/actions-comment-pull-request@v3.0.1
34+
with:
35+
pr-number: ${{ inputs.pr_number || github.event.issue.number }}
36+
message: |
37+
You do not have permission to run this command @${{ github.actor }}.
38+
39+
- name: Stop if not permitted
40+
if: ${{ !steps.check-permissions.outputs.permitted && github.actor != 'nixos-wsl-bot' }}
41+
run: |
42+
exit 1
43+
44+
- name: Parse command
45+
id: command
46+
if: steps.check-permissions.outputs.permitted || github.actor == 'nixos-wsl-bot'
47+
run: |
48+
echo ${{ github.event.comment.body }} | sed -nE 's|^/([a-z0-9\-]+)|command=\1|p' >> $GITHUB_OUTPUT
49+
50+
trigger-on-push:
51+
name: Trigger Push Workflow πŸš€
52+
needs: parse
53+
if: needs.parse.outputs.command == 'run-ci' && github.event.issue.pull_request
54+
runs-on: ubuntu-latest
55+
steps:
56+
- name: Checkout
57+
uses: actions/checkout@v4.2.2
58+
59+
- name: Get PR branch
60+
id: branch
61+
env:
62+
GH_TOKEN: ${{ github.token }}
63+
run: |
64+
echo "branch=$(gh pr view ${{ github.event.issue.number }} --json headRefName -q .headRefName)" >> $GITHUB_OUTPUT
65+
66+
- name: Dispatch workflow
67+
uses: actions/github-script@v7.0.1
68+
with:
69+
script: |
70+
github.rest.actions.createWorkflowDispatch({
71+
owner: context.repo.owner,
72+
repo: context.repo.repo,
73+
workflow_id: 'on_push.yml',
74+
ref: '${{ steps.branch.outputs.branch }}',
75+
inputs: {
76+
pr_number: '${{ github.event.issue.number }}',
77+
post_result: 'true'
78+
}
79+
})
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Heavily inspired by https://github.com/NixOS/nixpkgs/blob/a698ac1214cd924d4394ca9cd2691618765aa03c/.github/workflows/backport.yml
2+
3+
name: "On Backport Label"
4+
5+
on:
6+
pull_request:
7+
types:
8+
- closed
9+
- labeled
10+
11+
permissions:
12+
actions: write
13+
contents: write
14+
pull-requests: write
15+
16+
jobs:
17+
backport:
18+
if: github.event.pull_request.labels && contains(join(github.event.pull_request.labels.*.name, ', '), 'backport ')
19+
name: Backport PR πŸ”™
20+
runs-on: ubuntu-latest
21+
outputs:
22+
created_pull_numbers: ${{ steps.backport.outputs.created_pull_numbers }}
23+
steps:
24+
- name: Check actor permissions
25+
id: check-permissions
26+
uses: prince-chrismc/check-actor-permissions-action@v3.0.2
27+
with:
28+
permission: write
29+
30+
- name: Checkout
31+
uses: actions/checkout@v4.2.2
32+
33+
- name: Create backport PR
34+
id: backport
35+
uses: korthout/backport-action@v3.2.1
36+
with:
37+
merge_commits: "skip"
38+
add_author_as_assignee: true
39+
copy_requested_reviewers: true
40+
41+
trigger-ci:
42+
name: Trigger CI on created branches πŸš€
43+
needs: backport
44+
if: needs.backport.outputs.created_pull_numbers
45+
strategy:
46+
matrix:
47+
pr: ${{ fromJSON(format('[{0}]', needs.backport.outputs.created_pull_numbers)) }}
48+
runs-on: ubuntu-latest
49+
steps:
50+
- name: Checkout
51+
uses: actions/checkout@v4.2.2
52+
53+
- name: Get PR branch
54+
id: branch
55+
env:
56+
GH_TOKEN: ${{ github.token }}
57+
run: |
58+
echo "branch=$(gh pr view ${{ matrix.pr }} --json headRefName -q .headRefName)" >> $GITHUB_OUTPUT
59+
60+
- name: Dispatch workflow
61+
uses: actions/github-script@v7.0.1
62+
with:
63+
script: |
64+
github.rest.actions.createWorkflowDispatch({
65+
owner: context.repo.owner,
66+
repo: context.repo.repo,
67+
workflow_id: 'on_push.yml',
68+
ref: '${{ steps.branch.outputs.branch }}',
69+
inputs: {
70+
pr_number: '${{ matrix.pr }}',
71+
post_result: 'true'
72+
}
73+
})

β€Ž.github/workflows/on_push.ymlβ€Ž

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
1-
name: "Push"
1+
name: "On Push"
22

33
on:
44
push:
55
branches:
66
- main
77
- "release-*"
88
pull_request: {}
9+
workflow_dispatch:
10+
inputs:
11+
pr_number:
12+
description: "Pull Request Number"
13+
required: false
14+
default: ""
15+
type: string
16+
post_result:
17+
description: "Post the result of the pipeline to the PR"
18+
required: false
19+
default: false
20+
type: boolean
921

1022
jobs:
23+
start-comment:
24+
name: Post Start Comment πŸ’¬
25+
if: ${{ inputs.post_result }}
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: thollander/actions-comment-pull-request@v3.0.1
29+
with:
30+
pr-number: ${{ inputs.pr_number || github.event.pull_request.number }}
31+
comment-tag: ci-status
32+
mode: recreate
33+
create-if-not-exists: true
34+
message: |
35+
CI is currently running
36+
[See the logs here](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
37+
1138
build:
1239
name: Build πŸ› οΈ
1340
uses: ./.github/workflows/run_build.yml
@@ -55,3 +82,28 @@ jobs:
5582
}
5683
5784
exit $exit
85+
86+
finish-comment:
87+
name: Post Finish Comment πŸ’¬
88+
if: ${{ inputs.post_result }}
89+
needs:
90+
- start-comment
91+
- build
92+
- checks
93+
- tests
94+
- docs
95+
runs-on: ubuntu-latest
96+
steps:
97+
- uses: thollander/actions-comment-pull-request@v3.0.1
98+
with:
99+
pr-number: ${{ inputs.pr_number || github.event.pull_request.number }}
100+
comment-tag: ci-status
101+
mode: upsert
102+
message: |
103+
CI has finished:
104+
- πŸ“‹ Checks: ${{ needs.checks.result && 'βœ…' || '❌' }}
105+
- πŸ› οΈ Build: ${{ needs.build.result && 'βœ…' || '❌' }}
106+
- πŸ§ͺ Tests: ${{ needs.tests.result && 'βœ…' || '❌' }}
107+
- πŸ“• Docs: ${{ needs.docs.result && 'βœ…' || '❌' }}
108+
109+
[See the logs here](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})

β€Ž.github/workflows/release-drafter.ymlβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ on:
99
- opened
1010
- reopened
1111
- synchronize
12+
- labeled
13+
- unlabeled
1214
workflow_dispatch: {}
1315

1416
permissions:

0 commit comments

Comments
Β (0)