Skip to content

Commit aa0a782

Browse files
[CI] Add missing support for skip-target-branches (#134743) (#134757)
(cherry picked from commit 28f8850) Co-authored-by: Elastic Machine <[email protected]>
1 parent 5dc10ba commit aa0a782

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

.buildkite/scripts/pull-request/__snapshots__/pipeline.test.ts.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,68 @@ exports[`generatePipelines should generate correct pipelines with a non-docs cha
309309
},
310310
]
311311
`;
312+
313+
exports[`generatePipelines should generate correct pipelines with a different branch that is not skipped 1`] = `
314+
[
315+
{
316+
"name": "bwc-snapshots",
317+
"pipeline": {
318+
"steps": [
319+
{
320+
"group": "bwc-snapshots",
321+
"steps": [
322+
{
323+
"agents": {
324+
"buildDirectory": "/dev/shm/bk",
325+
"image": "family/elasticsearch-ubuntu-2404",
326+
"machineType": "custom-32-98304",
327+
"provider": "gcp",
328+
},
329+
"command": ".ci/scripts/run-gradle.sh -Dignore.tests.seed v{{matrix.BWC_VERSION}}#bwcTest",
330+
"env": {
331+
"BWC_VERSION": "{{matrix.BWC_VERSION}}",
332+
},
333+
"label": "{{matrix.BWC_VERSION}} / bwc-snapshots",
334+
"matrix": {
335+
"setup": {
336+
"BWC_VERSION": [
337+
"7.17.14",
338+
"8.10.3",
339+
"8.11.0",
340+
],
341+
},
342+
},
343+
"timeout_in_minutes": 300,
344+
},
345+
],
346+
},
347+
],
348+
},
349+
},
350+
{
351+
"name": "skip-branch",
352+
"pipeline": {
353+
"steps": [
354+
{
355+
"command": "echo 1",
356+
"label": "skip-me",
357+
},
358+
],
359+
},
360+
},
361+
{
362+
"name": "using-defaults",
363+
"pipeline": {
364+
"env": {
365+
"CUSTOM_ENV_VAR": "value",
366+
},
367+
"steps": [
368+
{
369+
"command": "echo 'hello world'",
370+
"label": "test-step",
371+
},
372+
],
373+
},
374+
},
375+
]
376+
`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
config:
2+
skip-target-branches: "test-branch"
3+
steps:
4+
- label: skip-me
5+
command: echo 1

.buildkite/scripts/pull-request/pipeline.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe("generatePipelines", () => {
88
setBwcVersionsPath(`${import.meta.dir}/mocks/bwcVersions`);
99
setSnapshotBwcVersionsPath(`${import.meta.dir}/mocks/snapshotBwcVersions`);
1010

11+
process.env["GITHUB_PR_TARGET_BRANCH"] = "test-branch";
1112
process.env["GITHUB_PR_LABELS"] = "test-label-1,test-label-2";
1213
process.env["GITHUB_PR_TRIGGER_COMMENT"] = "";
1314
});
@@ -36,6 +37,12 @@ describe("generatePipelines", () => {
3637
testWithTriggerCheck(`${import.meta.dir}/mocks/pipelines`, ["build.gradle"]);
3738
});
3839

40+
test("should generate correct pipelines with a different branch that is not skipped", () => {
41+
process.env["GITHUB_PR_TARGET_BRANCH"] = "main";
42+
43+
testWithTriggerCheck(`${import.meta.dir}/mocks/pipelines`, ["build.gradle"]);
44+
});
45+
3946
test("should generate correct pipeline when using a trigger comment for it", () => {
4047
process.env["GITHUB_PR_TRIGGER_COMMENT"] = "run elasticsearch-ci/using-defaults";
4148

.buildkite/scripts/pull-request/pipeline.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ const changedFilesIncludedCheck = (pipeline: EsPipeline, changedFiles: string[])
5050
return true;
5151
};
5252

53+
const checkTargetBranch = (pipeline: EsPipeline, targetBranch: string | undefined) => {
54+
if (!targetBranch || !pipeline.config?.["skip-target-branches"]) {
55+
return true;
56+
}
57+
58+
return !getArray(pipeline.config["skip-target-branches"]).some((branch) => branch === targetBranch);
59+
};
60+
5361
const triggerCommentCheck = (pipeline: EsPipeline): boolean => {
5462
if (process.env["GITHUB_PR_TRIGGER_COMMENT"] && pipeline.config?.["trigger-phrase"]) {
5563
return !!process.env["GITHUB_PR_TRIGGER_COMMENT"].match(pipeline.config["trigger-phrase"]);
@@ -138,6 +146,7 @@ export const generatePipelines = (
138146
}
139147

140148
let filters: ((pipeline: EsPipeline) => boolean)[] = [
149+
(pipeline) => checkTargetBranch(pipeline, process.env["GITHUB_PR_TARGET_BRANCH"]),
141150
(pipeline) => labelCheckAllow(pipeline, labels),
142151
(pipeline) => labelCheckSkip(pipeline, labels),
143152
(pipeline) => changedFilesExcludedCheck(pipeline, changedFiles),

.buildkite/scripts/pull-request/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type EsPipelineConfig = {
55
"included-regions"?: string | string[];
66
"excluded-regions"?: string | string[];
77
"trigger-phrase"?: string;
8+
"skip-target-branches"?: string | string[];
89
};
910
};
1011

0 commit comments

Comments
 (0)