Repository navigation
488 lines (424 loc) · 19.1 KB
/
Copy pathget-environment.yml
File metadata and controls
488 lines (424 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
on:
workflow_call:
inputs:
version_file:
required: false
type: string
nightly_manual_trigger:
required: false
type: boolean
outputs:
version:
description: "version"
value: ${{ jobs.get-environment.outputs.version }}
release:
description: "release"
value: ${{ jobs.get-environment.outputs.release }}
stability:
description: "branch stability (stable, testing, unstable, canary)"
value: ${{ jobs.get-environment.outputs.stability }}
target_stability:
description: "Final target branch stability (stable, testing, unstable, canary or not defined if not a pull request)"
value: ${{ jobs.get-environment.outputs.target_stability }}
is_nightly:
description: "if the current workflow run is considered a nightly"
value: ${{ jobs.get-environment.outputs.is_nightly }}
release_type:
description: "type of release (hotfix, release or not defined if not a release)"
value: ${{ jobs.get-environment.outputs.release_type }}
is_targeting_feature_branch:
description: "if it is a PR, check if targeting a feature branch"
value: ${{ jobs.get-environment.outputs.is_targeting_feature_branch }}
skip_workflow:
description: "if the current workflow should be skipped"
value: ${{ jobs.get-environment.outputs.skip_workflow }}
labels:
description: "list of labels on the PR"
value: ${{ jobs.get-environment.outputs.labels }}
jobs:
get-environment:
runs-on: ubuntu-24.04
outputs:
version: ${{ steps.get_version.outputs.version }}
release: ${{ steps.get_release.outputs.release }}
stability: ${{ steps.get_stability.outputs.stability }}
target_stability: ${{ steps.get_stability.outputs.target_stability }}
release_type: ${{ steps.get_release_type.outputs.release_type }}
is_targeting_feature_branch: ${{ steps.get_stability.outputs.is_targeting_feature_branch }}
is_nightly: ${{ steps.get_nightly_status.outputs.is_nightly }}
skip_workflow: ${{ steps.skip_workflow.outputs.result }}
labels: ${{ steps.has_skip_label.outputs.labels }}
steps:
- name: Check if PR has skip label
id: has_skip_label
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
let hasSkipLabel = false;
let labels = [];
if (${{ contains(fromJSON('["pull_request"]') , github.event_name) }} === true) {
try {
const fetchedLabels = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
fetchedLabels.data.forEach(({ name }) => {
labels.push(name);
if (name === '${{ format('skip-workflow-{0}', github.workflow) }}') {
hasSkipLabel = true;
}
});
} catch (e) {
core.warning(`failed to list labels: ${e}`);
}
}
core.setOutput('labels', labels);
return hasSkipLabel;
- name: Checkout sources (current branch)
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: ${{ github.ref_type == 'tag' && '0' || (steps.has_skip_label.outputs.result == 'true' && 100 || 1) }}
- if: ${{ github.ref_type == 'tag' }}
name: Get release tag context
id: tag_context
env:
TAG_NAME: ${{ github.ref_name }}
WORKFLOW_REF: ${{ github.workflow_ref }}
run: |
WORKFLOW_FILE="${WORKFLOW_REF#"$GITHUB_REPOSITORY/"}"
export WORKFLOW_FILE="${WORKFLOW_FILE%%@*}"
.github/scripts/release/get-release-tag-context.sh
shell: bash
- if: ${{ steps.has_skip_label.outputs.result == 'true' }}
name: Get workflow triggered paths
id: get_workflow_triggered_paths
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const fs = require('fs');
let paths = [];
const workflowFilePath = '${{ github.workflow_ref }}'.replace('${{ github.repository }}/', '').split('@').shift();
if (fs.existsSync(workflowFilePath)) {
const workflowFileContent = fs.readFileSync(workflowFilePath, 'utf8');
const workflowFileContentLines = workflowFileContent.split('\n');
let hasReadOn = false;
let hasReadPullRequest = false;
let hasReadPaths = false;
for (const line of workflowFileContentLines) {
if (line.match(/^on:\s*$/)) {
hasReadOn = true;
continue;
}
if (line.match(/^\s{2}pull_request(_target)?:\s*$/)) {
hasReadPullRequest = true;
continue;
}
if (line.match(/^\s{4}paths:\s*$/)) {
hasReadPaths = true;
continue;
}
if (hasReadOn && hasReadPullRequest && hasReadPaths) {
const matches = line.match(/^\s{6}-\s['"](.+)['"]\s*$/);
if (matches) {
paths.push(matches[1].trim());
} else {
break;
}
}
}
}
if (paths.length === 0) {
paths = ['**'];
}
console.log(paths);
return paths;
- if: ${{ steps.has_skip_label.outputs.result == 'true' }}
name: Get push changes
id: get_push_changes
uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6
with:
since_last_remote_commit: true
json: true
escape_json: false
files: ${{ join(fromJSON(steps.get_workflow_triggered_paths.outputs.result), ';') }}
files_separator: ';'
- name: Check if current workflow should be skipped
id: skip_workflow
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
with:
script: |
if ('${{ steps.tag_context.outputs.is_release_tag }}' === 'true') {
if ('${{ steps.tag_context.outputs.changed }}' !== 'true') {
core.notice('skipping workflow because its paths did not change since ${{ steps.tag_context.outputs.previous_tag }}');
return true;
}
return false;
}
if (/^bump-.+/.test(process.env.BRANCH_NAME)) {
core.notice('skipping workflow because it is a bump branch');
return true;
}
if (${{ steps.has_skip_label.outputs.result }} === false) {
return false;
}
const label = '${{ format('skip-workflow-{0}', github.workflow) }}';
if ('${{ steps.get_push_changes.outputs.any_changed }}' === 'true') {
try {
await github.rest.issues.removeLabel({
name: label,
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
core.notice(`label ${label} removed because changes were detected on last push.`);
} catch (e) {
core.warning(`failed to remove label ${label}: ${e}`);
}
return false;
}
return true;
- if: ${{ github.event_name == 'pull_request' }}
name: Get nested pull request path
id: pr_path
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const prPath = ['${{ github.head_ref }}', '${{ github.base_ref }}'];
const result = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
state: 'open'
});
let found = true;
while (found) {
found = false;
result.data.forEach(({ head: { ref: headRef }, base: { ref: baseRef} }) => {
if (headRef === prPath[prPath.length - 1] && ! prPath.includes(baseRef)) {
found = true;
prPath.push(baseRef);
}
});
}
return prPath;
- name: Get stability
id: get_stability
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const getStability = (branchName) => {
switch (true) {
case /(^develop$)|(^dev-\d{2}\.\d{2}\.x$)/.test(branchName):
return 'unstable';
case /(^release.+)|(^hotfix.+)/.test(branchName):
return 'testing';
case /(^master$)|(^\d{2}\.\d{2}\.x$)/.test(branchName):
return 'stable';
default:
return 'canary';
}
};
if ('${{ steps.tag_context.outputs.is_release_tag }}' === 'true') {
core.setOutput('stability', 'stable');
} else {
core.setOutput('stability', getStability('${{ github.head_ref || github.ref_name }}'));
}
let isTargetingFeatureBranch = false;
if ("${{ github.event_name }}" === "pull_request") {
let targetStability = 'canary';
const prPath = ${{ steps.pr_path.outputs.result || '[]' }};
prPath.shift(); // remove current branch
if (prPath.length && getStability(prPath[0]) === 'canary') {
isTargetingFeatureBranch = true;
}
prPath.every((branchName) => {
console.log(`checking stability of ${branchName}`)
targetStability = getStability(branchName);
if (targetStability !== 'canary') {
return false;
}
return true;
});
core.setOutput('target_stability', targetStability);
}
core.setOutput('is_targeting_feature_branch', isTargetingFeatureBranch);
- name: Detect nightly status
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
id: get_nightly_status
env:
NIGHTLY_MANUAL_TRIGGER: ${{ inputs.nightly_manual_trigger }}
with:
script: |
const getNightlyInput = () => {
const nightly_manual_trigger = process.env.NIGHTLY_MANUAL_TRIGGER;
console.log(nightly_manual_trigger);
if (typeof nightly_manual_trigger === 'undefined' || nightly_manual_trigger === '' || '${{ github.repository }}'.match(/^workflow-.*$/)) {
return 'false';
} else if (context.eventName === 'schedule' || context.eventName === 'workflow_dispatch' && nightly_manual_trigger === 'true' ) {
return 'true';
}
return 'false';
};
core.setOutput('is_nightly', getNightlyInput());
- name: Get version
id: get_version
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const { execSync } = require('child_process');
const fs = require('fs');
let version = '';
if ('${{ inputs.version_file }}'.match(/pom\.xml$/)) {
version = execSync(`grep -m 1 "<version>.*</version>" ${{ inputs.version_file }} | sed 's/.*<version>\\(.*\\)<\\/version>.*/\\1/'`).toString().trim();
} else if ('${{ steps.tag_context.outputs.is_release_tag }}' === 'true') {
version = '${{ steps.tag_context.outputs.version }}';
} else if ('${{ steps.get_stability.outputs.stability }}' === 'stable') {
const { owner, repo } = context.repo;
// Fetch the most recent tag for plugins
const { data: tags } = await github.rest.repos.listTags({
owner,
repo,
per_page: 10
});
let latestTag = null;
let latestDate = 0;
// Filter tags matching format plugins-YYYYMMDD
for (const tag of tags) {
const match = tag.name.match(/^plugins-(\d{8})$/);
if (!match) {
continue;
}
const tagDate = parseInt(match[1], 10);
// ensure we get the true latest tag and not the most recent created
if (tagDate > latestDate) {
latestTag = tag.name;
latestDate = tagDate;
}
}
console.log(`Most recent tag found: ${latestTag}`)
// Get current release tag from .version file
version = fs.readFileSync('.version.plugins', 'utf8').trim();
console.log(`Stable version based on .version.plugins file will be: ${version}`)
} else if ('${{ steps.get_stability.outputs.stability }}' === 'testing') {
const branchName = "${{ github.head_ref || github.ref_name }}";
const matches = branchName.match(/^(?:release|hotfix)-(\d{8})$/);
if (matches) {
version = matches[1];
} else {
throw new Error('invalid version');
}
} else if ('${{ steps.get_stability.outputs.stability }}' === 'unstable') {
const currentDate = new Date();
version = `${currentDate.getFullYear()}${("0" + (currentDate.getMonth() + 1)).slice(-2)}${String(currentDate.getDate()).padStart(2, '0')}`;
} else {
const currentDate = new Date();
version = `${currentDate.getFullYear()}${("0" + (currentDate.getMonth() + 1)).slice(-2)}00`;
}
core.setOutput('version', version);
- name: "Get release: 1 for testing / stable, <date> for others"
id: get_release
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
let release = '';
if (${{ contains(fromJSON('["testing", "unstable"]') , steps.get_stability.outputs.stability) }} === true) {
release = "1"
} else {
release = Date.now()
}
core.setOutput('release', release);
- name: "Get release type: hotfix, release or not defined if not a release"
id: get_release_type
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
HEAD_REF: ${{ github.head_ref || github.ref_name }}
with:
script: |
if ('${{ steps.tag_context.outputs.is_release_tag }}' === 'true') {
core.setOutput('release_type', '${{ steps.tag_context.outputs.release_type }}');
return;
}
if ('${{ steps.get_stability.outputs.stability }}' !== 'stable') {
const releaseType = process.env.HEAD_REF.split('-')[0];
if (['hotfix', 'release'].includes(releaseType)) {
core.setOutput('release_type', releaseType);
}
return;
}
// stable branch push: release type comes from the merged release/hotfix pull request
if (context.eventName !== 'push') {
return;
}
const commitSha = context.sha;
const pulls = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
sort: 'updated',
direction: 'desc',
state: 'closed',
per_page: 100
});
core.startGroup(`Checking pull request linked to commit ${commitSha}`);
const pr = pulls.data.find(p => {
console.log(`Checking pull request ${p.number}("${p.title}") with merge commit ${p.merge_commit_sha}`);
return p.merge_commit_sha === commitSha;
});
core.endGroup();
if (!pr) {
core.error(`No pull request found for merge commit ${commitSha}`);
return;
}
const prHeadRef = pr?.head?.ref || 'unknown';
switch (true) {
case /^release.+/.test(prHeadRef):
core.setOutput('release_type', 'release');
break;
case /^hotfix.+/.test(prHeadRef):
core.setOutput('release_type', 'hotfix');
break;
default:
core.error(`No packages are promoted because push of branch ${prHeadRef} is not related to a hotfix/release pull request.`);
}
- name: Display info in job summary
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const outputTable = [
[{data: 'Name', header: true}, {data: 'Value', header: true}],
['version', '${{ steps.get_version.outputs.version }}'],
['release', '${{ steps.get_release.outputs.release }}'],
['stability', '${{ steps.get_stability.outputs.stability }}'],
['release_type', '${{ steps.get_release_type.outputs.release_type || '<em>not defined because this is not a release</em>' }}'],
['is_targeting_feature_branch', '${{ steps.get_stability.outputs.is_targeting_feature_branch }}'],
['target_stability', '${{ steps.get_stability.outputs.target_stability || '<em>not defined because current run is not triggered by pull request event</em>' }}'],
['is_nightly', '${{ steps.get_nightly_status.outputs.is_nightly }}'],
['skip_workflow', '${{ steps.skip_workflow.outputs.result }}'],
['labels', '${{ steps.has_skip_label.outputs.labels }}'],
];
core.summary
.addHeading(`${context.workflow} environment outputs`)
.addTable(outputTable);
if ("${{ github.event_name }}" === "pull_request") {
const prPath = ${{ steps.pr_path.outputs.result || '[]' }};
const mainBranchName = prPath.pop();
let codeBlock = `
%%{ init: { 'gitGraph': { 'mainBranchName': '${mainBranchName}', 'showCommitLabel': false } } }%%
gitGraph
commit`;
prPath.reverse().forEach((branchName) => {
codeBlock = `${codeBlock}
branch ${branchName}
checkout ${branchName}
commit`;
});
core.summary
.addHeading('Git workflow')
.addCodeBlock(
codeBlock,
"mermaid"
);
}
core.summary.write();