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

Add cloudbuild invert regex option #2171

Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/3628.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
cloudbuild: Added invert_regex flag in Github PullRequestFilter and PushFilter + in triggerTemplate
```
66 changes: 66 additions & 0 deletions google-beta/resource_cloud_build_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ https://github.com/googlecloudplatform/cloud-builders is "googlecloudplatform".`
ValidateFunc: validation.StringInSlice([]string{"COMMENTS_DISABLED", "COMMENTS_ENABLED", ""}, false),
Description: `Whether to block builds on a "/gcbrun" comment from a repository owner or collaborator. Possible values: ["COMMENTS_DISABLED", "COMMENTS_ENABLED"]`,
},
"invert_regex": {
Type: schema.TypeBool,
Optional: true,
Description: `If true, branches that do NOT match the git_ref will trigger a build.`,
},
},
},
ExactlyOneOf: []string{"github.0.pull_request", "github.0.push"},
Expand All @@ -345,6 +350,11 @@ https://github.com/googlecloudplatform/cloud-builders is "googlecloudplatform".`
Description: `Regex of branches to match. Specify only one of branch or tag.`,
ExactlyOneOf: []string{"github.0.push.0.branch", "github.0.push.0.tag"},
},
"invert_regex": {
Type: schema.TypeBool,
Optional: true,
Description: `When true, only trigger a build if the revision regex does NOT match the git_ref regex.`,
},
"tag": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -440,6 +450,11 @@ This must be a relative path. If a step's dir is specified and
is an absolute path, this value is ignored for that step's
execution.`,
},
"invert_regex": {
Type: schema.TypeBool,
Optional: true,
Description: `Only trigger a build if the revision regex does NOT match the revision regex.`,
},
"project_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -833,6 +848,8 @@ func flattenCloudBuildTriggerTriggerTemplate(v interface{}, d *schema.ResourceDa
flattenCloudBuildTriggerTriggerTemplateRepoName(original["repoName"], d, config)
transformed["dir"] =
flattenCloudBuildTriggerTriggerTemplateDir(original["dir"], d, config)
transformed["invert_regex"] =
flattenCloudBuildTriggerTriggerTemplateInvertRegex(original["invertRegex"], d, config)
transformed["branch_name"] =
flattenCloudBuildTriggerTriggerTemplateBranchName(original["branchName"], d, config)
transformed["tag_name"] =
Expand All @@ -853,6 +870,10 @@ func flattenCloudBuildTriggerTriggerTemplateDir(v interface{}, d *schema.Resourc
return v
}

func flattenCloudBuildTriggerTriggerTemplateInvertRegex(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerTriggerTemplateBranchName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -905,6 +926,8 @@ func flattenCloudBuildTriggerGithubPullRequest(v interface{}, d *schema.Resource
flattenCloudBuildTriggerGithubPullRequestBranch(original["branch"], d, config)
transformed["comment_control"] =
flattenCloudBuildTriggerGithubPullRequestCommentControl(original["commentControl"], d, config)
transformed["invert_regex"] =
flattenCloudBuildTriggerGithubPullRequestInvertRegex(original["invertRegex"], d, config)
return []interface{}{transformed}
}
func flattenCloudBuildTriggerGithubPullRequestBranch(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Expand All @@ -915,6 +938,10 @@ func flattenCloudBuildTriggerGithubPullRequestCommentControl(v interface{}, d *s
return v
}

func flattenCloudBuildTriggerGithubPullRequestInvertRegex(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerGithubPush(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand All @@ -924,12 +951,18 @@ func flattenCloudBuildTriggerGithubPush(v interface{}, d *schema.ResourceData, c
return nil
}
transformed := make(map[string]interface{})
transformed["invert_regex"] =
flattenCloudBuildTriggerGithubPushInvertRegex(original["invertRegex"], d, config)
transformed["branch"] =
flattenCloudBuildTriggerGithubPushBranch(original["branch"], d, config)
transformed["tag"] =
flattenCloudBuildTriggerGithubPushTag(original["tag"], d, config)
return []interface{}{transformed}
}
func flattenCloudBuildTriggerGithubPushInvertRegex(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerGithubPushBranch(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -1129,6 +1162,13 @@ func expandCloudBuildTriggerTriggerTemplate(v interface{}, d TerraformResourceDa
transformed["dir"] = transformedDir
}

transformedInvertRegex, err := expandCloudBuildTriggerTriggerTemplateInvertRegex(original["invert_regex"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedInvertRegex); val.IsValid() && !isEmptyValue(val) {
transformed["invertRegex"] = transformedInvertRegex
}

transformedBranchName, err := expandCloudBuildTriggerTriggerTemplateBranchName(original["branch_name"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1165,6 +1205,10 @@ func expandCloudBuildTriggerTriggerTemplateDir(v interface{}, d TerraformResourc
return v, nil
}

func expandCloudBuildTriggerTriggerTemplateInvertRegex(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerTriggerTemplateBranchName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1248,6 +1292,13 @@ func expandCloudBuildTriggerGithubPullRequest(v interface{}, d TerraformResource
transformed["commentControl"] = transformedCommentControl
}

transformedInvertRegex, err := expandCloudBuildTriggerGithubPullRequestInvertRegex(original["invert_regex"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedInvertRegex); val.IsValid() && !isEmptyValue(val) {
transformed["invertRegex"] = transformedInvertRegex
}

return transformed, nil
}

Expand All @@ -1259,6 +1310,10 @@ func expandCloudBuildTriggerGithubPullRequestCommentControl(v interface{}, d Ter
return v, nil
}

func expandCloudBuildTriggerGithubPullRequestInvertRegex(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerGithubPush(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand All @@ -1268,6 +1323,13 @@ func expandCloudBuildTriggerGithubPush(v interface{}, d TerraformResourceData, c
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedInvertRegex, err := expandCloudBuildTriggerGithubPushInvertRegex(original["invert_regex"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedInvertRegex); val.IsValid() && !isEmptyValue(val) {
transformed["invertRegex"] = transformedInvertRegex
}

transformedBranch, err := expandCloudBuildTriggerGithubPushBranch(original["branch"], d, config)
if err != nil {
return nil, err
Expand All @@ -1285,6 +1347,10 @@ func expandCloudBuildTriggerGithubPush(v interface{}, d TerraformResourceData, c
return transformed, nil
}

func expandCloudBuildTriggerGithubPushInvertRegex(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerGithubPushBranch(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
2 changes: 2 additions & 0 deletions google-beta/resource_cloudbuild_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ resource "google_cloudbuild_trigger" "build_trigger" {
trigger_template {
branch_name = "master"
repo_name = "some-repo"
invert_regex = false
}
build {
images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA"]
Expand Down Expand Up @@ -221,6 +222,7 @@ resource "google_cloudbuild_trigger" "build_trigger" {
trigger_template {
branch_name = "master-updated"
repo_name = "some-repo-updated"
invert_regex = true
}
build {
images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA"]
Expand Down
12 changes: 12 additions & 0 deletions website/docs/r/cloudbuild_trigger.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ The `trigger_template` block supports:
is an absolute path, this value is ignored for that step's
execution.

* `invert_regex` -
(Optional)
Only trigger a build if the revision regex does NOT match the revision regex.

* `branch_name` -
(Optional)
Name of the branch to build. Exactly one a of branch name, tag, or commit SHA must be provided.
Expand Down Expand Up @@ -194,8 +198,16 @@ The `pull_request` block supports:
* `COMMENTS_DISABLED`
* `COMMENTS_ENABLED`

* `invert_regex` -
(Optional)
If true, branches that do NOT match the git_ref will trigger a build.

The `push` block supports:

* `invert_regex` -
(Optional)
When true, only trigger a build if the revision regex does NOT match the git_ref regex.

* `branch` -
(Optional)
Regex of branches to match. Specify only one of branch or tag.
Expand Down