Skip to content

Commit 923e06b

Browse files
authored
Merge pull request #3 from getsentry/create-CI-CD
Create ci cd
2 parents ff12f01 + efb2eaa commit 923e06b

File tree

8 files changed

+92
-22
lines changed

8 files changed

+92
-22
lines changed

.github/workflows/terraform-apply.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ jobs:
2121
id: auth
2222
uses: google-github-actions/auth@v2
2323
with:
24-
workload_identity_provider: |
25-
projects/${{ var.project_number }}/locations/global/workloadIdentityPools/${{ var.environ }}-github/providers/github-oidc-pool
26-
service_account: ${{ var.terraformer }}
24+
workload_identity_provider: projects/546928617664/locations/global/workloadIdentityPools/gha-terraform-checker-pool/providers/gha-terraform-checker-provider
25+
service_account: gha-cloud-functions-deployment@jeffreyhung-test.iam.gserviceaccount.com
2726

2827
- name: terraform apply
2928
id: terraform-apply

.github/workflows/terraform-plan.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ jobs:
2121
id: auth
2222
uses: google-github-actions/auth@v2
2323
with:
24-
workload_identity_provider: |
25-
projects/${{ var.project_number }}/locations/global/workloadIdentityPools/${{ var.environ }}-github/providers/github-oidc-pool
26-
service_account: ${{ var.terraformer }}
24+
workload_identity_provider: projects/546928617664/locations/global/workloadIdentityPools/gha-terraform-checker-pool/providers/gha-terraform-checker-provider
25+
service_account: gha-cloud-functions-deployment@jeffreyhung-test.iam.gserviceaccount.com
2726

2827
- name: terraform plan
2928
id: terraform-plan
@@ -32,3 +31,5 @@ jobs:
3231
${{ steps.auth.outcome == 'success'}}
3332
with:
3433
add_github_comment: changes-only
34+
env:
35+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

functions/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ module "cronjob-gen2" {
8282
target_function_name = module.cloud_function_gen2[each.value.name].function_name
8383
https_trigger_url = module.cloud_function_gen2[each.value.name].function_trigger_url
8484
# passing the static values
85-
target_project = var.project
86-
target_region = var.region
85+
target_project = var.project
86+
target_region = var.region
8787
deploy_sa_email = var.deploy_sa_email
8888

8989
depends_on = [

infrastructure/permissions.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Project-wide roles
2+
locals {
3+
roles = [
4+
"roles/viewer", # general read-only access to most Google Cloud resources
5+
"roles/storage.admin", # full access to manage GCS buckets and objects
6+
"roles/secretmanager.secretAccessor", # access to Secret Manager
7+
"roles/cloudfunctions.developer", # deploy and manage Cloud Functions
8+
"roles/logging.viewer", # view logs
9+
"roles/iam.serviceAccountUser", # necessary to invoke Cloud Functions
10+
"roles/iam.workloadIdentityPoolViewer" # view workload identity pool
11+
]
12+
}
13+
14+
resource "google_project_iam_member" "project_roles" {
15+
for_each = toset(local.roles)
16+
project = var.project
17+
role = each.value
18+
member = "serviceAccount:${google_service_account.gha_cloud_functions_deployment.email}"
19+
20+
}

infrastructure/workload_identity.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ variable "region" {}
88

99
resource "google_service_account" "gha_cloud_functions_deployment" {
1010
account_id = "gha-cloud-functions-deployment"
11-
description = "For use by Terraform and GitHub Actions to deploy DNR pipeline resources via security-cloud-functions repo"
11+
description = "For use by Terraform and GitHub Actions to deploy cloud-functions"
1212
display_name = "gha-cloud-functions-deployment"
1313
project = var.project
1414
}
@@ -41,4 +41,4 @@ resource "google_service_account_iam_member" "gha_workload_identity_user" {
4141
service_account_id = google_service_account.gha_cloud_functions_deployment.id
4242
role = "roles/iam.workloadIdentityUser"
4343
member = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.gha_terraform_checker_pool.name}/*"
44-
}
44+
}

main.tf

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ locals {
66
project_num = "546928617664"
77
bucket_location = "US-WEST1"
88
alerts_collection = "alerts"
9-
sentry_jira_url = "https://getsentry.atlassian.net"
109
}
1110

1211
terraform {
@@ -16,6 +15,10 @@ terraform {
1615
version = "~> 6.0.1"
1716
}
1817
}
18+
backend "gcs" {
19+
bucket = "jeffreyhung-test-tfstate"
20+
prefix = "terraform/state"
21+
}
1922
}
2023

2124
provider "google" {
@@ -25,8 +28,55 @@ provider "google" {
2528
}
2629

2730
resource "google_storage_bucket" "staging_bucket" {
28-
name = "${local.project}-cloud-function-staging"
29-
location = "US"
30-
force_destroy = true
31+
name = "${local.project}-cloud-function-staging"
32+
location = "US"
33+
force_destroy = true
3134
public_access_prevention = "enforced"
35+
}
36+
37+
resource "google_storage_bucket_iam_binding" "staging-bucket-iam" {
38+
bucket = google_storage_bucket.tf-state.name
39+
role = "roles/storage.objectUser"
40+
41+
members = ["serviceAccount:${module.infrastructure.deploy_sa_email}"]
42+
43+
depends_on = [
44+
module.infrastructure,
45+
google_storage_bucket.staging_bucket
46+
]
47+
}
48+
49+
resource "google_storage_bucket_iam_member" "staging_bucket_get" {
50+
bucket = google_storage_bucket.staging_bucket.name
51+
role = "roles/storage.objectViewer"
52+
member = "serviceAccount:${module.infrastructure.deploy_sa_email}"
53+
}
54+
55+
resource "google_storage_bucket" "tf-state" {
56+
name = "${local.project}-tfstate"
57+
force_destroy = false
58+
location = "US"
59+
storage_class = "STANDARD"
60+
public_access_prevention = "enforced"
61+
versioning {
62+
enabled = true
63+
}
64+
}
65+
66+
resource "google_storage_bucket_iam_binding" "tfstate-bucket-iam" {
67+
bucket = google_storage_bucket.tf-state.name
68+
role = "roles/storage.objectUser"
69+
70+
members = ["serviceAccount:${module.infrastructure.deploy_sa_email}"]
71+
72+
depends_on = [
73+
module.infrastructure,
74+
google_storage_bucket.tf-state
75+
]
76+
}
77+
78+
resource "google_storage_bucket_iam_member" "tfstate_bucket_get" {
79+
bucket = google_storage_bucket.tf-state.name
80+
role = "roles/storage.objectViewer"
81+
member = "serviceAccount:${module.infrastructure.deploy_sa_email}"
3282
}

modules/cloud-function-gen2/variables.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ variable "runtime" {
3131
type = string
3232
description = "Function runtime, default python 3.11"
3333
default = "python311"
34-
nullable=false
34+
nullable = false
3535
}
3636

3737
variable "source_object_prefix" {
@@ -56,21 +56,21 @@ variable "trigger_http" {
5656
type = bool
5757
description = "Whether or not the trigger for this cloud function should be an HTTP endpoint"
5858
default = true
59-
nullable=false
59+
nullable = false
6060
}
6161

6262
variable "execution_timeout" {
6363
type = number
6464
description = "Amount of time function can execute before timing out, in seconds"
6565
default = 60
66-
nullable=false
66+
nullable = false
6767
}
6868

6969
variable "available_memory_mb" {
7070
type = string
7171
description = "Amount of memory assigned to each execution"
7272
default = "128M"
73-
nullable=false
73+
nullable = false
7474
}
7575

7676
variable "temp_zip_output_dir" {

modules/cloud-function/variables.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ variable "runtime" {
2424
type = string
2525
description = "Function runtime, default python 3.11"
2626
default = "python311"
27-
nullable=false
27+
nullable = false
2828
}
2929

3030
variable "source_object_prefix" {
@@ -49,21 +49,21 @@ variable "trigger_http" {
4949
type = bool
5050
description = "Whether or not the trigger for this cloud function should be an HTTP endpoint"
5151
default = true
52-
nullable=false
52+
nullable = false
5353
}
5454

5555
variable "execution_timeout" {
5656
type = number
5757
description = "Amount of time function can execute before timing out, in seconds"
5858
default = 60
59-
nullable=false
59+
nullable = false
6060
}
6161

6262
variable "available_memory_mb" {
6363
type = number
6464
description = "Amount of memory assigned to each execution"
6565
default = 128
66-
nullable=false
66+
nullable = false
6767
}
6868

6969
variable "temp_zip_output_dir" {

0 commit comments

Comments
 (0)