Skip to content

Commit 887901f

Browse files
Add inline cron triggers to codefresh_pipeline (#122)
## What `[codefresh_pipeline]`: add `spec.cron_trigger` ## Why New feature that supersedes [pipeline_cron_trigger](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline_cron_trigger) added in #90 (the API used by `pipeline_cron_trigger` is limited; additional cron trigger options were recently added to the pipelines API, currently behind a feature flag) ## Notes Will be first released as part of `0.6.0-beta-1` (pre-release) ## Checklist * [x] _I have read [CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._ * [x] _I have [allowed changes to my fork to be made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._ * [x] _I have added tests, assuming new tests are warranted_. * [x] _I understand that the `/test` comment will be ignored by the CI trigger [unless it is made by a repo admin or collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._ --------- Co-authored-by: Yonatan Koren <[email protected]>
1 parent ab97b85 commit 887901f

File tree

8 files changed

+579
-46
lines changed

8 files changed

+579
-46
lines changed

client/pipeline.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ type Trigger struct {
6262
Variables []Variable `json:"variables,omitempty"`
6363
}
6464

65+
type CronTrigger struct {
66+
Name string `json:"name,omitempty"`
67+
Type string `json:"type,omitempty"`
68+
Expression string `json:"expression,omitempty"`
69+
Message string `json:"message,omitempty"`
70+
GitTriggerId string `json:"gitTriggerId,omitempty"`
71+
Branch string `json:"branch,omitempty"`
72+
Disabled bool `json:"disabled,omitempty"`
73+
Options *TriggerOptions `json:"options,omitempty"`
74+
RuntimeEnvironment *RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
75+
Variables []Variable `json:"variables,omitempty"`
76+
}
77+
6578
type TriggerOptions struct {
6679
NoCache bool `json:"noCache,omitempty"`
6780
NoCfCache bool `json:"noCfCache,omitempty"`
@@ -83,10 +96,17 @@ func (t *Trigger) SetVariables(variables map[string]interface{}) {
8396
}
8497
}
8598

99+
func (t *CronTrigger) SetVariables(variables map[string]interface{}) {
100+
for key, value := range variables {
101+
t.Variables = append(t.Variables, Variable{Key: key, Value: value.(string)})
102+
}
103+
}
104+
86105
type Spec struct {
87106
Variables []Variable `json:"variables,omitempty"`
88107
SpecTemplate *SpecTemplate `json:"specTemplate,omitempty"`
89108
Triggers []Trigger `json:"triggers,omitempty"`
109+
CronTriggers []CronTrigger `json:"cronTriggers,omitempty"`
90110
Priority int `json:"priority,omitempty"`
91111
Concurrency int `json:"concurrency,omitempty"`
92112
BranchConcurrency int `json:"branchConcurrency,omitempty"`

codefresh/resource_pipeline.go

Lines changed: 277 additions & 46 deletions
Large diffs are not rendered by default.

codefresh/resource_pipeline_cron_trigger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
func resourcePipelineCronTrigger() *schema.Resource {
1818
return &schema.Resource{
19+
DeprecationMessage: "This resource is deprecated and will be removed in a future version of the Codefresh Terraform provider. Please use the cron_triggers attribute of the codefresh_pipeline resource instead.",
1920
Description: "This resource is used to create cron-based triggers for pipeilnes.",
2021
Create: resourcePipelineCronTriggerCreate,
2122
Read: resourcePipelineCronTriggerRead,

codefresh/resource_pipeline_test.go

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,125 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
406406
})
407407
}
408408

409+
func TestAccCodefreshPipeline_CronTriggers(t *testing.T) {
410+
name := pipelineNamePrefix + acctest.RandString(10)
411+
resourceName := "codefresh_pipeline.test"
412+
var pipeline cfClient.Pipeline
413+
414+
resource.ParallelTest(t, resource.TestCase{
415+
PreCheck: func() { testAccPreCheck(t) },
416+
Providers: testAccProviders,
417+
CheckDestroy: testAccCheckCodefreshPipelineDestroy,
418+
Steps: []resource.TestStep{
419+
{
420+
Config: testAccCodefreshPipelineBasicConfigCronTriggers(
421+
name,
422+
"codefresh-contrib/react-sample-app",
423+
"./codefresh.yml",
424+
"master",
425+
"git",
426+
"cT1",
427+
"first",
428+
"0 0/1 * 1/1 * *",
429+
"64abd1550f02a62699b10df7",
430+
"runtime1",
431+
"100mb",
432+
"1cpu",
433+
"1gb",
434+
"1gb",
435+
"cT2",
436+
"second",
437+
"0 0/1 * 1/1 * *",
438+
"64abd1550f02a62699b10df7",
439+
true,
440+
true,
441+
true,
442+
true,
443+
"MY_VAR",
444+
"test",
445+
),
446+
Check: resource.ComposeTestCheckFunc(
447+
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
448+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.#", "2"),
449+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.name", "cT1"),
450+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.message", "first"),
451+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.expression", "0 0/1 * 1/1 * *"),
452+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.name", "runtime1"),
453+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.memory", "100mb"),
454+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.cpu", "1cpu"),
455+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.dind_storage", "1gb"),
456+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.required_available_storage", "1gb"),
457+
458+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.#", "2"),
459+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.name", "cT2"),
460+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.message", "second"),
461+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.expression", "0 0/1 * 1/1 * *"),
462+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.git_trigger_id", "64abd1550f02a62699b10df7"),
463+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.no_cache", "true"),
464+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.no_cf_cache", "true"),
465+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.reset_volume", "true"),
466+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.enable_notifications", "true"),
467+
),
468+
},
469+
{
470+
ResourceName: resourceName,
471+
ImportState: true,
472+
ImportStateVerify: true,
473+
},
474+
{
475+
Config: testAccCodefreshPipelineBasicConfigCronTriggers(
476+
name,
477+
"codefresh-contrib/react-sample-app",
478+
"./codefresh.yml",
479+
"master",
480+
"git",
481+
"cT1",
482+
"first-1",
483+
"0 0/1 * 1/1 * *",
484+
"00abd1550f02a62699b10df7",
485+
"runtime2",
486+
"500mb",
487+
"2cpu",
488+
"2gb",
489+
"3gb",
490+
"cT2",
491+
"second",
492+
"0 1/1 * 1/1 * *",
493+
"00abd1550f02a62699b10df7",
494+
true,
495+
true,
496+
false,
497+
false,
498+
"MY_VAR",
499+
"test",
500+
),
501+
Check: resource.ComposeTestCheckFunc(
502+
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
503+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.#", "2"),
504+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.name", "cT1"),
505+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.message", "first-1"),
506+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.expression", "0 0/1 * 1/1 * *"),
507+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.name", "runtime2"),
508+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.memory", "500mb"),
509+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.cpu", "2cpu"),
510+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.dind_storage", "2gb"),
511+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.required_available_storage", "3gb"),
512+
513+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.#", "2"),
514+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.name", "cT2"),
515+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.message", "second"),
516+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.expression", "0 1/1 * 1/1 * *"),
517+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.git_trigger_id", "00abd1550f02a62699b10df7"),
518+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.no_cache", "true"),
519+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.no_cf_cache", "true"),
520+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.reset_volume", "false"),
521+
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.enable_notifications", "false"),
522+
),
523+
},
524+
},
525+
})
526+
}
527+
409528
func TestAccCodefreshPipeline_Revision(t *testing.T) {
410529
name := pipelineNamePrefix + acctest.RandString(10)
411530
resourceName := "codefresh_pipeline.test"
@@ -901,6 +1020,116 @@ resource "codefresh_pipeline" "test" {
9011020
trigger2CommitStatusTitle)
9021021
}
9031022

1023+
func testAccCodefreshPipelineBasicConfigCronTriggers(
1024+
rName,
1025+
repo,
1026+
path,
1027+
revision,
1028+
context,
1029+
cronTrigger1Name string,
1030+
cronTrigger1Message string,
1031+
cronTrigger1Expression string,
1032+
cronTrigger1GitTriggerId string,
1033+
cronTrigger1REName string,
1034+
cronTrigger1REMemory string,
1035+
cronTrigger1RECpu string,
1036+
cronTrigger1REDindStorage string,
1037+
cronTrigger1RERequiredAvailableStorage string,
1038+
cronTrigger2Name string,
1039+
cronTrigger2Message string,
1040+
cronTrigger2Expression string,
1041+
cronTrigger2GitTriggerId string,
1042+
cronTrigger2NoCache bool,
1043+
cronTrigger2NoCfCache bool,
1044+
cronTrigger2ResetVolume bool,
1045+
cronTrigger2EnableNotifications bool,
1046+
cronTrigger2VarName string,
1047+
cronTrigger2VarValue string,
1048+
) string {
1049+
return fmt.Sprintf(`
1050+
resource "codefresh_pipeline" "test" {
1051+
1052+
lifecycle {
1053+
ignore_changes = [
1054+
revision
1055+
]
1056+
}
1057+
1058+
name = "%s"
1059+
1060+
spec {
1061+
spec_template {
1062+
repo = %q
1063+
path = %q
1064+
revision = %q
1065+
context = %q
1066+
}
1067+
1068+
cron_trigger {
1069+
name = %q
1070+
type = "cron"
1071+
branch = "main"
1072+
message = %q
1073+
expression = %q
1074+
git_trigger_id = %q
1075+
disabled = true
1076+
runtime_environment {
1077+
name = %q
1078+
memory = %q
1079+
cpu = %q
1080+
dind_storage = %q
1081+
required_available_storage = %q
1082+
}
1083+
}
1084+
1085+
cron_trigger {
1086+
name = %q
1087+
type = "cron"
1088+
branch = "master"
1089+
message = %q
1090+
expression = %q
1091+
git_trigger_id = %q
1092+
disabled = false
1093+
options {
1094+
no_cache = %t
1095+
no_cf_cache = %t
1096+
reset_volume = %t
1097+
enable_notifications = %t
1098+
}
1099+
variables = {
1100+
%q = %q
1101+
}
1102+
}
1103+
}
1104+
}
1105+
`,
1106+
rName,
1107+
repo,
1108+
path,
1109+
revision,
1110+
context,
1111+
cronTrigger1Name,
1112+
cronTrigger1Message,
1113+
cronTrigger1Expression,
1114+
cronTrigger1GitTriggerId,
1115+
cronTrigger1REName,
1116+
cronTrigger1REMemory,
1117+
cronTrigger1RECpu,
1118+
cronTrigger1REDindStorage,
1119+
cronTrigger1RERequiredAvailableStorage,
1120+
cronTrigger2Name,
1121+
cronTrigger2Message,
1122+
cronTrigger2Expression,
1123+
cronTrigger2GitTriggerId,
1124+
cronTrigger2NoCache,
1125+
cronTrigger2NoCfCache,
1126+
cronTrigger2ResetVolume,
1127+
cronTrigger2EnableNotifications,
1128+
cronTrigger2VarName,
1129+
cronTrigger2VarValue,
1130+
)
1131+
}
1132+
9041133
func testAccCodefreshPipelineBasicConfigRuntimeEnvironment(rName, repo, path, revision, context, runtimeName string) string {
9051134
return fmt.Sprintf(`
9061135
resource "codefresh_pipeline" "test" {

docs/resources/pipeline.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The central component of the Codefresh Platform. Pipelines are workflows that co
1111

1212
See the [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/introduction-to-codefresh-pipelines/) for the details.
1313

14+
~> **NOTE:** `cron_trigger` conflicts with the deprecated [codefresh_pipeline_cron_trigger](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline_cron_trigger) resource.
15+
1416
## Example Usage
1517

1618
```hcl
@@ -125,6 +127,7 @@ Optional:
125127
- `branch_concurrency` (Number) The maximum amount of concurrent builds that may run for each branch. Zero is unlimited (default: `0`).
126128
- `concurrency` (Number) The maximum amount of concurrent builds. Zero is unlimited (default: `0`).
127129
- `contexts` (List of String) A list of strings representing the contexts ([shared_configuration](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/shared-configuration/)) to be configured for the pipeline.
130+
- `cron_trigger` (Block List) The pipeline's cron triggers. Conflicts with the deprecated [codefresh_pipeline_cron_trigger](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline_cron_trigger) resource. (see [below for nested schema](#nestedblock--spec--cron_trigger))
128131
- `options` (Block List, Max: 1) The options for the pipeline. (see [below for nested schema](#nestedblock--spec--options))
129132
- `pack_id` (String) SAAS pack (`5cd1746617313f468d669013` for Small; `5cd1746717313f468d669014` for Medium; `5cd1746817313f468d669015` for Large; `5cd1746817313f468d669017` for XL; `5cd1746817313f468d669018` for XXL).
130133
- `priority` (Number) Helps to organize the order of builds execution in case of reaching the concurrency limit (default: `0`).
@@ -136,6 +139,49 @@ Optional:
136139
- `trigger_concurrency` (Number) The maximum amount of concurrent builds that may run for each trigger (default: `0`).
137140
- `variables` (Map of String) The pipeline's variables.
138141

142+
<a id="nestedblock--spec--cron_trigger"></a>
143+
### Nested Schema for `spec.cron_trigger`
144+
145+
Required:
146+
147+
- `expression` (String)
148+
- `message` (String)
149+
- `name` (String) The name of the cron trigger.
150+
151+
Optional:
152+
153+
- `branch` (String) Branch that should be passed for build triggered by this cron trigger.
154+
- `disabled` (Boolean) Flag to disable the trigger.
155+
- `git_trigger_id` (String) Related git-trigger id. Will by used to take all possible git information by branch.
156+
- `options` (Block List) The trigger's options. (see [below for nested schema](#nestedblock--spec--cron_trigger--options))
157+
- `runtime_environment` (Block List) The runtime environment for the trigger. (see [below for nested schema](#nestedblock--spec--cron_trigger--runtime_environment))
158+
- `type` (String) The type of the trigger (default: `cron`; see notes above).
159+
- `variables` (Map of String) Trigger variables.
160+
161+
<a id="nestedblock--spec--cron_trigger--options"></a>
162+
### Nested Schema for `spec.cron_trigger.options`
163+
164+
Optional:
165+
166+
- `enable_notifications` (Boolean) If false the pipeline will not send notifications to Slack and status updates back to the Git provider.
167+
- `no_cache` (Boolean) If true, docker layer cache is disabled.
168+
- `no_cf_cache` (Boolean) If true, extra Codefresh caching is disabled.
169+
- `reset_volume` (Boolean) If true, all files on volume will be deleted before each execution.
170+
171+
172+
<a id="nestedblock--spec--cron_trigger--runtime_environment"></a>
173+
### Nested Schema for `spec.cron_trigger.runtime_environment`
174+
175+
Optional:
176+
177+
- `cpu` (String) The CPU allocated to the runtime environment.
178+
- `dind_storage` (String) The storage allocated to the runtime environment.
179+
- `memory` (String) The memory allocated to the runtime environment.
180+
- `name` (String) The name of the runtime environment.
181+
- `required_available_storage` (String) Minimum disk space required for build filesystem ( unit Gi is required).
182+
183+
184+
139185
<a id="nestedblock--spec--options"></a>
140186
### Nested Schema for `spec.options`
141187

docs/resources/pipeline_cron_trigger.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This resource is used to create cron-based triggers for pipeilnes.
1111

1212
See the [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/cron-triggers/).
1313

14+
~> **DEPRECATED:** This resource is being deprecated in favor of the `cron_trigger` attribute of the [codefresh_pipeline](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline) resource.
15+
1416
## Example usage
1517

1618
```hcl

templates/resources/pipeline.md.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ description: |-
1111

1212
See the [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/introduction-to-codefresh-pipelines/) for the details.
1313

14+
~> **NOTE:** `cron_trigger` conflicts with the deprecated [codefresh_pipeline_cron_trigger](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline_cron_trigger) resource.
15+
1416
## Example Usage
1517

1618
```hcl

templates/resources/pipeline_cron_trigger.md.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ description: |-
1111

1212
See the [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/cron-triggers/).
1313

14+
~> **DEPRECATED:** This resource is being deprecated in favor of the `cron_trigger` attribute of the [codefresh_pipeline](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline) resource.
15+
1416
## Example usage
1517

1618
```hcl

0 commit comments

Comments
 (0)