Skip to content

Commit f8eb752

Browse files
jamielennoxrosbo
authored andcommitted
Allow using in repo configuration for cloudbuild trigger (#1557)
* Allow using in repo configuration for cloudbuild trigger Cloudbuild triggers have a complex configuration that can be defined from the API. When using the console, the more typical way of doing this is to defined the configuration within the repository and point the configuration to the file that defines the config. This can be supported by sending the filename parameter instead of the build parameter, however only one can be sent. * Acceptance testing for cloudbuild trigger with filename Ensure that when a cloudbuild repo trigger is created with a filename, that filename is what actually ends up in the cloud. * Don't specify "by default" in cloudbuild-trigger. The docs shouldn't say that "cloudbuild.yaml" is used by default. There is no default from the APIs, but the console suggest using this value. Just say it's the typical value in documentation.
1 parent 5bc1050 commit f8eb752

File tree

3 files changed

+135
-11
lines changed

3 files changed

+135
-11
lines changed

google/resource_cloudbuild_build_trigger.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ func resourceCloudBuildTrigger() *schema.Resource {
3636
Computed: true,
3737
ForceNew: true,
3838
},
39+
"filename": &schema.Schema{
40+
Type: schema.TypeString,
41+
Optional: true,
42+
ForceNew: true,
43+
ConflictsWith: []string{"build"},
44+
},
3945
"build": {
4046
Type: schema.TypeList,
4147
Description: "Contents of the build template.",
@@ -142,7 +148,12 @@ func resourceCloudbuildBuildTriggerCreate(d *schema.ResourceData, meta interface
142148
buildTrigger.Description = v.(string)
143149
}
144150

145-
buildTrigger.Build = expandCloudbuildBuildTriggerBuild(d)
151+
if v, ok := d.GetOk("filename"); ok {
152+
buildTrigger.Filename = v.(string)
153+
} else {
154+
buildTrigger.Build = expandCloudbuildBuildTriggerBuild(d)
155+
}
156+
146157
buildTrigger.TriggerTemplate = expandCloudbuildBuildTriggerTemplate(d, project)
147158

148159
tstr, err := json.Marshal(buildTrigger)
@@ -179,7 +190,10 @@ func resourceCloudbuildBuildTriggerRead(d *schema.ResourceData, meta interface{}
179190
if buildTrigger.TriggerTemplate != nil {
180191
d.Set("trigger_template", flattenCloudbuildBuildTriggerTemplate(d, config, buildTrigger.TriggerTemplate))
181192
}
182-
if buildTrigger.Build != nil {
193+
194+
if buildTrigger.Filename != "" {
195+
d.Set("filename", buildTrigger.Filename)
196+
} else if buildTrigger.Build != nil {
183197
d.Set("build", flattenCloudbuildBuildTriggerBuild(d, config, buildTrigger.Build))
184198
}
185199

google/resource_cloudbuild_build_trigger_test.go

+100-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/hashicorp/terraform/helper/acctest"
88
"github.com/hashicorp/terraform/helper/resource"
99
"github.com/hashicorp/terraform/terraform"
10+
cloudbuild "google.golang.org/api/cloudbuild/v1"
1011
)
1112

1213
func TestAccCloudBuildTrigger_basic(t *testing.T) {
@@ -37,24 +38,80 @@ func TestAccCloudBuildTrigger_basic(t *testing.T) {
3738
})
3839
}
3940

41+
func TestAccCloudBuildTrigger_filename(t *testing.T) {
42+
t.Parallel()
43+
44+
projectID := "terraform-" + acctest.RandString(10)
45+
projectOrg := getTestOrgFromEnv(t)
46+
projectBillingAccount := getTestBillingAccountFromEnv(t)
47+
48+
resource.Test(t, resource.TestCase{
49+
PreCheck: func() { testAccPreCheck(t) },
50+
Providers: testAccProviders,
51+
CheckDestroy: testAccCheckGoogleCloudBuildTriggerVersionsDestroyed,
52+
Steps: []resource.TestStep{
53+
resource.TestStep{
54+
Config: testGoogleCloudBuildTrigger_filename(projectID, projectOrg, projectBillingAccount),
55+
Check: resource.ComposeTestCheckFunc(
56+
testAccCheckGoogleCloudFilenameConfig("google_cloudbuild_trigger.filename_build_trigger"),
57+
),
58+
},
59+
resource.TestStep{
60+
Config: testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount),
61+
Check: resource.ComposeTestCheckFunc(
62+
testAccCheckGoogleCloudBuildTriggerWasRemovedFromState("google_cloudbuild_trigger.filename_build_trigger"),
63+
),
64+
},
65+
},
66+
})
67+
68+
}
69+
70+
func testAccGetBuildTrigger(s *terraform.State, resourceName string) (*cloudbuild.BuildTrigger, error) {
71+
rs, ok := s.RootModule().Resources[resourceName]
72+
if !ok {
73+
return nil, fmt.Errorf("Resource not found: %s", resourceName)
74+
}
75+
76+
if rs.Primary.ID == "" {
77+
return nil, fmt.Errorf("No ID is set")
78+
}
79+
80+
config := testAccProvider.Meta().(*Config)
81+
project := rs.Primary.Attributes["project"]
82+
83+
trigger, err := config.clientBuild.Projects.Triggers.Get(project, rs.Primary.ID).Do()
84+
if err != nil {
85+
return nil, fmt.Errorf("Trigger does not exist")
86+
}
87+
88+
return trigger, nil
89+
}
90+
4091
func testAccCheckGoogleCloudBuildTriggerExists(resourceName string) resource.TestCheckFunc {
4192
return func(s *terraform.State) error {
93+
_, err := testAccGetBuildTrigger(s, resourceName)
4294

43-
rs, ok := s.RootModule().Resources[resourceName]
44-
if !ok {
45-
return fmt.Errorf("Resource not found: %s", resourceName)
95+
if err != nil {
96+
return fmt.Errorf("Trigger does not exist")
4697
}
4798

48-
if rs.Primary.ID == "" {
49-
return fmt.Errorf("No ID is set")
50-
}
51-
config := testAccProvider.Meta().(*Config)
52-
project := rs.Primary.Attributes["project"]
99+
return nil
100+
}
101+
}
102+
103+
func testAccCheckGoogleCloudFilenameConfig(resourceName string) resource.TestCheckFunc {
104+
return func(s *terraform.State) error {
105+
trigger, err := testAccGetBuildTrigger(s, resourceName)
53106

54-
_, err := config.clientBuild.Projects.Triggers.Get(project, rs.Primary.ID).Do()
55107
if err != nil {
56108
return fmt.Errorf("Trigger does not exist")
57109
}
110+
111+
if trigger.Filename != "cloudbuild.yaml" {
112+
return fmt.Errorf("Config filename mismatch: %s", trigger.Filename)
113+
}
114+
58115
return nil
59116
}
60117
}
@@ -147,6 +204,40 @@ resource "google_cloudbuild_trigger" "build_trigger" {
147204
`, projectID, projectID, projectOrg, projectBillingAccount)
148205
}
149206

207+
func testGoogleCloudBuildTrigger_filename(projectID, projectOrg, projectBillingAccount string) string {
208+
return fmt.Sprintf(`
209+
resource "google_project" "acceptance" {
210+
name = "%s"
211+
project_id = "%s"
212+
org_id = "%s"
213+
billing_account = "%s"
214+
}
215+
216+
resource "google_project_services" "acceptance" {
217+
project = "${google_project.acceptance.project_id}"
218+
219+
services = [
220+
"cloudbuild.googleapis.com",
221+
"containerregistry.googleapis.com",
222+
"logging.googleapis.com",
223+
"pubsub.googleapis.com",
224+
"storage-api.googleapis.com",
225+
]
226+
}
227+
228+
resource "google_cloudbuild_trigger" "filename_build_trigger" {
229+
project = "${google_project_services.acceptance.project}"
230+
description = "acceptance test build trigger"
231+
trigger_template {
232+
branch_name = "master"
233+
project = "${google_project_services.acceptance.project}"
234+
repo_name = "some-repo"
235+
}
236+
filename = "cloudbuild.yaml"
237+
}
238+
`, projectID, projectID, projectOrg, projectBillingAccount)
239+
}
240+
150241
func testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount string) string {
151242
return fmt.Sprintf(`
152243
resource "google_project" "acceptance" {

website/docs/r/cloudbuild_trigger.html.markdown

+19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ resource "google_cloudbuild_trigger" "build_trigger" {
3333
}
3434
```
3535

36+
OR
37+
38+
```hcl
39+
resource "google_cloudbuild_trigger" "build_trigger" {
40+
project = "my-project"
41+
trigger_template {
42+
branch_name = "master"
43+
project = "my-project"
44+
repo_name = "some-repo"
45+
}
46+
filename = "cloudbuild.yaml"
47+
}
48+
```
49+
50+
3651
## Argument Reference
3752

3853
(Argument descriptions sourced from https://godoc.org/google.golang.org/api/cloudbuild/v1#BuildTrigger)
@@ -59,6 +74,10 @@ will be expanded when the build is created:
5974
or resolved from the specified branch or tag.
6075
* `$SHORT_SHA`: first 7 characters of `$REVISION_ID` or `$COMMIT_SHA`.
6176

77+
* `filename` - (Optional) Specify the path to a Cloud Build configuration file
78+
in the Git repo. This is mutually exclusive with `build`. This is typically
79+
`cloudbuild.yaml` however it can be specified by the user.
80+
6281
---
6382

6483
The `trigger_template` block supports:

0 commit comments

Comments
 (0)