Skip to content

Commit

Permalink
CloudBuild Trigger substitutions (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson authored Jul 25, 2018
1 parent 0fdc262 commit aaf18da
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
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 {
_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

0 comments on commit aaf18da

Please sign in to comment.