-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.yml
215 lines (203 loc) · 8.17 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
version: 2.1
orbs:
python: circleci/python@0.2.1
codecov: codecov/codecov@3.2.4
jobs:
install_and_update_dependencies:
docker:
- image: cimg/python:3.12
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v7-dependencies-{{ checksum "Pipfile.lock" }}
- v7-dependencies-default
- run: &install-pipenv pip install -U pipenv pip --quiet --no-input
- run: pipenv sync --dev
- run: pipenv check # before save_cache so an insecure cache is never saved
- run: make lambda-layers/DependenciesLayer/requirements.txt
- persist_to_workspace:
root: ~/repo/
paths: [ lambda-layers/DependenciesLayer/requirements.txt ]
- save_cache:
when: on_success
paths:
- ~/.local/share/virtualenvs/
key: v7-dependencies-{{ checksum "Pipfile.lock" }}
run-tests:
docker:
- image: cimg/python:3.12
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v7-dependencies-{{ checksum "Pipfile.lock" }}
- v7-dependencies-default
- run: *install-pipenv
- run:
name: Ruff Check
command: pipenv run ruff check .
- run:
name: Ruff format
command: pipenv run ruff format . --check
- run: pipenv run djhtml --check .
- run: pipenv run pipenv verify
- run: pipenv run collectstatic
- run:
name: Run Tests
command: pipenv run pytest --junitxml=test-results/junit.xml --cov --ruff
- store_artifacts:
path: test-results
destination: test-results
- store_test_results:
path: test-results
- codecov/upload:
file: /home/circleci/repo/test-results/junit.xml
sam_build:
docker:
- image: public.ecr.aws/sam/build-python3.12:latest
working_directory: ~/repo
steps:
- checkout
- attach_workspace:
at: ~/repo/
- run: pip install --upgrade pip setuptools
- run: pip install -r lambda-layers/DependenciesLayer/requirements.txt
- run: make collectstatic
- run: sam build ${DASH_DASH_DEBUG}
- persist_to_workspace:
root: ~/repo/
paths: [ .aws-sam/build/ ]
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
SAM_LAMBDA_CONFIG_ENV: <<parameters.dc-environment>>
SAM_PUBLIC_CONFIG_ENV: <<parameters.dc-environment>>-public-access
SAM_LAMBDA_FUNCTION_CONFIG_ENV: <<parameters.dc-environment>>-cloudfront-lambda-functions
DC_DEPLOY_NAME: <<parameters.dc-deploy-name>>
POSTGRES_DATABASE_NAME: <<parameters.dc-deploy-name>>
steps:
- checkout
- attach_workspace:
at: ~/repo/
- restore_cache:
keys:
- v7-dependencies-{{ checksum "Pipfile.lock" }}
- v7-dependencies-default
- run: *install-pipenv
- run: pip install aws-sam-cli
- run: printenv DC_DEPLOY_NAME DJANGO_SETTINGS_MODULE SAM_CONFIG_FILE SAM_LAMBDA_CONFIG_ENV 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=$POSTGRES_PASSWORD createdb --host $POSTGRES_HOST --username postgres $POSTGRES_DATABASE_NAME || true
- run:
name: "pipenv run sam deploy # App: Lambda + API Gateway"
command: |
pipenv run sam deploy ${DASH_DASH_DEBUG} \
--config-file ~/repo/${SAM_CONFIG_FILE} \
--config-env $SAM_LAMBDA_CONFIG_ENV \
--template-file ~/repo/.aws-sam/build/template.yaml \
--parameter-overrides " \
AppDjangoSettingsModule=$DJANGO_SETTINGS_MODULE \
AppSecretKey='$SECRET_KEY' \
AppSentryDSN='$SENTRY_DSN' \
GitHash='$CIRCLE_SHA1' \
AppIsBehindCloudFront=True \
AppLogRetentionDays=60 \
AppPostgresHost='$POSTGRES_HOST' \
AppPostgresDatabaseName='$POSTGRES_DATABASE_NAME' \
AppPostgresPassword='$POSTGRES_PASSWORD' \
AppWidgetS3URL='$WIDGET_S3_URL' \
AppDCAPIToken='$DC_API_TOKEN' \
AppSamLambdaConfigEnv='$SAM_LAMBDA_CONFIG_ENV' \
AppDomain='$PUBLIC_FQDN' \
"
no_output_timeout: 30m
- run: pipenv run migratedb
- run:
name: "sam deploy lambda@edge function"
command: |
pipenv run sam deploy \
--config-file ~/repo/${SAM_CONFIG_FILE} \
--config-env $SAM_LAMBDA_FUNCTION_CONFIG_ENV
- run:
name: printenv PUBLIC_FQDN CERTIFICATE_ARN
# These envvars are stored inside CircleCI, which helpfully masks them if they're echoed.
command: |
#
# NB Do *not* copy any of these string values!
# -> In order to make them visible inside CircleCI, a null character
# -> has been inserted between characters 1 and 2, which may have
# -> unintended consequences if you copy & paste them.
#
printenv PUBLIC_FQDN CERTIFICATE_ARN | sed -E 's/^(.)(.)/\1\x00\2/'
- 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 \
AuthHeaderToQueryStringARN=`python .circleci/get_cf_export_value.py AuthHeaderToQueryString` \
"
- 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: ec_api.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-ec-api ]
filters: { branches: { only: [ /stage-deploy-.*/, main ] } }
- sam_deploy:
name: sam_deploy_production
dc-environment: production
dc-deploy-name: production
dc-django-settings-module: ec_api.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-ec-api ]
filters: { branches: { only: [ main ] } }