Skip to content

Commit 1cf2d17

Browse files
delannialbertoblaz
authored andcommitted
[CI] Trigger backports when v* labels are added afterwards (elastic#234452)
## Summary backport should now trigger if `vX.Y.Z` labels are added last - introduce a concurrency group to avoid multiple runs This was already done in elastic#225511 then reverted in elastic#226840 This time, we're also adding a concurrency group to avoid the multiple runs (reason for the revert), and adding some wait time before the backport is started in (elastic/kibana-github-actions#62) Also, as per @mistic's suggestion, we're calculating the do/don't in a javascript block, so that we're not struggling with inline condition language.
1 parent 38cd880 commit 1cf2d17

File tree

5 files changed

+118
-10
lines changed

5 files changed

+118
-10
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"_description": "GitHub event payload for a pull request merge event. The PR is merged, action should always run.",
3+
"action": "closed",
4+
"number": 42,
5+
"pull_request": {
6+
"merged": true,
7+
"labels": [
8+
{ "name": "backport:any" },
9+
{ "name": "bugfix" }
10+
],
11+
"base": {
12+
"ref": "main"
13+
},
14+
"head": {
15+
"ref": "feature-branch"
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"_description": "GitHub event payload for a pull request merge event. The PR is labelled, has version and targets.",
3+
"action": "labeled",
4+
"number": 42,
5+
"pull_request": {
6+
"merged": true,
7+
"labels": [
8+
{ "name": "backport:version" },
9+
{ "name": "v9.2.1" }
10+
],
11+
"base": {
12+
"ref": "main"
13+
},
14+
"head": {
15+
"ref": "feature-branch"
16+
}
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"_description": "GitHub event payload for a pull request merge event. The PR is labelled, has backport:all-open, should trigger backports",
3+
"action": "labeled",
4+
"number": 42,
5+
"pull_request": {
6+
"merged": true,
7+
"labels": [
8+
{ "name": "backport:all-open" }
9+
],
10+
"base": {
11+
"ref": "main"
12+
},
13+
"head": {
14+
"ref": "feature-branch"
15+
}
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"_description": "GitHub event payload for a pull request merge event. The PR is labelled, has backport:skip, should not trigger backports",
3+
"action": "labeled",
4+
"number": 42,
5+
"pull_request": {
6+
"merged": true,
7+
"labels": [
8+
{ "name": "backport:skip" }
9+
],
10+
"base": {
11+
"ref": "main"
12+
},
13+
"head": {
14+
"ref": "feature-branch"
15+
}
16+
}
17+
}

.github/workflows/on-merge.yml

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,58 @@ on:
55
- labeled
66
- unlabeled
77

8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
10+
cancel-in-progress: true
11+
812
jobs:
913
on-merge:
1014
name: 'Label and Backport'
1115
runs-on: ubuntu-latest
12-
if: |
13-
github.event.pull_request.merged == true &&
14-
(github.event.action == 'closed' ||
15-
(github.event.action == 'labeled' &&
16-
(github.event.label.name == 'backport:all-open' ||
17-
github.event.label.name == 'backport:version')) ||
18-
(github.event.action == 'unlabeled' &&
19-
github.event.label.name == 'backport:skip' &&
20-
(contains(github.event.pull_request.labels.*.name, 'backport:all-open') ||
21-
contains(github.event.pull_request.labels.*.name, 'backport:version'))))
16+
if: github.event.pull_request.merged == true
2217
steps:
18+
- run: "echo hello!"
19+
- name: Check out versions.json
20+
uses: actions/checkout@v5
21+
with:
22+
repository: 'elastic/kibana'
23+
ref: main
24+
sparse-checkout: |
25+
versions.json
26+
- name: Check Event Qualifies
27+
uses: actions/github-script@v7
28+
id: check_event
29+
with:
30+
result-encoding: string
31+
script: |
32+
let shouldRun = false;
33+
const backportLabels = { skip: 'backport:skip', allOpen: 'backport:all-open', version: 'backport:version' };
34+
const { pull_request: pr, action } = context.payload;
35+
const labels = pr.labels.map(l => l.name);
36+
const validVersionLabels = require('./versions.json').versions
37+
.map(v => v.version.split('.').slice(0,2).join('.'))
38+
.map(v => RegExp(`^v${v}\.[0-9]{1,2}$`));
39+
const labelsWarrantBackport = labels.includes(backportLabels.allOpen) || (
40+
labels.includes(backportLabels.version) &&
41+
labels.some(label => validVersionLabels.some(v => v.test(label)))
42+
);
43+
44+
if (action === 'closed') {
45+
core.info('PR was merged, proceeding');
46+
shouldRun = true;
47+
} else if ((action === 'labeled' || action === 'unlabeled') && labelsWarrantBackport) {
48+
core.info('labelling action warrants backport, proceeding');
49+
shouldRun = true;
50+
} else {
51+
core.info('event does not warrant backport action, skipping');
52+
shouldRun = false;
53+
}
54+
55+
core.info(`Should run on-merge action: ${shouldRun}`);
56+
return shouldRun.toString();
57+
2358
- name: Checkout Actions
59+
if: ${{ steps.check_event.outputs.result == 'true' }}
2460
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
2561
with:
2662
repository: 'elastic/kibana-github-actions'
@@ -29,9 +65,11 @@ jobs:
2965
persist-credentials: false
3066

3167
- name: Install Actions
68+
if: ${{ steps.check_event.outputs.result == 'true' }}
3269
run: npm install --production --prefix ./actions
3370

3471
- name: Run On-Merge
72+
if: ${{ steps.check_event.outputs.result == 'true' }}
3573
uses: ./actions/on-merge
3674
with:
3775
github_token: ${{secrets.KIBANAMACHINE_TOKEN}}

0 commit comments

Comments
 (0)