diff --git a/pkg/api/types.go b/pkg/api/types.go index 1af626139c2..1115a5314a4 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -881,6 +881,14 @@ type TestStepConfiguration struct { // Only applicable to presubmits; postsubmits always run on every merge. SkipIfOnlyChanged string `json:"skip_if_only_changed,omitempty"` + // SkipBranches is a list of branch name regex patterns. Branches matching + // any of these patterns will be excluded from running this presubmit job. + // This maps directly to the upstream Prow Brancher.SkipBranches field and + // is useful to prevent feature branch inheritance (e.g. preventing a + // presubmit configured for "main" from also running on "main-*" branches). + // Only applicable to presubmit tests. + SkipBranches []string `json:"skip_branches,omitempty"` + // PipelineSkipIfOnlyChanged is a regex that will result in the test being skipped in second // stage of the pipeline run if all changed files match that regex. PipelineSkipIfOnlyChanged string `json:"pipeline_skip_if_only_changed,omitempty"` diff --git a/pkg/prowgen/prowgen.go b/pkg/prowgen/prowgen.go index 7663a360821..5379975482b 100644 --- a/pkg/prowgen/prowgen.go +++ b/pkg/prowgen/prowgen.go @@ -225,6 +225,7 @@ func handlePresubmit(g *prowJobBaseBuilder, element cioperatorapi.TestStepConfig options.Capabilities = element.Capabilities options.runIfChanged = element.RunIfChanged options.skipIfOnlyChanged = element.SkipIfOnlyChanged + options.skipBranches = element.SkipBranches options.defaultDisable = element.AlwaysRun != nil && !*element.AlwaysRun options.optional = element.Optional options.disableRehearsal = disableRehearsal @@ -244,6 +245,7 @@ type generatePresubmitOptions struct { Capabilities []string runIfChanged string skipIfOnlyChanged string + skipBranches []string defaultDisable bool optional bool disableRehearsal bool @@ -307,7 +309,10 @@ func generatePresubmitForTest(jobBaseBuilder *prowJobBaseBuilder, name string, i pj := &prowconfig.Presubmit{ JobBase: base, AlwaysRun: opts.shouldAlwaysRun(), - Brancher: prowconfig.Brancher{Branches: sets.List(sets.New[string](jc.ExactlyBranch(info.Branch), jc.FeatureBranch(info.Branch)))}, + Brancher: prowconfig.Brancher{ + Branches: sets.List(sets.New[string](jc.ExactlyBranch(info.Branch), jc.FeatureBranch(info.Branch))), + SkipBranches: opts.skipBranches, + }, Reporter: prowconfig.Reporter{ Context: fmt.Sprintf("ci/prow/%s", shortName), }, diff --git a/pkg/prowgen/prowgen_test.go b/pkg/prowgen/prowgen_test.go index 9f3035c9e70..83f2023baac 100644 --- a/pkg/prowgen/prowgen_test.go +++ b/pkg/prowgen/prowgen_test.go @@ -240,6 +240,14 @@ func TestGeneratePresubmitForTest(t *testing.T) { options.maxConcurrency = 4 }, }, + { + description: "presubmit with skip_branches", + test: "testname", + repoInfo: &ciop.Metadata{Org: "org", Repo: "repo", Branch: "branch"}, + generateOption: func(options *generatePresubmitOptions) { + options.skipBranches = []string{"^branch-foo$", "^branch-bar$"} + }, + }, } for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { @@ -935,8 +943,21 @@ func TestGenerateJobs(t *testing.T) { Branch: "branch", }, }, + { + id: "presubmit with per-test skip_branches", + keep: true, + config: &ciop.ReleaseBuildConfiguration{ + Tests: []ciop.TestStepConfiguration{ + {As: "derTest", SkipBranches: []string{"^branch-foo$", "^branch-bar$"}, ContainerTestConfiguration: &ciop.ContainerTestConfiguration{From: "from"}}, + }, + }, + repoInfo: &ciop.Metadata{ + Org: "organization", + Repo: "repository", + Branch: "branch", + }, + }, } - for _, tc := range tests { t.Run(tc.id, func(t *testing.T) { jobConfig, err := GenerateJobs(tc.config, tc.repoInfo) diff --git a/pkg/prowgen/testdata/zz_fixture_TestGenerateJobs_presubmit_with_per-test_skip_branches.yaml b/pkg/prowgen/testdata/zz_fixture_TestGenerateJobs_presubmit_with_per-test_skip_branches.yaml new file mode 100644 index 00000000000..f6168cb225a --- /dev/null +++ b/pkg/prowgen/testdata/zz_fixture_TestGenerateJobs_presubmit_with_per-test_skip_branches.yaml @@ -0,0 +1,58 @@ +presubmits: + organization/repository: + - agent: kubernetes + always_run: true + branches: + - ^branch$ + - ^branch- + context: ci/prow/derTest + decorate: true + decoration_config: + skip_cloning: true + labels: + pj-rehearse.openshift.io/can-be-rehearsed: "true" + name: pull-ci-organization-repository-branch-derTest + rerun_command: /test derTest + skip_branches: + - ^branch-foo$ + - ^branch-bar$ + spec: + containers: + - args: + - --gcs-upload-secret=/secrets/gcs/service-account.json + - --image-import-pull-secret=/etc/pull-secret/.dockerconfigjson + - --report-credentials-file=/etc/report/credentials + - --target=derTest + command: + - ci-operator + image: quay-proxy.ci.openshift.org/openshift/ci:ci_ci-operator_latest + imagePullPolicy: Always + name: "" + resources: + requests: + cpu: 10m + volumeMounts: + - mountPath: /secrets/gcs + name: gcs-credentials + readOnly: true + - mountPath: /secrets/manifest-tool + name: manifest-tool-local-pusher + readOnly: true + - mountPath: /etc/pull-secret + name: pull-secret + readOnly: true + - mountPath: /etc/report + name: result-aggregator + readOnly: true + serviceAccountName: ci-operator + volumes: + - name: manifest-tool-local-pusher + secret: + secretName: manifest-tool-local-pusher + - name: pull-secret + secret: + secretName: registry-pull-credentials + - name: result-aggregator + secret: + secretName: result-aggregator + trigger: (?m)^/test( | .* )derTest,?($|\s.*) diff --git a/pkg/prowgen/testdata/zz_fixture_TestGeneratePresubmitForTest_presubmit_with_skip_branches.yaml b/pkg/prowgen/testdata/zz_fixture_TestGeneratePresubmitForTest_presubmit_with_skip_branches.yaml new file mode 100644 index 00000000000..61d59f90029 --- /dev/null +++ b/pkg/prowgen/testdata/zz_fixture_TestGeneratePresubmitForTest_presubmit_with_skip_branches.yaml @@ -0,0 +1,17 @@ +agent: kubernetes +always_run: true +branches: +- ^branch$ +- ^branch- +context: ci/prow/testname +decorate: true +decoration_config: + skip_cloning: true +labels: + pj-rehearse.openshift.io/can-be-rehearsed: "true" +name: pull-ci-org-repo-branch-testname +rerun_command: /test testname +skip_branches: +- ^branch-foo$ +- ^branch-bar$ +trigger: (?m)^/test( | .* )testname,?($|\s.*)