-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_test_setup.yaml
More file actions
156 lines (139 loc) · 4.7 KB
/
e2e_test_setup.yaml
File metadata and controls
156 lines (139 loc) · 4.7 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
steps:
- id: "Create a dedicated database"
name: "gcr.io/cloud-builders/gcloud"
entrypoint: "/bin/bash"
args:
- "-c"
- |
WAIT=120 ./retry.sh "gcloud sql databases create ${_DB_NAME} \
--instance ${_DB_INSTANCE} \
--project ${PROJECT_ID}"
- id: "Create a dedicated database user"
name: "gcr.io/cloud-builders/gcloud"
entrypoint: "/bin/bash"
args:
- "-c"
- |
echo -n "${_DB_PASS}" > db_password
WAIT=120 ./retry.sh "gcloud sql users create ${_DB_USER} \
--password $(cat db_password) \
--instance ${_DB_INSTANCE} \
--project ${PROJECT_ID}"
rm db_password
- id: "Create a dedicated storage bucket"
name: "gcr.io/cloud-builders/gcloud"
entrypoint: "/bin/bash"
args:
- "-c"
- |
./retry.sh "gsutil mb \
-l ${_REGION} \
-p ${PROJECT_ID} \
gs://${_STORAGE_BUCKET}"
- id: "Add Django secrets to Secret Manager"
name: "gcr.io/cloud-builders/gcloud"
entrypoint: "/bin/bash"
args:
- "-c"
- |
echo "
DATABASE_URL=postgres://${_DB_USER}:${_DB_PASS}@//cloudsql/${_CLOUD_SQL_CONNECTION_NAME}/${_DB_NAME}
GS_BUCKET_NAME=${_STORAGE_BUCKET}
SECRET_KEY=$(cat /dev/urandom | LC_ALL=C tr -dc '[:alpha:]' | fold -w 30 | head -n1)
PASSWORD_NAME=${_SECRET_PASSWORD_NAME}" > ${_SECRET_SETTINGS_NAME}
./retry.sh "gcloud secrets create ${_SECRET_SETTINGS_NAME} \
--project $PROJECT_ID \
--data-file=${_SECRET_SETTINGS_NAME}"
gcloud secrets add-iam-policy-binding ${_SECRET_SETTINGS_NAME} \
--member serviceAccount:$(gcloud projects list --filter "name=${PROJECT_ID}" --format "value(projectNumber)")@cloudbuild.gserviceaccount.com \
--role roles/secretmanager.secretAccessor \
--project ${PROJECT_ID}
rm ${_SECRET_SETTINGS_NAME}
echo -n "${_SECRET_PASSWORD_VALUE}" > ${_SECRET_PASSWORD_NAME}
./retry.sh "gcloud secrets create ${_SECRET_PASSWORD_NAME} \
--project $PROJECT_ID \
--data-file=${_SECRET_PASSWORD_NAME}"
gcloud secrets add-iam-policy-binding ${_SECRET_PASSWORD_NAME} \
--member serviceAccount:$(gcloud projects list --filter "name=${PROJECT_ID}" --format "value(projectNumber)")@cloudbuild.gserviceaccount.com \
--role roles/secretmanager.secretAccessor \
--project ${PROJECT_ID}
rm ${_SECRET_PASSWORD_NAME}
- id: "Build Container Image"
name: "gcr.io/cloud-builders/docker"
entrypoint: "/bin/bash"
args:
- "-c"
- |
./retry.sh "docker build -t gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION} ."
- id: "Push Container Image"
name: "gcr.io/cloud-builders/docker"
entrypoint: "/bin/bash"
args:
- "-c"
- |
./retry.sh "docker push gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION}"
- id: "Migrate database"
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i",
"gcr.io/$PROJECT_ID/${_SERVICE}:${_VERSION}",
"-s",
"${_CLOUD_SQL_CONNECTION_NAME}",
"-e",
"SETTINGS_NAME=${_SECRET_SETTINGS_NAME}",
"-e",
"PASSWORD_NAME=${_SECRET_PASSWORD_NAME}",
"--",
"python",
"manage.py",
"migrate",
]
- id: "Collect static"
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i",
"gcr.io/$PROJECT_ID/${_SERVICE}:${_VERSION}",
"-s",
"${_CLOUD_SQL_CONNECTION_NAME}",
"-e",
"SETTINGS_NAME=${_SECRET_SETTINGS_NAME}",
"--",
"python",
"manage.py",
"collectstatic",
"--verbosity",
"2",
"--no-input",
]
- id: "Deploy to Cloud Run"
name: "gcr.io/cloud-builders/gcloud:latest"
entrypoint: /bin/bash
args:
- "-c"
- |
./retry.sh "gcloud run deploy ${_SERVICE} \
--project $PROJECT_ID \
--image gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION} \
--no-allow-unauthenticated \
--region ${_REGION} \
--platform ${_PLATFORM} \
--add-cloudsql-instances ${_CLOUD_SQL_CONNECTION_NAME} \
--update-env-vars SETTINGS_NAME=${_SECRET_SETTINGS_NAME}"
images:
- gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION}
substitutions:
_SERVICE: django
_VERSION: manual
_REGION: us-central1
_PLATFORM: managed
_STORAGE_BUCKET: ${PROJECT_ID}-bucket
_DB_INSTANCE: django-instance
_CLOUD_SQL_CONNECTION_NAME: $PROJECT_ID:us-central1:django-instance
_DB_NAME: postgres
_DB_USER: postgres
_DB_PASS: password1234
_SECRET_SETTINGS_NAME: django_settings
_SECRET_PASSWORD_NAME: admin_password
_SECRET_PASSWORD_VALUE: password