Skip to content

Commit 06dcda6

Browse files
authored
Merge pull request #1 from oun/feature/cloud-build-service-account
feat: support custom service account for cloud build
2 parents d2e85d6 + 7aca72c commit 06dcda6

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

.github/workflows/release.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Release
2+
on:
3+
push:
4+
branches:
5+
- main
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- name: Bump version and push tag
12+
id: tag_version
13+
uses: mathieudutour/[email protected]
14+
with:
15+
github_token: ${{ secrets.GITHUB_TOKEN }}
16+
- name: Create a GitHub release
17+
uses: ncipollo/release-action@v1
18+
with:
19+
tag: ${{ steps.tag_version.outputs.new_tag }}
20+
name: Release ${{ steps.tag_version.outputs.new_tag }}
21+
body: ${{ steps.tag_version.outputs.changelog }}

terraform/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Then perform the following commands:
4848

4949
| Name | Description | Type | Default | Required |
5050
|------|-------------|------|---------|:--------:|
51+
| <a name="input_cloud_build_service_account"></a> [cloud\_build\_service\_account](#input\_cloud\_build\_service\_account) | The fully-qualified name of the custom cloud build service account. | `string` | `null` | no |
52+
| <a name="input_cloud_build_service_account_iam_roles"></a> [cloud\_build\_service\_account\_iam\_roles](#input\_cloud\_build\_service\_account\_iam\_roles) | IAM roles for custom cloud build service account | `list(string)` | <pre>[<br> "roles/logging.logWriter",<br> "roles/artifactregistry.writer",<br> "roles/storage.objectViewer"<br>]</pre> | no |
53+
| <a name="input_cloud_build_service_account_id"></a> [cloud\_build\_service\_account\_id](#input\_cloud\_build\_service\_account\_id) | The name of the service account that will be created if create\_cloud\_build\_service\_account is true. | `string` | `"sa-gcf"` | no |
54+
| <a name="input_create_cloud_build_service_account"></a> [create\_cloud\_build\_service\_account](#input\_create\_cloud\_build\_service\_account) | If the custom cloud build service account should be created. | `bool` | `true` | no |
5155
| <a name="input_create_trigger_service_account"></a> [create\_trigger\_service\_account](#input\_create\_trigger\_service\_account) | If the service account to trigger function should be created. | `bool` | `true` | no |
5256
| <a name="input_function_labels"></a> [function\_labels](#input\_function\_labels) | A set of key/value label pairs to assign to the function. | `map(string)` | `{}` | no |
5357
| <a name="input_gce_function_config"></a> [gce\_function\_config](#input\_gce\_function\_config) | The settings for start and stop Compute instances function. | <pre>object({<br> enabled = optional(bool, true)<br> create_service_account = optional(bool, true)<br> service_account_id = optional(string, "sa-start-stop-gce-function")<br> service_account_email = optional(string)<br> timeout = optional(number)<br> available_memory = optional(string)<br> max_instance_count = optional(number)<br> })</pre> | <pre>{<br> "enabled": false<br>}</pre> | no |

terraform/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ locals {
33
pubsub_messages = [for index, schedule in var.schedules : jsonencode({ project = local.scheduled_projects[index], labels = schedule.resource_labels })]
44
pubsub_attributes = [for index, schedule in var.schedules : { for type in schedule.resource_types : type => "true" }]
55

6+
cloud_build_sa = var.create_cloud_build_service_account ? google_service_account.gcf_sa[0].id : var.cloud_build_service_account
67
trigger_sa_email = var.create_trigger_service_account ? google_service_account.trigger_sa[0].email : var.trigger_service_account_email
78
gce_function_sa_email = var.gce_function_config.enabled && var.gce_function_config.create_service_account ? google_service_account.gce_function_sa[0].email : var.gce_function_config.service_account_email
89
sql_function_sa_email = var.sql_function_config.enabled && var.sql_function_config.create_service_account ? google_service_account.sql_function_sa[0].email : var.sql_function_config.service_account_email
@@ -70,6 +71,14 @@ resource "google_storage_bucket" "default" {
7071
uniform_bucket_level_access = true
7172
}
7273

74+
resource "google_service_account" "gcf_sa" {
75+
count = var.create_cloud_build_service_account ? 1 : 0
76+
77+
account_id = var.cloud_build_service_account_id
78+
display_name = "Service Account for building cloud run function"
79+
project = var.project_id
80+
}
81+
7382
resource "google_service_account" "trigger_sa" {
7483
count = var.create_trigger_service_account ? 1 : 0
7584

@@ -162,6 +171,14 @@ resource "google_cloud_run_service_iam_member" "stop_gke_function" {
162171
member = "serviceAccount:${local.trigger_sa_email}"
163172
}
164173

174+
resource "google_project_iam_member" "gcf_sa" {
175+
for_each = var.create_cloud_build_service_account ? toset(var.cloud_build_service_account_iam_roles) : []
176+
177+
project = var.project_id
178+
role = each.value
179+
member = google_service_account.gcf_sa[0].member
180+
}
181+
165182
resource "google_project_iam_member" "gce_function" {
166183
for_each = var.gce_function_config.enabled && var.gce_function_config.create_service_account ? toset(local.scheduled_projects) : []
167184

@@ -198,6 +215,7 @@ module "function_start_gce_instances" {
198215
entry_point = "startInstances"
199216
pubsub_topic = google_pubsub_topic.start_topic.id
200217
service_account_email = local.gce_function_sa_email
218+
build_service_account = local.cloud_build_sa
201219
timeout = var.gce_function_config.timeout
202220
available_memory = var.gce_function_config.available_memory
203221
max_instance_count = var.gce_function_config.max_instance_count
@@ -218,6 +236,7 @@ module "function_stop_gce_instances" {
218236
entry_point = "stopInstances"
219237
pubsub_topic = google_pubsub_topic.stop_topic.id
220238
service_account_email = local.gce_function_sa_email
239+
build_service_account = local.cloud_build_sa
221240
timeout = var.gce_function_config.timeout
222241
available_memory = var.gce_function_config.available_memory
223242
max_instance_count = var.gce_function_config.max_instance_count
@@ -238,6 +257,7 @@ module "function_start_sql_instances" {
238257
entry_point = "startInstances"
239258
pubsub_topic = google_pubsub_topic.start_topic.id
240259
service_account_email = local.sql_function_sa_email
260+
build_service_account = local.cloud_build_sa
241261
timeout = var.sql_function_config.timeout
242262
available_memory = var.sql_function_config.available_memory
243263
max_instance_count = var.sql_function_config.max_instance_count
@@ -258,6 +278,7 @@ module "function_stop_sql_instances" {
258278
entry_point = "stopInstances"
259279
pubsub_topic = google_pubsub_topic.stop_topic.id
260280
service_account_email = local.sql_function_sa_email
281+
build_service_account = local.cloud_build_sa
261282
timeout = var.sql_function_config.timeout
262283
available_memory = var.sql_function_config.available_memory
263284
max_instance_count = var.sql_function_config.max_instance_count
@@ -278,6 +299,7 @@ module "function_start_gke_node_pools" {
278299
entry_point = "startInstances"
279300
pubsub_topic = google_pubsub_topic.start_topic.id
280301
service_account_email = local.gke_function_sa_email
302+
build_service_account = local.cloud_build_sa
281303
timeout = var.gke_function_config.timeout
282304
available_memory = var.gke_function_config.available_memory
283305
max_instance_count = var.gke_function_config.max_instance_count
@@ -302,6 +324,7 @@ module "function_stop_gke_node_pools" {
302324
entry_point = "stopInstances"
303325
pubsub_topic = google_pubsub_topic.stop_topic.id
304326
service_account_email = local.gke_function_sa_email
327+
build_service_account = local.cloud_build_sa
305328
timeout = var.gke_function_config.timeout
306329
available_memory = var.gke_function_config.available_memory
307330
max_instance_count = var.gke_function_config.max_instance_count

terraform/modules/pubsub-function/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
|------|-------------|------|---------|:--------:|
88
| <a name="input_available_memory"></a> [available\_memory](#input\_available\_memory) | The amount of memory allotted for the function to use. | `string` | `"256M"` | no |
99
| <a name="input_bucket_name"></a> [bucket\_name](#input\_bucket\_name) | The bucket to store function source code. | `string` | `""` | no |
10+
| <a name="input_build_service_account"></a> [build\_service\_account](#input\_build\_service\_account) | The fully-qualified name of the service account to be used for building container. | `string` | `null` | no |
1011
| <a name="input_description"></a> [description](#input\_description) | The description of the cloud function. | `string` | `"Processes events."` | no |
1112
| <a name="input_entry_point"></a> [entry\_point](#input\_entry\_point) | The name of a method in the function source which will be invoked when the function is executed. | `string` | n/a | yes |
1213
| <a name="input_environment_variables"></a> [environment\_variables](#input\_environment\_variables) | A set of key/value environment variable pairs to assign to the function. | `map(string)` | `{}` | no |

terraform/modules/pubsub-function/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ resource "google_cloudfunctions2_function" "default" {
2525
object = google_storage_bucket_object.default.name
2626
}
2727
}
28+
service_account = var.build_service_account
2829
}
2930

3031
service_config {

terraform/modules/pubsub-function/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ variable "pubsub_topic" {
4545
description = "The name of a Pub/Sub topic."
4646
}
4747

48+
variable "build_service_account" {
49+
type = string
50+
description = "The fully-qualified name of the service account to be used for building container."
51+
default = null
52+
}
53+
4854
variable "service_account_email" {
4955
type = string
5056
description = "The existing service account to run cloud function."

terraform/variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ variable "function_labels" {
114114
description = "A set of key/value label pairs to assign to the function."
115115
}
116116

117+
variable "create_cloud_build_service_account" {
118+
type = bool
119+
description = "If the custom cloud build service account should be created."
120+
default = true
121+
}
122+
123+
variable "cloud_build_service_account_id" {
124+
type = string
125+
description = "The name of the service account that will be created if create_cloud_build_service_account is true."
126+
default = "sa-gcf"
127+
}
128+
129+
variable "cloud_build_service_account" {
130+
type = string
131+
default = null
132+
description = "The fully-qualified name of the custom cloud build service account."
133+
}
134+
135+
variable "cloud_build_service_account_iam_roles" {
136+
type = list(string)
137+
description = "IAM roles for custom cloud build service account"
138+
default = ["roles/logging.logWriter", "roles/artifactregistry.writer", "roles/storage.objectViewer"]
139+
}
140+
117141
variable "create_trigger_service_account" {
118142
type = bool
119143
description = "If the service account to trigger function should be created."

0 commit comments

Comments
 (0)