Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudBuild Trigger substitutions #1810

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion google/resource_cloudbuild_build_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func resourceCloudBuildTrigger() *schema.Resource {
Read: resourceCloudbuildBuildTriggerRead,
Delete: resourceCloudbuildBuildTriggerDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceCloudBuildTriggerImportState,
},

Timeouts: &schema.ResourceTimeout{
Expand Down Expand Up @@ -89,6 +89,12 @@ func resourceCloudBuildTrigger() *schema.Resource {
Optional: true,
ForceNew: true,
},
"substitutions": &schema.Schema{
Optional: true,
Type: schema.TypeMap,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"trigger_template": &schema.Schema{
Optional: true,
Type: schema.TypeList,
Expand Down Expand Up @@ -155,6 +161,7 @@ func resourceCloudbuildBuildTriggerCreate(d *schema.ResourceData, meta interface
}

buildTrigger.TriggerTemplate = expandCloudbuildBuildTriggerTemplate(d, project)
buildTrigger.Substitutions = expandStringMap(d, "substitutions")

tstr, err := json.Marshal(buildTrigger)
if err != nil {
Expand Down Expand Up @@ -186,6 +193,7 @@ func resourceCloudbuildBuildTriggerRead(d *schema.ResourceData, meta interface{}
}

d.Set("description", buildTrigger.Description)
d.Set("substitutions", buildTrigger.Substitutions)

if buildTrigger.TriggerTemplate != nil {
d.Set("trigger_template", flattenCloudbuildBuildTriggerTemplate(d, config, buildTrigger.TriggerTemplate))
Expand Down Expand Up @@ -312,3 +320,17 @@ func resourceCloudbuildBuildTriggerDelete(d *schema.ResourceData, meta interface
d.SetId("")
return nil
}

func resourceCloudBuildTriggerImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")

if len(parts) == 1 {
return []*schema.ResourceData{d}, nil
} else if len(parts) == 2 {
d.Set("project", parts[0])
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
} else {
return nil, fmt.Errorf("Invalid import id %q. Expecting {trigger_name} or {project}/{trigger_name}", d.Id())
}
}
10 changes: 10 additions & 0 deletions google/resource_cloudbuild_build_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func TestAccCloudBuildTrigger_basic(t *testing.T) {
testAccCheckGoogleCloudBuildTriggerExists("google_cloudbuild_trigger.build_trigger"),
),
},
resource.TestStep{
ResourceName: "google_cloudbuild_trigger.build_trigger",
ImportState: true,
ImportStateVerify: true,
ImportStateIdPrefix: fmt.Sprintf("%s/", projectID),
},
resource.TestStep{
Config: testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -233,6 +239,10 @@ resource "google_cloudbuild_trigger" "filename_build_trigger" {
project = "${google_project_services.acceptance.project}"
repo_name = "some-repo"
}
substitutions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - this indent is wrong. Also, these substitutions seem to break the first rule that says "uppercase letters only/must start with _", which I would assume would make the tests fail. Do they actually work and docs are just lying?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They work, and the docs aren't lying - if you tried to use this trigger, it would break. While I'm here I'll go ahead and fix that.

foo = "bar"
baz = "qux"
}
filename = "cloudbuild.yaml"
}
`, projectID, projectID, projectOrg, projectBillingAccount)
Expand Down
16 changes: 16 additions & 0 deletions website/docs/r/cloudbuild_trigger.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ will be expanded when the build is created:
in the Git repo. This is mutually exclusive with `build`. This is typically
`cloudbuild.yaml` however it can be specified by the user.

* `substitutions`: (Optional) User-defined substitutions.
User-defined substitutions must conform to the following rules:
* Substitutions must begin with an underscore (`_`) and use only
uppercase-letters and numbers (respecting the regular expression
`_[A-Z0-9_]+`). This prevents conflicts with built-in substitutions.
* Unmatched keys in the template will cause an error (for example, if a build
request includes `$_FOO` and the substitutions map doesn’t define `_FOO`).
* Unmatched keys in the parameters list will result in an error (for example,
if a substitutions map defines `_FOO` but the build request doesn't include `$_FOO`).
* To include a literal `$_VARIABLE` in the template, you must escape with `$$`.
* You can explicitly denote variable expansion using the `${_VAR}` syntax. This prevents
ambiguity in cases like `${_FOO}BAR`, where `$_FOO` is a variable.
* The number of parameters is limited to 100 parameters.
* The length of a parameter key and the length of a parameter value
are limited to 100 characters.

---

The `trigger_template` block supports:
Expand Down