-
Notifications
You must be signed in to change notification settings - Fork 37
/
orb.yml
330 lines (318 loc) · 10 KB
/
orb.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
version: 2.1
description: "Common deploy scripts for IPA team services"
orbs:
serverless: circleci/[email protected]
slack: circleci/[email protected]
node: circleci/[email protected]
snyk: snyk/[email protected]
aws-configure-credentials-oidc: ovotech/[email protected]
executors:
node-runner:
docker:
- image: cimg/node:20.12.2
resource_class: large
commands:
load_templates:
description: "Loads custom Slack alert failed deploy template for use with slack orb for deployment notifications."
parameters:
deploy_failed_template:
description: "Template to use when deployment fails."
type: string
default: |
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": ":alert: Failed ${STAGE} deployment . :blob-fail: ",
"emoji": true
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Job*: ${CIRCLE_JOB}"
}
]
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Project*: $CIRCLE_PROJECT_REPONAME"
},
{
"type": "mrkdwn",
"text": "*Branch*: $CIRCLE_BRANCH"
},
{
"type": "mrkdwn",
"text": "*Author*: $CIRCLE_USERNAME"
}
],
"accessory": {
"type": "image",
"image_url": "https://assets.brandfolder.com/otz5mn-bw4j2w-6jzqo8/original/circle-logo-badge-black.png",
"alt_text": "CircleCI logo"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Mentions*: $SLACK_PARAM_MENTIONS"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View Job"
},
"url": "${CIRCLE_BUILD_URL}"
}
]
}
]
}
steps:
- attach_workspace:
at: .
- run:
name: Save template to file
command: |
echo Saving template to file
mkdir -p /tmp/ipa-deploy
echo '<< parameters.deploy_failed_template >>' > /tmp/ipa-deploy/failed_deploy_template.json
- run:
name: Export template to environment variable
command: |
echo $(cat /tmp/ipa-deploy/failed_deploy_template.json)
echo 'export SLACK_DEPLOY_FAILED_TEMPLATE=$(cat /tmp/ipa-deploy/failed_deploy_template.json)' >> $BASH_ENV
npm_ci:
description: Install node dependencies
parameters:
executor:
type: string
description: The executor used. Needed to avoid conflicting caches
steps:
- restore_cache:
keys:
- v1-dependencies-"<<parameters.executor>>"-{{ "package.json" }}
- v1-dependencies-
- run:
name: "Set NPM token"
command: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
- run:
name: "Install NPM dependencies."
command: |
npm ci
- save_cache:
paths:
- node_modules
key: v1-dependencies-"<<parameters.executor>>"-{{ checksum "package.json" }}
notify-slack-on-failure:
description: Sends a slack notification if the job fails
parameters:
channel:
type: string
description: The channel to send the notification to
mentions:
type: string
description: The Slack users/groups to mention in the message
default: ""
steps:
- attach_workspace:
at: .
- load_templates
- slack/notify:
event: fail
channel: <<parameters.channel>>
mentions: <<parameters.mentions>>
template: SLACK_DEPLOY_FAILED_TEMPLATE
deploy-run-integration-tests:
description: Run integration tests
parameters:
executor:
type: string
description: The executor used. Needed to avoid conflicting caches
stage:
type: string
default: integration
region:
default: ""
type: string
steps:
- attach_workspace:
at: .
- restore_cache:
keys:
- v1-dependencies-"<<parameters.executor>>"-{{ "package.json" }}
- v1-dependencies-
- aws-configure-credentials-oidc/aws-configure-credentials:
role-arn: $CIRCLE_IAM_ROLE_ARN
- run:
name: "Set NPM token"
command: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
- run:
name: "Install NPM dependencies."
command: |
npm ci
- run:
name: "Install serverless CLI"
command: sudo npm install -g [email protected]
- run:
command: |
export DEPLOYMENT_TYPE=integrationTest
serverless deploy --stage ${STAGE} --region ${AWS_REGION}
name: deploy
- run:
name: "Integration tests"
command: |
npm run integrationTest
- save_cache:
paths:
- node_modules
key: v1-dependencies-"<<parameters.executor>>"-{{ checksum "package.json" }}
jobs:
checkout:
executor: serverless/default
steps:
- checkout
- persist_to_workspace:
root: .
paths:
- .
eslint:
executor: node-runner
steps:
- run:
name: Print version
command: npm --version
- attach_workspace:
at: .
- npm_ci:
executor: node-runner
- run:
name: Run linting
command: npm run lint
snyk-scan:
executor: node-runner
steps:
- attach_workspace:
at: .
- snyk/scan:
additional-arguments: --dev=true
fail-on-issues: true
severity-threshold: high
jest-tests:
executor: node-runner
steps:
- attach_workspace:
at: .
- npm_ci:
executor: node-runner
- run:
name: "Run tests"
command: npm run test
deploy:
parameters:
stage:
type: string
default: sandbox
region:
default: ""
type: string
branch:
default: ""
type: string
executor: node-runner
steps:
- attach_workspace:
at: .
- run:
name: "Set NPM token"
command: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
- aws-configure-credentials-oidc/aws-configure-credentials:
role-arn: $CIRCLE_IAM_ROLE_ARN
- run:
name: "Install Serverless CLI"
command: sudo npm install -g [email protected]
- run:
name: Setting stage from branch name
command: |
if [[ << parameters.branch >> =~ ^(IPA-[0-9]+).*$ ]]; then
echo "export STAGE=${BASH_REMATCH[1]}" >> $BASH_ENV;
elif [[ << parameters.branch >> =~ ^main$ ]]; then
echo "export STAGE=<< parameters.stage >>" >> $BASH_ENV
else
echo 'export STAGE="sandbox"' >> $BASH_ENV;
fi
echo ${STAGE}
- run:
name: "Install NPM dependencies."
command: npm ci
- run:
command: |
export DEPLOYMENT_TYPE=normal
serverless deploy --stage ${STAGE} --region ${AWS_REGION}
name: deploy
- when:
condition:
or:
- equal: [prod, << parameters.stage >>]
- equal: [nonprod, << parameters.stage >>]
steps:
- notify-slack-on-failure:
channel: "ipa-alerts-<< parameters.stage >>"
delete-feature-branch-stack:
executor: node-runner
parameters:
region:
default: ""
type: string
steps:
- attach_workspace:
at: .
- aws-configure-credentials-oidc/aws-configure-credentials:
role-arn: $CIRCLE_IAM_ROLE_ARN
- npm_ci:
executor: node-runner
- run:
name: Remove stack created by feature branch
command: |
COMMIT_MESSAGE=`curl -H "Authorization: token ${GIT_TOKEN}" -s https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/git/commits/${CIRCLE_SHA1} | jq -r '.message'`
if [[ $COMMIT_MESSAGE =~ ^Merge" "pull" "request" "#[0-9]+" "from" "ovotech\/(IPA-[0-9]+).*$ ]]; then
sudo npm i -g serverless
serverless remove --stage ${BASH_REMATCH[1]} --region ${AWS_REGION} || true
fi
delete-feature-branch-integration-stack:
executor: node-runner
parameters:
region:
default: ""
type: string
steps:
- attach_workspace:
at: .
- aws-configure-credentials-oidc/aws-configure-credentials:
role-arn: $CIRCLE_IAM_ROLE_ARN
- npm_ci:
executor: node-runner
- run:
name: Remove stack created by feature branch
command: |
COMMIT_MESSAGE=`curl -H "Authorization: token ${GIT_TOKEN}" -s https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/git/commits/${CIRCLE_SHA1} | jq -r '.message'`
if [[ $COMMIT_MESSAGE =~ ^Merge" "pull" "request" "#[0-9]+" "from" "ovotech\/(IPA-[0-9]+).*$ ]]; then
sudo npm i -g serverless
serverless remove --stage ${BASH_REMATCH[1]}-integration --region ${AWS_REGION} || true
fi