-
Notifications
You must be signed in to change notification settings - Fork 32
/
cloudbuild-ci.yaml
183 lines (170 loc) · 4.93 KB
/
cloudbuild-ci.yaml
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
steps:
- id: "build image"
name: "gcr.io/cloud-builders/docker"
dir: "server"
args:
[
"build",
"-t",
"${_REGION}-${_ARTIFACT_REGISTRY_DOMAIN}/${PROJECT_ID}/${_REPO_NAME}/${_IMAGE_NAME}:${SHORT_SHA}",
"-f",
"Dockerfile.cloudbuild",
"."
]
- id: "push image"
name: "gcr.io/cloud-builders/docker"
args:
[
"push",
"${_REGION}-${_ARTIFACT_REGISTRY_DOMAIN}/${PROJECT_ID}/${_REPO_NAME}/${_IMAGE_NAME}:${SHORT_SHA}"
]
- id: "delete sql database if already present"
name: "gcr.io/cloud-builders/gcloud"
args:
[
"sql",
"databases",
"delete",
"${_GCP_CI_DATABASE_NAME}",
"--instance",
"${_SQL_INSTANCE_NAME}",
"--quiet"
]
- id: "create sql database"
name: "gcr.io/cloud-builders/gcloud"
args:
[
"sql",
"databases",
"create",
"${_GCP_CI_DATABASE_NAME}",
"--instance",
"${_SQL_INSTANCE_NAME}"
]
- id: "apply migrations"
name: "gcr.io/google-appengine/exec-wrapper"
dir: "server"
args:
[
"-i",
"${_REGION}-${_ARTIFACT_REGISTRY_DOMAIN}/${PROJECT_ID}/${_REPO_NAME}/${_IMAGE_NAME}:${SHORT_SHA}",
"-s",
"${PROJECT_ID}:${_REGION}:${_SQL_INSTANCE_NAME}",
"-e",
"SETTINGS_SECRETS_NAME=${_SECRET_SETTINGS_NAME}",
"-e",
"GCP_CI_DATABASE_URL=${_GCP_CI_DATABASE_URL}",
"--",
"python",
"manage.py",
"migrate",
]
- id: "collect static"
name: "gcr.io/google-appengine/exec-wrapper"
dir: "server"
args:
[
"-i",
"${_REGION}-${_ARTIFACT_REGISTRY_DOMAIN}/${PROJECT_ID}/${_REPO_NAME}/${_IMAGE_NAME}:${SHORT_SHA}",
"-s",
"${PROJECT_ID}:${_REGION}:${_SQL_INSTANCE_NAME}",
"-e",
"SETTINGS_SECRETS_NAME=${_SECRET_SETTINGS_NAME}",
"-e",
"GCP_CI_DATABASE_URL=${_GCP_CI_DATABASE_URL}",
"--",
"python",
"manage.py",
"collectstatic",
"--verbosity",
"2",
"--no-input",
]
- id: "deploy cloud run"
name: "gcr.io/cloud-builders/gcloud"
args:
[
"run",
"deploy",
"${_CLOUD_RUN_SERVICE_NAME}",
"--platform",
"managed",
"--region",
"${_REGION}",
"--image",
"${_REGION}-${_ARTIFACT_REGISTRY_DOMAIN}/${PROJECT_ID}/${_REPO_NAME}/${_IMAGE_NAME}:${SHORT_SHA}",
"--add-cloudsql-instances",
"${PROJECT_ID}:${_REGION}:${_SQL_INSTANCE_NAME}",
"--set-env-vars",
"SETTINGS_SECRETS_NAME=${_SECRET_SETTINGS_NAME}",
"--set-env-vars",
"GCP_CI_DATABASE_URL=${_GCP_CI_DATABASE_URL}",
"--allow-unauthenticated",
"--min-instances",
"${_CLOUD_RUN_MIN_INSTANCES}"
]
- id: "get cloud run endpoint"
name: "gcr.io/cloud-builders/gcloud"
entrypoint: "/bin/sh"
args:
[
"-c",
"echo $(gcloud run services describe ${_CLOUD_RUN_SERVICE_NAME} --region ${_REGION} --format 'value(status.url)') > /workspace/service-${_PR_NUMBER}.txt"
]
- id: "get super admin username and password"
name: "gcr.io/cloud-builders/gcloud"
entrypoint: "/bin/sh"
dir: "server"
args:
[
"-c",
"apt-get update && apt install jq -y && echo $(sh secret.sh -s ${_SECRET_SETTINGS_NAME} -k SUPERUSER_USERNAME)> /workspace/username-${_PR_NUMBER}.txt && echo $(sh secret.sh -s ${_SECRET_SETTINGS_NAME} -k SUPERUSER_PASSWORD) > /workspace/password-${_PR_NUMBER}.txt"
]
- id: "add seed values to the server"
name: "python"
entrypoint: "/bin/sh"
dir: "server"
args:
[
"-c",
"pip install -r test-requirements.txt --user && python seed.py --server $(cat /workspace/service-${_PR_NUMBER}.txt)"
]
- id: "delete cloud run"
name: "gcr.io/cloud-builders/gcloud"
args:
[
"run",
"services",
"delete",
"${_CLOUD_RUN_SERVICE_NAME}",
"--region",
"${_REGION}",
"--quiet"
]
- id: "delete sql database"
name: "gcr.io/cloud-builders/gcloud"
args:
[
"sql",
"databases",
"delete",
"${_GCP_CI_DATABASE_NAME}",
"--instance",
"${_SQL_INSTANCE_NAME}",
"--quiet"
]
substitutions:
_REGION: us-west1
_ARTIFACT_REGISTRY_DOMAIN: docker.pkg.dev
_REPO_NAME: medperf-repo
_IMAGE_NAME: medperf-api
_CLOUD_RUN_SERVICE_NAME: medperf-api-cb-ci-pr-${_PR_NUMBER}
_CLOUD_RUN_MIN_INSTANCES: '1'
_SECRET_SETTINGS_NAME: django-ci-settings
_SQL_INSTANCE_NAME: medperf-dev
##Settings specific for CI trigger##
##DB settings in secrets are overridden if DEPLOY_ENV is set to gcp-ci in secrets.
_GCP_CI_DATABASE_NAME: db-cb-ci-pr-${_PR_NUMBER}
#_GCP_CI_DATABASE_URL var is set in Cloud build trigger configuration using _GCP_CI_DATABASE_NAME
options:
dynamic_substitutions: true