Skip to content

Commit c18715a

Browse files
authored
Merge pull request #19 from HTTPArchive/development
Pipeline automation
2 parents c67f547 + 1657302 commit c18715a

File tree

11 files changed

+423
-98
lines changed

11 files changed

+423
-98
lines changed

.github/workflows/pipeline.yaml

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
name: Tech Report API Pipeline
22

3-
on:
4-
push:
5-
branches:
6-
- 'main'
7-
- 'feature**'
8-
delete:
9-
branches:
10-
- 'feature**'
11-
12-
# env:
13-
# PIPELINE_USER_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
3+
on: [push]
4+
5+
env:
6+
PIPELINE_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
7+
PIPELINE_SA_KEY: ${{ secrets.GCP_SA_KEY }}
8+
PIPELINE_PROJECT_DATABASE_DEV: ${{ secrets.GCP_PROJECT_DATABASE_DEV }}
9+
PIPELINE_PROJECT_DATABASE_PROD: ${{ secrets.GCP_PROJECT_DATABASE_PROD }}
10+
PIPELINE_GOOGLE_SERVICE_ACCOUNT_CLOUD_FUNCTIONS: ${{ secrets.GCP_SERVICE_ACCOUNT_CLOUD_FUNCTIONS }}
11+
PIPELINE_GOOGLE_SERVICE_ACCOUNT_API_GATEWAY: ${{ secrets.GCP_SERVICE_ACCOUNT_API_GATEWAY }}
1412

1513
jobs:
1614
test:
17-
if: github.event_name == 'push'
1815
runs-on: ubuntu-latest
1916
steps:
2017
- uses: actions/checkout@v3
@@ -31,3 +28,104 @@ jobs:
3128
run: |
3229
python -m pytest -W "ignore"
3330
31+
deploy_development:
32+
if: github.ref == 'refs/heads/development'
33+
runs-on: ubuntu-latest
34+
needs: [test]
35+
defaults:
36+
run:
37+
working-directory: ./terraform/dev
38+
steps:
39+
- uses: actions/checkout@v4
40+
- name: Google Cloud Auth
41+
uses: 'google-github-actions/auth@v2'
42+
with:
43+
project_id: ${{ env.PIPELINE_PROJECT_ID }}
44+
credentials_json: ${{ env.PIPELINE_SA_KEY }}
45+
46+
- uses: hashicorp/setup-terraform@v3
47+
48+
- name: Terraform fmt
49+
id: fmt
50+
run: terraform fmt -check
51+
continue-on-error: true
52+
53+
- name: Terraform Init
54+
id: init
55+
run: terraform init
56+
57+
- name: Terraform Validate
58+
id: validate
59+
run: terraform validate -no-color
60+
61+
- name: Terraform Plan
62+
id: plan
63+
run: |
64+
terraform plan -no-color -var="google_service_account_cloud_functions=${{ env.PIPELINE_GOOGLE_SERVICE_ACCOUNT_CLOUD_FUNCTIONS }}" \
65+
-var="google_service_account_api_gateway=${{ env.PIPELINE_GOOGLE_SERVICE_ACCOUNT_API_GATEWAY }}" \
66+
-var="project_database=${{ env.PIPELINE_PROJECT_DATABASE_DEV }}"
67+
continue-on-error: true
68+
69+
- name: Terraform Plan status
70+
if: steps.plan.outcome == 'failure'
71+
run: exit 1
72+
73+
- name: Terraform Apply
74+
id: apply
75+
run: |
76+
terraform apply -var="google_service_account_cloud_functions=${{ env.PIPELINE_GOOGLE_SERVICE_ACCOUNT_CLOUD_FUNCTIONS }}" \
77+
-var="google_service_account_api_gateway=${{ env.PIPELINE_GOOGLE_SERVICE_ACCOUNT_API_GATEWAY }}" \
78+
-var="project_database=${{ env.PIPELINE_PROJECT_DATABASE_DEV }}" \
79+
-auto-approve
80+
81+
deploy_production:
82+
if: github.ref == 'refs/heads/main'
83+
runs-on: ubuntu-latest
84+
needs: [test]
85+
defaults:
86+
run:
87+
working-directory: ./terraform/prod
88+
steps:
89+
- uses: actions/checkout@v4
90+
- name: Google Cloud Auth
91+
uses: 'google-github-actions/auth@v2'
92+
with:
93+
project_id: ${{ env.PIPELINE_PROJECT_ID }}
94+
credentials_json: ${{ env.PIPELINE_SA_KEY }}
95+
96+
- uses: hashicorp/setup-terraform@v3
97+
98+
- name: Terraform fmt
99+
id: fmt
100+
run: terraform fmt -check
101+
continue-on-error: true
102+
103+
- name: Terraform Init
104+
id: init
105+
run: terraform init
106+
107+
- name: Terraform Validate
108+
id: validate
109+
run: terraform validate -no-color
110+
111+
- name: Terraform Plan
112+
id: plan
113+
run: |
114+
terraform plan -no-color -var="google_service_account_cloud_functions=${{ env.PIPELINE_GOOGLE_SERVICE_ACCOUNT_CLOUD_FUNCTIONS }}" \
115+
-var="google_service_account_api_gateway=${{ env.PIPELINE_GOOGLE_SERVICE_ACCOUNT_API_GATEWAY }}" \
116+
-var="project_database=${{ env.PIPELINE_PROJECT_DATABASE_PROD }}"
117+
continue-on-error: true
118+
119+
- name: Terraform Plan status
120+
if: steps.plan.outcome == 'failure'
121+
run: exit 1
122+
123+
- name: Terraform Apply
124+
id: apply
125+
run: |
126+
terraform apply -var="google_service_account_cloud_functions=${{ env.PIPELINE_GOOGLE_SERVICE_ACCOUNT_CLOUD_FUNCTIONS }}" \
127+
-var="google_service_account_api_gateway=${{ env.PIPELINE_GOOGLE_SERVICE_ACCOUNT_API_GATEWAY }}" \
128+
-var="project_database=${{ env.PIPELINE_PROJECT_DATABASE_PROD }}" \
129+
-auto-approve
130+
131+

functions/adoption/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def dispatcher(request):
1010
if request.method == "OPTIONS":
1111
return respond_cors()
1212

13-
# Set CORS headers for the main request
1413
headers = {"Access-Control-Allow-Origin": "*"}
1514
args = request.args.to_dict()
1615

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
google_service_account_api_gateway = ""
2+
google_service_account_cloud_functions = ""
3+
project_database = ""
4+

terraform/dev/main.tf

Lines changed: 153 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,163 @@ provider "google" {
77

88
terraform {
99
backend "gcs" {
10-
bucket = var.project_bucket
10+
bucket = "tf-state-backingapi-20230314"
1111
prefix = "dev"
1212
}
1313
}
1414

15-
module "backend-api" {
16-
source = "./../modules/api-gateway"
17-
environment = "dev"
18-
project = "httparchive"
19-
region = "us-east1"
20-
service_account_email = var.google_service_account_api_gateway
15+
resource "google_api_gateway_api" "api" {
16+
provider = google-beta
17+
api_id = "api-gw-dev"
18+
display_name = "The dev API Gateway"
19+
project = "httparchive"
20+
}
21+
22+
# A Configuration, consisting of an OpenAPI specification
23+
resource "google_api_gateway_api_config" "api_config" {
24+
provider = google-beta
25+
api = google_api_gateway_api.api.api_id
26+
api_config_id_prefix = "api"
27+
project = "httparchive"
28+
display_name = "The dev Config"
29+
openapi_documents {
30+
document {
31+
path = "spec.yaml"
32+
contents = base64encode(<<-EOF
33+
swagger: "2.0"
34+
info:
35+
title: reports-backend-api
36+
description: API tech report
37+
version: 1.0.0
38+
schemes:
39+
- https
40+
produces:
41+
- application/json
42+
paths:
43+
/v1/categories:
44+
get:
45+
summary: categories
46+
operationId: getCategories
47+
x-google-backend:
48+
address: https://us-east1-httparchive.cloudfunctions.net/categories-dev
49+
deadline: 60
50+
# security:
51+
# - api_key: []
52+
responses:
53+
200:
54+
description: String
55+
/v1/adoption:
56+
get:
57+
summary: adoption
58+
operationId: getadoptionReports
59+
x-google-backend:
60+
address: https://us-east1-httparchive.cloudfunctions.net/adoption-dev
61+
deadline: 60
62+
# security:
63+
# - api_key: []
64+
responses:
65+
200:
66+
description: String
67+
/v1/page-weight:
68+
get:
69+
summary: pageWeight
70+
operationId: getpageWeight
71+
x-google-backend:
72+
address: https://us-east1-httparchive.cloudfunctions.net/page-weight-dev
73+
deadline: 60
74+
# security:
75+
# - api_key: []
76+
responses:
77+
200:
78+
description: String
79+
/v1/lighthouse:
80+
get:
81+
summary: lighthouse
82+
operationId: getLighthouseReports
83+
x-google-backend:
84+
address: https://us-east1-httparchive.cloudfunctions.net/lighthouse-dev
85+
deadline: 60
86+
# security:
87+
# - api_key: []
88+
responses:
89+
200:
90+
description: String
91+
/v1/cwv:
92+
get:
93+
summary: cwv
94+
operationId: getCwv
95+
x-google-backend:
96+
address: https://us-east1-httparchive.cloudfunctions.net/cwvtech-dev
97+
deadline: 60
98+
# security:
99+
# - api_key: []
100+
responses:
101+
200:
102+
description: String
103+
/v1/ranks:
104+
get:
105+
summary: ranks
106+
operationId: getRanks
107+
x-google-backend:
108+
address: https://us-east1-httparchive.cloudfunctions.net/ranks-dev
109+
deadline: 60
110+
# security:
111+
# - api_key: []
112+
responses:
113+
200:
114+
description: String
115+
/v1/geos:
116+
get:
117+
summary: geos
118+
operationId: getGeos
119+
x-google-backend:
120+
address: https://us-east1-httparchive.cloudfunctions.net/geos-dev
121+
deadline: 60
122+
# security:
123+
# - api_key: []
124+
responses:
125+
200:
126+
description: String
127+
/v1/technologies:
128+
get:
129+
summary: geos
130+
operationId: getTechnologies
131+
x-google-backend:
132+
address: https://us-east1-httparchive.cloudfunctions.net/technologies-dev
133+
deadline: 60
134+
# security:
135+
# - api_key: []
136+
responses:
137+
200:
138+
description: String
139+
EOF
140+
)
141+
}
142+
}
143+
gateway_config {
144+
backend_config {
145+
google_service_account = var.google_service_account_api_gateway
146+
}
147+
}
148+
}
149+
# The actual API Gateway
150+
resource "google_api_gateway_gateway" "gateway" {
151+
provider = google-beta
152+
project = "httparchive"
153+
region = "us-east1"
154+
api_config = google_api_gateway_api_config.api_config.id
155+
gateway_id = "dev-gw"
156+
display_name = "devApi Gateway"
157+
labels = {
158+
owner = "tech_report_api"
159+
environment = "dev"
160+
}
161+
depends_on = [google_api_gateway_api_config.api_config]
162+
lifecycle {
163+
replace_triggered_by = [
164+
google_api_gateway_api_config.api_config
165+
]
166+
}
21167
}
22168

23169
module "cwvtech" {

terraform/dev/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,3 @@ variable "project_database" {
1313
description = "The database name"
1414

1515
}
16-
17-
variable "project_bucket" {
18-
type = string
19-
description = "The project name"
20-
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
google_service_account_api_gateway = ""
2+
google_service_account_cloud_functions = ""
3+
project_database = ""
4+

0 commit comments

Comments
 (0)