-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.tf
More file actions
259 lines (210 loc) · 7.31 KB
/
main.tf
File metadata and controls
259 lines (210 loc) · 7.31 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
terraform {
cloud {
organization = "govuk"
workspaces { name = "GitHub" }
}
required_version = "~> 1.10"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
github = {
source = "integrations/github"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "eu-west-2"
default_tags {
tags = {
Product = "GOV.UK"
System = "GitHub"
Environment = "[GitHub] Production"
Owner = "govuk-platform-engineering@digital.cabinet-office.gov.uk"
repository = "govuk-infrastructure"
terraform_deployment = basename(abspath(path.root))
}
}
}
provider "github" {
owner = "alphagov"
app_auth {
id = var.github_app_id # or `GITHUB_APP_ID`
installation_id = var.github_app_installation_id # or `GITHUB_APP_INSTALLATION_ID`
pem_file = var.github_app_pem_file # or `GITHUB_APP_PEM_FILE`
}
}
data "github_repositories" "govuk" {
query = "topic:govuk org:alphagov archived:false"
lifecycle {
postcondition {
condition = length(self.full_names) > 100
error_message = "not enough repositories were found by data source"
}
}
}
data "github_repository" "govuk" {
for_each = toset(data.github_repositories.govuk.full_names)
full_name = each.key
}
locals {
repositories = yamldecode(file("repos.yml"))["repos"]
deployable_repos = [
for r in data.github_repository.govuk : r
if !r.fork && contains(r.topics, "container")
]
gems = [
for r in data.github_repository.govuk : r
if !r.fork && contains(r.topics, "gem")
]
pact_publishers = [
for r in data.github_repository.govuk : r
if !r.fork && contains(r.topics, "pact-publisher")
]
}
resource "github_team" "govuk_ci_bots" {
name = "GOV.UK CI Bots"
privacy = "closed"
description = "Contains the `govuk-ci` user and grants it admin access to all GOV.UK repos"
}
resource "github_team" "govuk_production_admin" {
name = "GOV.UK Production Admin"
privacy = "closed"
description = "https://docs.publishing.service.gov.uk/manual/rules-for-getting-production-access.html"
}
resource "github_team" "govuk_production_deploy" {
name = "GOV.UK Production Deploy"
privacy = "closed"
description = "https://docs.publishing.service.gov.uk/manual/rules-for-getting-production-access.html"
}
import {
to = github_team.govuk_production_deploy
id = "gov-uk-production-deploy"
}
resource "github_team" "govuk" {
name = "GOV.UK"
privacy = "closed"
}
data "github_team" "co_platform_engineering" {
slug = "co-platform-engineering"
}
resource "github_team_repository" "govuk_production_admin_repos" {
for_each = local.repositories
repository = each.key
team_id = github_team.govuk_production_admin.id
permission = try(each.value.teams["govuk_production_admin"], "admin")
}
resource "github_team_repository" "govuk_ci_bots_repos" {
for_each = local.repositories
repository = each.key
team_id = github_team.govuk_ci_bots.id
permission = try(each.value.teams["govuk_ci_bots"], "admin")
}
resource "github_team_repository" "govuk_repos" {
for_each = local.repositories
repository = each.key
team_id = github_team.govuk.id
permission = try(each.value.teams["govuk"], "push")
}
resource "github_team_repository" "govuk_production_deploy_repos" {
for_each = local.repositories
repository = each.key
team_id = github_team.govuk_production_deploy.id
# give prod deploy the same permissions as the GOV.UK team
permission = try(each.value.teams["govuk"], "push")
}
resource "github_team_repository" "co_platform_engineering_repos" {
for_each = toset(["govuk-dns-tf", "govuk-dns", "govuk-dns-config"])
repository = each.key
team_id = data.github_team.co_platform_engineering.id
permission = "pull"
}
resource "github_repository" "govuk_repos" {
for_each = local.repositories
name = each.key
visibility = try(each.value.visibility, "public")
allow_squash_merge = true
allow_merge_commit = false
has_downloads = true
vulnerability_alerts = true
delete_branch_on_merge = true
homepage_url = try(each.value.homepage_url, null)
archive_on_destroy = true
lifecycle {
ignore_changes = [
description,
allow_auto_merge,
allow_merge_commit,
allow_rebase_merge,
allow_squash_merge,
allow_update_branch,
has_issues,
has_projects,
has_wiki,
squash_merge_commit_title,
squash_merge_commit_message,
pages
]
}
}
resource "github_branch_protection" "govuk_repos" {
for_each = { for repo_name, repo_details in local.repositories : repo_name => repo_details if try(repo_details["branch_protection"], true) }
repository_id = github_repository.govuk_repos[each.key].node_id
pattern = "main"
enforce_admins = true
allows_deletions = false
required_pull_request_reviews {
required_approving_review_count = 1
pull_request_bypassers = try(each.value.required_pull_request_reviews.pull_request_bypassers, null)
}
restrict_pushes {
blocks_creations = false
push_allowances = try(
each.value["push_allowances"],
["alphagov/gov-uk-production-admin", "alphagov/gov-uk-production-deploy"]
)
}
required_status_checks {
strict = try(each.value.strict, false)
contexts = concat(
try(each.value["required_status_checks"]["standard_contexts"], []),
try(each.value["required_status_checks"]["additional_contexts"], [])
)
}
lifecycle {
ignore_changes = [
require_conversation_resolution
]
}
}
#
# Only the list of repositories which will have access to a secret is created/modified
# here, the secret should have been created in the GitHub UI in advance by a
# GitHub Admin.
#
resource "github_actions_organization_secret_repositories" "ci_user_github_api_token" {
secret_name = "GOVUK_CI_GITHUB_API_TOKEN" # pragma: allowlist secret
selected_repository_ids = [for repo in concat(local.deployable_repos, local.gems) : repo.repo_id]
}
resource "github_actions_organization_secret_repositories" "argo_events_webhook_token" {
secret_name = "GOVUK_ARGO_EVENTS_WEBHOOK_TOKEN" # pragma: allowlist secret
selected_repository_ids = [for repo in local.deployable_repos : repo.repo_id]
}
resource "github_actions_organization_secret_repositories" "argo_events_webhook_url" {
secret_name = "GOVUK_ARGO_EVENTS_WEBHOOK_URL" # pragma: allowlist secret
selected_repository_ids = [for repo in local.deployable_repos : repo.repo_id]
}
resource "github_actions_organization_secret_repositories" "pact_broker_password" {
secret_name = "GOVUK_PACT_BROKER_PASSWORD" # pragma: allowlist secret
selected_repository_ids = [for repo in local.pact_publishers : repo.repo_id]
}
resource "github_actions_organization_secret_repositories" "pact_broker_username" {
secret_name = "GOVUK_PACT_BROKER_USERNAME" # pragma: allowlist secret
selected_repository_ids = [for repo in local.pact_publishers : repo.repo_id]
}
resource "github_actions_organization_secret_repositories" "slack_webhook_url" {
secret_name = "GOVUK_SLACK_WEBHOOK_URL" # pragma: allowlist secret
selected_repository_ids = [for repo in data.github_repository.govuk : repo.repo_id]
}