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

Merge terraform into early access #202

Merged
merged 158 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from 141 commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
6681bcc
add baseline org infra
umairidris Apr 2, 2020
cd9d55f
comment out gcs block, fmt
umairidris Apr 2, 2020
d0680ab
add data deployment
umairidris Apr 2, 2020
9948ff5
add cloudsql and network
umairidris Apr 3, 2020
b5cec97
rename network to networks
umairidris Apr 3, 2020
74cad75
add shared vpc
umairidris Apr 3, 2020
c18f5f3
add backend blocks
umairidris Apr 3, 2020
d071e1e
fix list item
umairidris Apr 3, 2020
bdc6908
Add GKE clusters and GKE network
MartinPetkov Apr 3, 2020
b110a8a
Merge branch 'terraform' of https://github.com/GoogleCloudPlatform/fd…
MartinPetkov Apr 3, 2020
ffe59bd
add data regions
umairidris Apr 3, 2020
4144d05
fix cloudsql registry path
umairidris Apr 3, 2020
dd1a937
fix terragrunt configs
umairidris Apr 3, 2020
085888e
fixes
umairidris Apr 3, 2020
dd0f065
Input the network for GKE through Terragrunt
MartinPetkov Apr 3, 2020
d51d0b1
Fix typo in mock output for GKE network terragrunt dependency
MartinPetkov Apr 3, 2020
554d0a5
add firebase project
umairidris Apr 6, 2020
c7db167
update names to match gcp setup doc
umairidris Apr 6, 2020
f516f97
Add network project in org policy.
xingao267 Apr 6, 2020
41dade5
Merge pull request #91 from xingao267/terraform
xingao267 Apr 6, 2020
e2e6de1
Add cloudbuild triggers.
xingao267 Apr 6, 2020
54a38ec
rename folder
umairidris Apr 6, 2020
db5ff02
Merge branch 'terraform' of https://github.com/GoogleCloudPlatform/fd…
umairidris Apr 6, 2020
bb62653
Move the GKE clusters into the same subnet
MartinPetkov Apr 6, 2020
0c67302
add gitignore, update bootstrap to fix dep and cleanup order
umairidris Apr 6, 2020
0fe3e03
put terraform block back up top
umairidris Apr 6, 2020
8619f31
Add selective CICD steps.
xingao267 Apr 6, 2020
229e7b0
Merge pull request #92 from MartinPetkov/terraform
umairidris Apr 6, 2020
dc66f45
Pure fmt fix.
xingao267 Apr 6, 2020
566a79d
HCL formatting.
xingao267 Apr 6, 2020
b6501e9
Add a note for tf validate.
xingao267 Apr 6, 2020
42d3aa1
Add plan cloudbuild job.
xingao267 Apr 6, 2020
54b978d
Comment out cloud build resources initially.
xingao267 Apr 6, 2020
242aff6
check in bootstrap terraform block
umairidris Apr 6, 2020
bf56b53
Merge branch 'terraform' of https://github.com/GoogleCloudPlatform/fd…
umairidris Apr 6, 2020
8e7d66c
add org policies terragrunt file
umairidris Apr 6, 2020
bfb6c18
Specify file include path in triggers and disable them initially.
xingao267 Apr 6, 2020
a95005e
Make plan presubmit job run by default and only make SA viewer.
xingao267 Apr 6, 2020
c9c0e68
Merge pull request #95 from xingao267/terraform
xingao267 Apr 6, 2020
165660b
post-deployment changes
umairidris Apr 6, 2020
d59f1c9
Format fix.
xingao267 Apr 7, 2020
33cc6f9
Add permissions to SA and enable more APIs.
xingao267 Apr 7, 2020
52042d5
Use for_each for roles.
xingao267 Apr 7, 2020
1ce0cd7
Refine viewer roles.
xingao267 Apr 7, 2020
dd3492d
more deployment fixes
umairidris Apr 7, 2020
53da9c7
Format
xingao267 Apr 7, 2020
ecf8dce
Merge pull request #97 from xingao267/terraform
xingao267 Apr 7, 2020
fc7a17c
move devops triggers to org folder
umairidris Apr 7, 2020
ee2a490
Merge branch 'terraform' of https://github.com/GoogleCloudPlatform/fd…
umairidris Apr 7, 2020
b40d174
rename to hhas_gke
umairidris Apr 7, 2020
608488d
make buckets multi regional
umairidris Apr 7, 2020
38bde91
add sql admin api o on apps project
umairidris Apr 7, 2020
c7d7d90
Add variables/tfvars for triggers.
xingao267 Apr 7, 2020
53c41e6
Add apply trigger.
xingao267 Apr 7, 2020
460f29a
Use variable to control CD job.
xingao267 Apr 7, 2020
6942ae9
move shared vpc deployment to networks, limit gke SA access to subnet
umairidris Apr 7, 2020
24d9883
merge
umairidris Apr 7, 2020
117ab4e
Add missing API and permissions to deploy firebase project.
xingao267 Apr 7, 2020
eaa5c5b
Delete .gitignore in Terraform/ directory.
xingao267 Apr 8, 2020
ea58e33
Add included files for presubmit triggers.
xingao267 Apr 8, 2020
9a949e3
Enable subnet_private_access and subnet_flow_logs
MartinPetkov Apr 8, 2020
fc107f6
Merge pull request #109 from MartinPetkov/terraform
umairidris Apr 8, 2020
801209f
Add explicit IP address range for the Cloud SQL private access
MartinPetkov Apr 8, 2020
c3fe2d7
Merge pull request #110 from MartinPetkov/cloudsql-add-ip-and-prefix
umairidris Apr 8, 2020
f09dc21
Update comment for Cloud SQL network prefix
MartinPetkov Apr 8, 2020
375e2bf
Merge pull request #111 from MartinPetkov/terraform
umairidris Apr 8, 2020
02497e3
Make the GKE apps depend on the data deployment
MartinPetkov Apr 8, 2020
f067333
Merge pull request #112 from MartinPetkov/terraform
umairidris Apr 8, 2020
4377df5
Enable CD to do a full deployment.
xingao267 Apr 8, 2020
23d3889
Increase the timeout.
xingao267 Apr 8, 2020
38f5c2a
add initial readme, add org admin to bootstrap
umairidris Apr 8, 2020
2b6c70c
minor spacing fixes
umairidris Apr 8, 2020
482795f
minor spacing fixes
umairidris Apr 8, 2020
ba2f48c
fmt
umairidris Apr 8, 2020
3590141
Merge branch 'terraform' into umairidris
umairidris Apr 8, 2020
a9985d2
add terraform readme
umairidris Apr 8, 2020
0a12873
rm readme
umairidris Apr 8, 2020
553dad4
Merge pull request #114 from GoogleCloudPlatform/umairidris
umairidris Apr 8, 2020
238d142
Merge branch 'terraform' into terraform
xingao267 Apr 8, 2020
777a24a
Include cd config itself in trigger.
xingao267 Apr 8, 2020
cc33e58
Merge pull request #113 from xingao267/terraform
umairidris Apr 8, 2020
74446ee
Use CFT container.
xingao267 Apr 8, 2020
ba1b3ca
Upgrade TF version.
xingao267 Apr 8, 2020
a1d67b3
Merge pull request #117 from xingao267/terraform
umairidris Apr 9, 2020
d5f46c5
add deployment steps
umairidris Apr 9, 2020
b7af48d
Combine all clusters into one (#122)
MartinPetkov Apr 9, 2020
f6f72f0
Re-add subnet ranges because GCP can't add and remove secondary IP ra…
MartinPetkov Apr 9, 2020
55139d5
Missed one for auth-server (#124)
MartinPetkov Apr 9, 2020
6fc630f
aaddress zohreh comments
umairidris Apr 9, 2020
117d59c
try to fix numbering
umairidris Apr 9, 2020
7aa2659
rename folder
umairidris Apr 9, 2020
801c703
Merge branch 'terraform' into readme
umairidris Apr 9, 2020
73d22e9
wording
umairidris Apr 9, 2020
f1e951d
Merge branch 'readme' of https://github.com/GoogleCloudPlatform/fda-m…
umairidris Apr 9, 2020
32ee0cf
Re-remove subnets, can't add and delete secondary subnet IP ranges (#…
MartinPetkov Apr 9, 2020
d4f56df
Merge branch 'terraform' into readme
umairidris Apr 9, 2020
527e58b
prefix
umairidris Apr 9, 2020
5919748
Merge branch 'readme' of https://github.com/GoogleCloudPlatform/fda-m…
umairidris Apr 9, 2020
25eabc0
Merge pull request #115 from GoogleCloudPlatform/readme
umairidris Apr 9, 2020
2736151
Add roles/cloudsql.client binding for the data project (#126)
MartinPetkov Apr 14, 2020
d818a2e
Make region and zone mandatory and consistent. (#137)
xingao267 Apr 14, 2020
0650383
Add random suffix to sql name to avoid name duplication.
xingao267 Apr 14, 2020
936efb1
Merge branch 'terraform' into suffix
xingao267 Apr 14, 2020
51325e3
Simply rename the SQL instead.
xingao267 Apr 14, 2020
ed2b286
Merge pull request #143 from xingao267/suffix
xingao267 Apr 14, 2020
9bc4cc2
Add missing serviceAccount: prefix. (#146)
xingao267 Apr 14, 2020
fe8a58f
Add support for secrets (#144)
umairidris Apr 14, 2020
950d7a2
Add README for CICD. (#139)
xingao267 Apr 14, 2020
0549d55
include children for audit exports (#147)
umairidris Apr 14, 2020
bf6895d
Add bastion host. (#136)
xingao267 Apr 15, 2020
fe12751
Enable IAP api in network project. (#148)
xingao267 Apr 15, 2020
30b815a
add bastion service account output, fix apps mock outputs (#153)
umairidris Apr 15, 2020
ce8f727
add bastion service account as sql client (#151)
umairidris Apr 15, 2020
d018ed6
fix startup script for cloud sql proxy setup (#155)
umairidris Apr 15, 2020
bbad118
Add new secrets in Secret Manager for later use in Kubernetes (#152)
MartinPetkov Apr 16, 2020
70781b7
Add documentations for org level resources. (#160)
xingao267 Apr 16, 2020
52bb977
Add license. (#163)
xingao267 Apr 16, 2020
96b1bc2
Move Cloud Build Triggers for GKE containers to app project. (#159)
xingao267 Apr 16, 2020
473afa2
Add more documentation. (#164)
xingao267 Apr 16, 2020
1bcc63e
Add service accounts for each GKE app (#168)
MartinPetkov Apr 17, 2020
188b653
Add multiple DB credentials and roles/cloudsql.client for the GKE SAs…
MartinPetkov Apr 17, 2020
a70fd7b
Fix projects for DB users secrets (#169)
MartinPetkov Apr 17, 2020
ec0ffa4
Update k8s deployments and add cluster-wide configs (#157)
MartinPetkov Apr 17, 2020
c57a346
Update TF Engine config to date. (#177)
xingao267 Apr 17, 2020
becc583
Minor fix in engine template and configs. (#178)
xingao267 Apr 20, 2020
493ed59
Add TODO to replace kubernetes_version with release_channel (#182)
MartinPetkov Apr 20, 2020
91cd517
Add Kubernetes Secrets (#149)
MartinPetkov Apr 20, 2020
161b9df
Update kubeapply (#180)
MartinPetkov Apr 20, 2020
ac668ee
Use GKE token and fully-qualified SAs in secrets (#183)
MartinPetkov Apr 20, 2020
d474b81
Fix SQL instance connections in k8s deployments (#185)
MartinPetkov Apr 20, 2020
a72bfff
Update Terraform configs with changes in engine. (#184)
xingao267 Apr 20, 2020
08129e9
Generate configs using OSS TF Engine. (#187)
xingao267 Apr 20, 2020
18b23d2
Improve CICD permissions (#189)
umairidris Apr 20, 2020
67447fb
Document the GKE cluster setup steps (#181)
MartinPetkov Apr 21, 2020
1f889dc
Fixes. (#194)
xingao267 Apr 21, 2020
fd0d095
More fixes. (#195)
xingao267 Apr 21, 2020
b53fe93
Use the right DB names for the apps (#193)
MartinPetkov Apr 21, 2020
c6a4022
Add super experimental rename templates script. (#196)
xingao267 Apr 21, 2020
f02e17e
Fix user-registration-server-np port (#197)
MartinPetkov Apr 21, 2020
690cc0f
Comment out customer triggers initially. (#199)
xingao267 Apr 21, 2020
4c24cd8
Merge branch 'terraform' into merge-terraform-into-early-access
MartinPetkov Apr 21, 2020
79eeb1e
Install the Istio sidecar (#203)
MartinPetkov Apr 22, 2020
e707a60
Add cloud build viewer IAM member. (#201)
xingao267 Apr 22, 2020
d113518
Improve rename.sh and add to README.md. (#205)
xingao267 Apr 22, 2020
7d322a9
Merge branch 'early-access' into merge-terraform-into-early-access
MartinPetkov Apr 22, 2020
2a2aaf1
Address comments from https://github.com/GoogleCloudPlatform/fda-myst…
MartinPetkov Apr 22, 2020
ce0e224
Merge branch 'terraform' into merge-terraform-into-early-access
MartinPetkov Apr 22, 2020
756498a
Remove workflows to try to get GitHub to see them. Will be re-added i…
MartinPetkov Apr 22, 2020
9d4004c
Revert "Remove workflows to try to get GitHub to see them. Will be re…
MartinPetkov Apr 22, 2020
dcc781b
Remove the whole .github/ dir. Will readd immediately.
MartinPetkov Apr 22, 2020
54f93e7
Revert "Remove the whole .github/ dir. Will readd immediately."
MartinPetkov Apr 22, 2020
cee746f
Create a static external IP for the Ingress (#207)
MartinPetkov Apr 22, 2020
8249cf2
Merge remote-tracking branch 'origin/terraform' into terraform
MartinPetkov Apr 22, 2020
9fa1d6c
Merge branch 'terraform' into merge-terraform-into-early-access
MartinPetkov Apr 22, 2020
fc9b5b0
Change Ingress static IP to global (#208)
MartinPetkov Apr 23, 2020
acb2d4d
Merge remote-tracking branch 'origin/terraform' into terraform
MartinPetkov Apr 23, 2020
8bf4e07
Merge branch 'terraform' into merge-terraform-into-early-access
MartinPetkov Apr 23, 2020
dd6a246
Fork the service.yaml files into tf-service.yaml
MartinPetkov Apr 23, 2020
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/.terraform
*.tfstate
*.tfstate.*
sql_credentials.json
MartinPetkov marked this conversation as resolved.
Show resolved Hide resolved
key.json
146 changes: 146 additions & 0 deletions Terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# FDA MyStudies Terraform Infrastructure

These directories define the entire GCP infrastructure app to run the FDA
MyStudies application.

## Prerequisites

1. Install the following dependencies and add them to your PATH:

- [GCloud](https://cloud.google.com/sdk/gcloud)
- [Terraform](https://www.terraform.io/)
- [Terragrunt](https://terragrunt.gruntwork.io/)

1. Get familiar with [GCP](https://cloud.google.com/docs/overview),
[Terraform](https://www.terraform.io/intro/index.html) and
[Terragrunt](https://blog.gruntwork.io/terragrunt-how-to-keep-your-terraform-code-dry-and-maintainable-f61ae06959d8).

The infrastructure is deployed using Terraform, which is an industry
standard for defining infrastructure-as-code. Terragrunt is used as a
wrapper around Terraform to manage multiple Terraform deployments and reduce
duplication.

1. Setup your
[organization](https://cloud.google.com/resource-manager/docs/creating-managing-organization)
for GCP resources and [G Suite Domain](https://gsuite.google.com/) for
groups.

1. [Create administrative groups](https://support.google.com/a/answer/33343?hl=en)
in the G Suite Domain:

- {PREFIX}-org-admins@{DOMAIN}.com: This group has administrative access
to the entire org. This group can be used in break-glass situations to
give humans access to the org to make changes.

- {PREFIX}-devops-owners@{DOMAIN}.com: This group has owners access to the
devops project to make changes to the CICD project or make changes to
the Terraform state.

- {PREFIX}-auditors@{DOMAIN}.com: This group has security reviewer
(metadata viewer) access to the entire org, as well as viewer access to
the audit logs BigQuery and Cloud Storage resources.

WARNING: It is always recommended to use CICD to deploy changes to the
infrastructure. The groups above should remain empty and only have humans
added for emergency situations or when investigation is required.

## Directory Structure

The infrastructure is split into multiple directories. Each directory represents
one Terraform deployment. Each deployment will manage specific resources in you
infrastructure.

A deployment typically contains the following files:

- **main.tf**: This file defines the Terraform resources and modules to
manage. For more complex deployments, there may be multiple .tf files that
define resources.

- **variables.tf**: This file defines any input variables that the deployment
can take.

- **outputs.tf**: This file defines any outputs from this deployment. These
values can be used by other deployments.

- **terraform.tfvars**: This file defines values for the input variables.

- **terragrunt.hcl**: This file defines dependencies between other
deployments, the remote state, and input values from other dependent
deployments.

To see what resources each deployment provisions, check out the comments in each
**main.tf** file.

## Layout

```
|- bootstrap: one time setup to create projects to host Terraform state and CICD pipeline.
|- cicd: CloudBuild configs for the CICD pipeline.
|- secrets: Definitions of secrets used in the org (secret values are not set in configs).
|- org: org level resources. Resources within this directory should be managed by CICD pipeline.
|- terragrunt.hcl: root Terragrunt config which defines remote state for all deployments.
|- project.{PREFIX}-audit: the project to hold all audit logs for the org.
|- audit: deployment to setup auditing for the org.
|- iam: org level iam definitions such as org admins.
|- folder.fda-mystudies: folder to hold all projects related to FDA MyStudies.
|- project.{PREFIX}-apps: apps project and resources (GKE)
|- project.{PREFIX}-data: data project and resources (GCS buckets, CloudSQL instances)
|- project.{PREFIX}-networks: network project and resources (VPC)
|- project.{PREFIX}-firebase: firebase project (firestores)
```

## Deployment Steps

1. Authenticate as a super admin using `gcloud auth login [ACCOUNT]`.

WARNING: remember to run `gcloud auth revoke` to logout as a super admin.
Being logged in as a super admin beyond the initial setup is dangerous!

1. Checkout the Terraform configs and set some helper environment variables.

```
$ git clone my-repo
$ cd my-repo
$ ROOT=$PWD
```

1. The bootstrap config must be deployed first in order to create the `devops`
project which will host your Terraform state and CICD pipelines.

```
$ cd $ROOT/boootstrap
$ terraform init
$ terraform plan
$ terraform apply
```

Your `devops` project should now be ready.

1. Backup the state of the `devops` project to the newly created state bucket
by uncommenting out the `terraform` block in `$ROOT/bootstrap/main.tf` and
running:

```
$ terraform init
```

1. Deploy secrets used in the org in the `devops` project.

```
$ cd $ROOT/secrets
$ terraform init
$ terraform plan
$ terraform apply
```

After the secrets have been created, you must go to the Google Cloud
Console, open `Security` --> `Secret Manager` and fill in their values.

1. Follow `$ROOT/cicd/README.md` to set up CICD pipelines for Terraform
configs.

1. Follow `$ROOT/kubernetes/README.md` to deploy the Kubernetes resources in
the GKE cluster.

1. Revoke your super admin access by running `gcloud auth revoke` and
authenticate as a normal user for daily activities.
9 changes: 9 additions & 0 deletions Terraform/bootstrap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This directory defines resources that must be deployed first in order for the
rest of the Terraform configs to function.

Run `terraform init` and `terraform apply` in this directory and backup the
Terraform state files manually.

Currently this only creates the central devops project. After this project has
been created, Terragrunt can bootstrap the state bucket inside the project and
manage all the following resources.
74 changes: 74 additions & 0 deletions Terraform/bootstrap/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright 2020 Google Inc.
#
# 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.

# This folder contains Terraform resources to setup the devops project, which includes:
# - The project itself,
# - APIs to enable,
# - Deletion lien,
# - Project level IAM permissions for the project owners,
# - A Cloud Storage bucket to store Terraform states for all deployments,
# - Org level IAM permissions for org admins.

// TODO: replace with https://github.com/terraform-google-modules/terraform-google-bootstrap

# ==============================================================================
# TODO: Uncomment after initial deployment and run `terraform init`.
terraform {
backend "gcs" {
bucket = "heroes-hat-dev-terraform-state-08679"
prefix = "bootstrap"
}
}
# ==============================================================================

# Create the project, enable APIs, and create the deletion lien, if specified.
module "project" {
source = "terraform-google-modules/project-factory/google"
version = "~> 7.0"

name = var.devops_project_id
org_id = var.org_id
billing_account = var.billing_account
lien = true
default_service_account = "keep"
skip_gcloud_download = true
activate_apis = [
"cloudbuild.googleapis.com",
"secretmanager.googleapis.com",
]
}

# Terraform state bucket, hosted in the devops project.
module "state_bucket" {
source = "terraform-google-modules/cloud-storage/google//modules/simple_bucket"
version = "~> 1.4"

name = var.state_bucket
project_id = module.project.project_id
location = var.storage_location
}

# Project level IAM permissions for devops project owners.
resource "google_project_iam_binding" "devops_owners" {
project = module.project.project_id
role = "roles/owner"
members = var.devops_owners
}

# Org level IAM permissions for org admins.
resource "google_organization_iam_member" "org_admin" {
org_id = var.org_id
role = "roles/resourcemanager.organizationAdmin"
member = var.org_admin
}
23 changes: 23 additions & 0 deletions Terraform/bootstrap/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2020 Google Inc.
#
# 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.

devops_project_id = "heroes-hat-dev-devops"
org_id = "707577601068"
billing_account = "01EA90-3519E1-89CB1F"
state_bucket = "heroes-hat-dev-terraform-state-08679"
storage_location = "us-central1"
org_admin = "group:rocketturtle-gcp-admin@rocketturtle.net"
devops_owners = [
"group:rocketturtle-gcp-admin@rocketturtle.net",
]
zohrehj marked this conversation as resolved.
Show resolved Hide resolved
41 changes: 41 additions & 0 deletions Terraform/bootstrap/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2020 Google Inc.
#
# 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.

variable "devops_project_id" {
type = string
}

variable "devops_owners" {
type = list(string)
}

variable "org_id" {
type = string
}

variable "billing_account" {
type = string
}

variable "state_bucket" {
type = string
}

variable "storage_location" {
type = string
}

variable "org_admin" {
type = string
}
88 changes: 88 additions & 0 deletions Terraform/cicd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
This directory defines resources needed to setup CICD pipelines of Terraform
configs.

The CI and CD pipelines use
[Google Cloud Build](https://cloud.google.com/cloud-build) and
[Cloud Build Triggers](https://cloud.google.com/cloud-build/docs/automating-builds/create-manage-triggers)
to detect changes in the repo, trigger builds and run the workloads.

## Setup

1. In the Terraform Engine config, add a `CICD` block under the `foundation`
recipe and specify the following attributes:

* `PROJECT_ID`: Project ID of the `devops` project
* `STATE_BUCKET`: Name of the state bucket
* `REPO_OWNER`: GitHub repo owner
* `REPO_NAME`: GitHub repo name
* `BRANCH_REGEX`: Regex of the branches to set the Cloud Build Triggers to
monitor
* `CONTINUOUS_DEPLOYMENT_ENABLED`: Whether or not to enable continuous
deployment of Terraform configs
* `TRIGGER_ENABLED`: Whether or not to enable all Cloud Build Triggers
* `TERRAFORM_ROOT`: Path of the directory relative to the repo root
containing the Terraform configs

1. Generate the CICD Terraform configs and Cloud Build configs using the
Terraform Engine.

1. Before deployment CICD Terraform resources, follow
[installing_the_cloud_build_app](https://cloud.google.com/cloud-build/docs/automating-builds/create-github-app-triggers#installing_the_cloud_build_app)
to install the Cloud Build app and connect your GitHub repository to your
Cloud project. This currently cannot be done through automation.

1. Once the GitHub repo is connected, run the following commands in this
directory to enable necessary APIs, grant Cloud Build Service Account
necessary permissions and create Cloud Build Triggers:

```
$ terraform init
$ terraform plan
$ terraform apply
```

Two presubmit triggers are created by default and results are posted in the
Pull Request. Failing these presubmits will block Pull Request submission.

1. `tf-validate`: Perform Terraform format and syntax check.
1. `tf-plan`: Generate speculative plans to show a set of possible changes
if the pending config changes are deployed.

If `CONTINUOUS_DEPLOYMENT_ENABLED` is set to `true` in your Terraform Engine
config, `continuous_deployment_enabled` will be set to `true` in
`terraform.tfvars` in this directory to create an additional Cloud Build
Trigger and grant the Cloud Build Service Account broder permissions to
automaticaly apply the config changes to GCP after the Pull Request is
approved and submitted.

After the triggers are created, to temporarily disable or re-enable them,
set the `trigger_enabled` in `terraform.tfvars` to `false` or `true` and
apply the changes by running:

```
$ terraform init
$ terraform plan
$ terraform apply
```

## Operation

### Continuous Integration (presubmit)

Presubmit Cloud Build results will be posted as a Cloud Build job link in the
Pull Request, and they will be configured to block Pull Request submission.

Every new push to the Pull Request at the configured branches will automatically
trigger presubmit runs. To manually re-trigger CI jobs, comment `/gcbrun` in the
Pull Ruquest.

### Continuous Deployment (postsubmit)

Postsubmit Cloud Build job will automatically start when a Pull Ruquest is
submitted to a configured branch. To view the result of the Cloud Build run, go
to https://console.cloud.google.com/cloud-build/builds and look for your commit
to view the Cloud Build job triggered by your merged commit.

The Postsubmit Cloud Build Trigger monitors and deploys changes made to `org/`
folder only. Other changes made to `bootstrap`, `cicd` and `secrets` folders
should be deployed manually if needed.
Loading