Skip to content
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
49 changes: 47 additions & 2 deletions gcp/cloud-cloudbuild-trigger/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ resource "google_cloudbuild_trigger" "trigger_main" {
owner = var.repository_owner
name = var.repository_name
push {
branch = var.branching_strategy[var.environment]["provision"]["branch"]
invert_regex = var.branching_strategy[var.environment]["provision"]["invert_regex"]
branch = local.branching_strategy[var.environment]["provision"]["branch"]
invert_regex = local.branching_strategy[var.environment]["provision"]["invert_regex"]
}
}
service_account = var.trigger_service_account != "" ? "projects/${data.google_project.current.project_id}/serviceAccounts/${var.trigger_service_account}" : null
Expand All @@ -22,3 +22,48 @@ resource "google_cloudbuild_trigger" "trigger_main" {
ignored_files = var.exclude
disabled = var.disabled
}

locals {
branching_strategy = coalesce(var.branching_strategy, {
dev = {
Comment thread
jbadavis marked this conversation as resolved.
validate = {
branch = "^NOT_USED_PREVIEW$"
invert_regex = false
}
provision = {
branch = ".*"
invert_regex = false
}
},
preview = {
validate = {
branch = "^NOT_USED_PREVIEW$"
invert_regex = false
}
provision = {
branch = ".*"
invert_regex = false
}
},
preprod = {
validate = {
branch = "^main$|^preprod$|^release/(.*)$"
invert_regex = true
}
provision = {
branch = "^main$|^preprod$|^release/(.*)$"
invert_regex = false
}
},
prod = {
validate = {
branch = "^main$"
invert_regex = true
}
provision = {
branch = "^main$"
invert_regex = false
}
}
})
}
59 changes: 14 additions & 45 deletions gcp/cloud-cloudbuild-trigger/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -71,54 +71,23 @@ variable "environment" {
}
}


variable "branching_strategy" {
description = "Branching strategy for different environments"
Comment thread
jbadavis marked this conversation as resolved.
type = map(any)
default = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

By removing this default values, won't this be affecting triggers created with this module ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The default is now specified in locals:

https://github.com/NandosUK/infrastructure-terraform-modules/pull/98/changes#diff-129fd87e2e7e1569396ba98d00bbc72bcdacb88809af686031ae44176c2ef365R27

If no variable is passed it defaults to null and the second arg of the coalesce function is used.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, all good. It looks like the default values before where slightly different to the ones in the variables. All good now

dev = {
validate = {
branch = "^NOT_USED_PREVIEW$"
invert_regex = false
}
provision = {
branch = ".*"
invert_regex = false
}
},
preview = {
validate = {
branch = "^NOT_USED_PREVIEW$"
invert_regex = false
}
provision = {
branch = ".*"
invert_regex = false
}
},
preprod = {
validate = {
branch = "^main$|^preprod$|^release/(.*)$"
invert_regex = true
}
provision = {
branch = "^main$|^preprod$|^release/(.*)$"
invert_regex = false
}
},
prod = {
validate = {
branch = "^main$"
invert_regex = true
}
provision = {
branch = "^main$"
invert_regex = false
}
}
}
}

type = map(object({
validate = object({
branch = string
invert_regex = bool
})
provision = object({
branch = string
invert_regex = bool
})
}))

default = null
nullable = true
}
Comment thread
jbadavis marked this conversation as resolved.

# Tags
variable "tags" {
Expand Down
24 changes: 12 additions & 12 deletions gcp/cloud-run-v2-job/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ resource "google_project_iam_member" "sa_run_invoke" {

# Cloud Build trigger configuration
module "trigger_provision" {
count = var.create_trigger == true ? 1 : 0
source = "../cloud-cloudbuild-trigger"
name = "service-${var.name}-job-provision"
repository_name = var.repository_name
location = var.location
description = "Provision ${var.name} Job (CI/CD)"
filename = "${var.service_path}/cloudbuild.yaml"
include = concat(["${var.service_path}/**"], var.dependencies)
exclude = ["${var.service_path}/functions/**"]
environment = var.environment
project_id = var.project_id

count = var.create_trigger == true ? 1 : 0
source = "../cloud-cloudbuild-trigger"
name = "service-${var.name}-job-provision"
repository_name = var.repository_name
location = var.location
description = "Provision ${var.name} Job (CI/CD)"
filename = "${var.service_path}/cloudbuild.yaml"
include = concat(["${var.service_path}/**"], var.dependencies)
exclude = ["${var.service_path}/functions/**"]
environment = var.environment
project_id = var.project_id
branching_strategy = var.trigger_branching_strategy
trigger_service_account = var.trigger_service_account

# Substitution variables for Cloud Build Trigger
Expand Down
19 changes: 19 additions & 0 deletions gcp/cloud-run-v2-job/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,22 @@ variable "trigger_service_account" {
type = string
default = ""
}

variable "trigger_branching_strategy" {
description = "Branching strategy for the Cloud Build trigger."

type = map(object({
validate = object({
branch = string
invert_regex = bool
})
provision = object({
branch = string
invert_regex = bool
})
}))

default = null
nullable = true
}