-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
config.yml
251 lines (231 loc) · 9.43 KB
/
config.yml
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
version: 2.1
orbs:
python: circleci/python@2.1.1
jobs:
install_and_update_dependencies:
docker:
- image: cimg/python:3.12-node
working_directory: ~/repo
executor: python/default
steps:
- checkout
- restore_cache:
keys:
- v7-dependencies-{{ checksum "Pipfile.lock" }}-{{ checksum "thumbs/Pipfile.lock" }}
- v7-dependencies-default
# Ensure pip and pipenv are installed
- run:
name: Install pipenv
command: |
pip install --upgrade pipenv==2024.0.1 pip --quiet --no-input
- run:
name: Set up virtual environment
command: |
pipenv sync --dev
- run: make lambda-layers/DependenciesLayer/requirements.txt
- run: pipenv run collectstatic
- run: npm install
- run: npm run build
- run: DJANGO_SETTINGS_MODULE=electionleaflets.settings.base_lambda pipenv run collectstatic
- persist_to_workspace:
root: ~/repo/
paths:
- .aws-sam/build/
- thumbs/.aws-sam/build/
- lambda-layers/DependenciesLayer/requirements.txt
- electionleaflets/static/
- save_cache:
when: on_success
paths:
- ~/.local/share/virtualenvs/
key: v7-dependencies-{{ checksum "Pipfile.lock" }}-{{ checksum "thumbs/Pipfile.lock" }}
run-tests:
docker:
- image: cimg/python:3.12
environment:
CIRCLECI: true
PGHOST: 127.0.0.1
- image: cimg/postgres:16.3
environment:
POSTGRES_USER: electionleaflets
POSTGRES_DB: electionleaflets
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v7-dependencies-{{ checksum "Pipfile.lock" }}
- run: pipenv run pipenv verify
- run: pipenv run python manage.py check
# TODO: enable this once we drop uk_political_parties package
#- run: pipenv run python manage.py makemigrations --check
# Run tests
- run:
name: Run tests
command: |
pipenv run test
- store_artifacts:
path: test-results
destination: test-results
- store_test_results:
path: test-results
sam_build:
docker:
- image: public.ecr.aws/sam/build-python3.12
working_directory: ~/repo
steps:
- checkout
- attach_workspace:
at: ~/repo/
- run: pip install --upgrade pip
- run: pip install -r deploy-requirements.txt
- run: pip install -r lambda-layers/DependenciesLayer/requirements.txt
- run: sam build ${DASH_DASH_DEBUG}
- persist_to_workspace:
root: ~/repo/
paths:
- .aws-sam/build/
- thumbs/.aws-sam/build/
- lambda-layers/DependenciesLayer/requirements.txt
- electionleaflets/static/
sam_deploy:
docker:
- image: cimg/python:3.12
working_directory: ~/repo/
parameters:
dc-environment:
type: enum
enum: [ development, staging, production ]
dc-django-settings-module:
type: string
dc-deploy-name:
type: string
environment:
DJANGO_SETTINGS_MODULE: "<<parameters.dc-django-settings-module>>"
SAM_CONFIG_FILE: samconfig.toml.d/ci-<<parameters.dc-environment>>.toml
DC_ENVIRONMENT: <<parameters.dc-environment>>
SAM_PUBLIC_CONFIG_ENV: <<parameters.dc-environment>>-public-access
DC_DEPLOY_NAME: <<parameters.dc-deploy-name>>
POSTGRES_DATABASE_NAME: <<parameters.dc-deploy-name>>
LEAFLET_IMAGES_BUCKET_NAME: "electionleaflets-images-<<parameters.dc-deploy-name>>"
steps:
- checkout
- attach_workspace:
at: ~/repo/
- restore_cache:
keys:
- v7-dependencies-{{ checksum "Pipfile.lock" }}
- v7-dependencies-default
- run:
name: Install pipenv
command: |
pip install --upgrade pipenv==2024.0.1 pip --quiet --no-input
- run: pip install -r deploy-requirements.txt
- run: printenv DC_DEPLOY_NAME DJANGO_SETTINGS_MODULE SAM_CONFIG_FILE DC_ENVIRONMENT SAM_PUBLIC_CONFIG_ENV
- run: printenv SECRET_KEY | md5sum
- run: printenv AWS_ACCESS_KEY_ID | md5sum
- run: sudo apt update && sudo apt install postgresql-client
- run: PGPASSWORD=$DATABASE_PASS createdb --host $DATABASE_HOST --username $DATABASE_USER $POSTGRES_DATABASE_NAME || true
- run:
name: "pipenv run sam deploy # App: Lambda + API Gateway"
command: |
sam deploy ${DASH_DASH_DEBUG} \
--config-file ~/repo/${SAM_CONFIG_FILE} \
--config-env $DC_ENVIRONMENT \
--template-file ~/repo/.aws-sam/build/template.yaml \
--parameter-overrides " \
AppDjangoSettingsModule=$DJANGO_SETTINGS_MODULE \
AppSecretKey='$SECRET_KEY' \
AppSentryDSN='$SENTRY_DSN' \
AppDCAPIToken='$DEVS_DC_AUTH_TOKEN' \
GitHash='$CIRCLE_SHA1' \
AppIsBehindCloudFront=True \
AppLogRetentionDays=60 \
AppPostgresHost='$DATABASE_HOST' \
AppPostgresDatabaseName='$POSTGRES_DATABASE_NAME' \
AppPostgresPassword='$DATABASE_PASS' \
AppDomain='$PUBLIC_FQDN' \
AppLeafletImagesBucketName='$LEAFLET_IMAGES_BUCKET_NAME' \
"
- run: pipenv run migratedb
- run: pipenv run createcachetable
- run: printenv LEAFLET_IMAGES_BUCKET_NAME
- run: echo $LEAFLET_IMAGES_BUCKET_NAME > thumbs/LEAFLET_IMAGES_BUCKET_NAME
- run: make thumbs/requirements.txt
- run: pipenv run thumbs_sam_build
- run: pipenv run sam validate --region eu-west-2
- run:
name: "Deploy thumbs function"
command: |
pipenv run sam deploy ${DASH_DASH_DEBUG} \
--config-file ~/repo/thumbs/${SAM_CONFIG_FILE} \
--config-env $DC_ENVIRONMENT \
--template-file ~/repo/thumbs/.aws-sam/build/ThumbsFunction/template.yml \
--region eu-west-2 \
--s3-bucket=$THUMBS_DEPLOY_BUCKET_NAME
- run:
name: "Deploy thumbs function to eu-west-1 for lambda@edge"
command: |
pipenv run sam deploy ${DASH_DASH_DEBUG} \
--config-file ~/repo/thumbs/${SAM_CONFIG_FILE} \
--config-env $DC_ENVIRONMENT \
--template-file ~/repo/thumbs/.aws-sam/build/ThumbsFunction/template.yml \
--region us-east-1
- run:
name: "pipenv run sam deploy # Public access: CDN + DNS"
no_output_timeout: 20m # CloudFront can take longer than CircleCI's 10m default
command: |
pipenv run sam deploy ${DASH_DASH_DEBUG} \
--config-file ~/repo/${SAM_CONFIG_FILE} \
--config-env $SAM_PUBLIC_CONFIG_ENV \
--template-file ~/repo/public-access-template.yaml \
--parameter-overrides " \
StackNameSuffix=<<parameters.dc-environment>> \
CertificateArn=$CERTIFICATE_ARN \
PublicFqdn=$PUBLIC_FQDN \
LeafletsBucketName=$LEAFLET_IMAGES_BUCKET_NAME
"
- run: pip install -U boto3 pipenv==2024.0.1 --quiet --no-input
- run: python thumbs/attach_lambda_triggers.py
- run:
name: "Publish a new Sentry Release"
command: |
curl -sL https://sentry.io/get-cli/ | bash
sentry-cli releases --org democracy-club-gp new $CIRCLE_SHA1 --project ec-api
sentry-cli releases --org democracy-club-gp set-commits --auto $CIRCLE_SHA1
sentry-cli releases --org democracy-club-gp finalize $CIRCLE_SHA1
workflows:
main:
jobs:
- install_and_update_dependencies
- run-tests:
requires:
- install_and_update_dependencies # tests need installed dev package set
- sam_build:
requires:
- install_and_update_dependencies # DependenciesLayer build needs lambda-layers/DependenciesLayer/requirements.txt
- run-tests # Function build needs aggregator/apps/api/v1/templates/api_docs_rendered.html
- sam_deploy:
name: sam_deploy_staging
dc-environment: staging
dc-deploy-name: staging
dc-django-settings-module: electionleaflets.settings.base_lambda
requires:
- install_and_update_dependencies # SAM CLI is in the dev package set
- run-tests # staging should only deploy if tests pass
- sam_build # deploy needs .aws-sam/build/
context: [ deployment-staging-election-leaflets ]
filters: { branches: { only: [ master ] } }
- sam_deploy:
name: sam_deploy_production
dc-environment: production
dc-deploy-name: production
dc-django-settings-module: electionleaflets.settings.base_lambda
requires:
- install_and_update_dependencies # SAM CLI is in the dev package set
- run-tests # staging should only deploy if tests pass
- sam_build # deploy needs .aws-sam/build/
- sam_deploy_staging
context: [ deployment-production-election-leaflets ]
filters: { branches: { only: [ master ] } }