-
Notifications
You must be signed in to change notification settings - Fork 105
/
main.tf
166 lines (151 loc) · 5.63 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
locals {
network_name = var.create_network ? google_compute_network.gh-network[0].self_link : var.network_name
subnet_name = var.create_subnetwork ? google_compute_subnetwork.gh-subnetwork[0].self_link : var.subnet_name
service_account = var.service_account == "" ? google_service_account.runner_service_account[0].email : var.service_account
startup_script = var.startup_script == "" ? file("${path.module}/scripts/startup.sh") : var.startup_script
shutdown_script = var.shutdown_script == "" ? file("${path.module}/scripts/shutdown.sh") : var.shutdown_script
}
/*****************************************
Optional Runner Networking
*****************************************/
resource "google_compute_network" "gh-network" {
count = var.create_network ? 1 : 0
name = var.network_name
project = var.project_id
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "gh-subnetwork" {
count = var.create_subnetwork ? 1 : 0
project = var.project_id
name = var.subnet_name
ip_cidr_range = var.subnet_ip
region = var.region
network = local.network_name
}
resource "google_compute_router" "default" {
count = var.create_network ? 1 : 0
name = "${var.network_name}-router"
network = google_compute_network.gh-network[0].self_link
region = var.region
project = var.project_id
}
resource "google_compute_router_nat" "nat" {
count = var.create_network ? 1 : 0
project = var.project_id
name = "${var.network_name}-nat"
router = google_compute_router.default[0].name
region = google_compute_router.default[0].region
nat_ip_allocate_option = "AUTO_ONLY"
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
}
/*****************************************
IAM Bindings GCE SVC
*****************************************/
resource "google_service_account" "runner_service_account" {
count = var.service_account == "" ? 1 : 0
project = var.project_id
account_id = "runner-service-account"
display_name = "Github Runner GCE Service Account"
}
/*****************************************
Runner Secrets
*****************************************/
resource "google_secret_manager_secret" "gh-secret" {
provider = google-beta
project = var.project_id
secret_id = "gh-token"
labels = {
label = "gh-token"
}
replication {
user_managed {
replicas {
location = var.region
}
}
}
}
resource "google_secret_manager_secret_version" "gh-secret-version" {
provider = google-beta
secret = google_secret_manager_secret.gh-secret.id
secret_data = jsonencode({
"REPO_NAME" = var.repo_name
"REPO_OWNER" = var.repo_owner
"GITHUB_TOKEN" = var.gh_token
"LABELS" = join(",", var.gh_runner_labels)
})
}
resource "google_secret_manager_secret_iam_member" "gh-secret-member" {
provider = google-beta
project = var.project_id
secret_id = google_secret_manager_secret.gh-secret.id
role = "roles/secretmanager.secretAccessor"
member = "serviceAccount:${local.service_account}"
}
/*****************************************
Runner GCE Instance Template
*****************************************/
locals {
instance_name = "gh-runner-vm"
}
module "mig_template" {
source = "terraform-google-modules/vm/google//modules/instance_template"
version = "~> 12.0"
project_id = var.project_id
machine_type = var.machine_type
network = local.network_name
subnetwork = local.subnet_name
region = var.region
subnetwork_project = var.subnetwork_project != "" ? var.subnetwork_project : var.project_id
service_account = {
email = local.service_account
scopes = [
"https://www.googleapis.com/auth/cloud-platform",
]
}
disk_size_gb = 100
disk_type = "pd-ssd"
auto_delete = true
name_prefix = "gh-runner"
source_image_family = var.source_image_family
source_image_project = var.source_image_project
startup_script = local.startup_script
source_image = var.source_image
metadata = merge({
"secret-id" = google_secret_manager_secret_version.gh-secret-version.name
}, {
"shutdown-script" = local.shutdown_script
}, var.custom_metadata)
tags = concat(["gh-runner-vm"], var.instance_tags)
}
/*****************************************
Runner MIG
*****************************************/
module "mig" {
source = "terraform-google-modules/vm/google//modules/mig"
version = "~> 12.0"
project_id = var.project_id
hostname = local.instance_name
region = var.region
instance_template = module.mig_template.self_link
/* autoscaler */
autoscaling_enabled = true
min_replicas = var.min_replicas
max_replicas = var.max_replicas
cooldown_period = var.cooldown_period
}