-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeploy-stage.yml
More file actions
287 lines (266 loc) · 13.3 KB
/
deploy-stage.yml
File metadata and controls
287 lines (266 loc) · 13.3 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
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
parameters:
- name: jinja_templates
type: object
- name: depends_on
type: object
- name: stage_name
- name: apigee_organization
- name: environment
values:
- internal-dev
- internal-dev-sandbox
- internal-qa
- internal-qa-sandbox
- ref
- dev
- int
- sandbox
- prod
- name: aws_account
- name: service_name
- name: short_service_name
- name: fully_qualified_service_name
- name: service_base_path
- name: pr_label
default: ''
- name: secret_file_ids
type: object
- name: secret_ids
type: object
- name: config_ids
type: object
- name: proxy_path
- name: product_display_name
- name: product_description
- name: spec_file
- name: portal_api_requires_callback_url
- name: make_spec_visible
- name: friendly_api_name
- name: ping
type: boolean
- name: pre_template
type: stepList
- name: post_template
type: stepList
- name: pre_deploy
type: stepList
- name: post_deploy
type: stepList
- name: notify
type: boolean
default: false
- name: enable_monitoring
type: boolean
- name: enable_status_monitoring
type: boolean
- name: hosted_target_connection_path_suffix
type: string
- name: hosted_target_healthcheck_suffix
type: string
- name: _expose_blacklist
type: object
default:
- _expose_blacklist
- pre_template
- post_template
- pre_deploy
- post_deploy
- secret_file_ids
- secret_ids
- config_ids
- depends_on
- jinja_templates
- name: python_version
type: string
- name: agent_pool
type: string
stages:
- stage: ${{ parameters.stage_name }}
dependsOn: ${{ parameters.depends_on }}
jobs:
- deployment: deploy
environment: ${{ parameters.environment }}
pool:
name: ${{ parameters.agent_pool }}
workspace:
clean: all
strategy:
runOnce:
deploy:
steps:
- download: none
- bash: |
if [ ! -z "$(ls -A \"$(Pipeline.Workspace)/s/${{ parameters.service_name }}\" 2>/dev/null)" ]; then
echo "workspace directory is not empty!"
exit 1
fi
displayName: "check workspace is clean"
- task: DownloadPipelineArtifact@2
inputs:
source: "specific"
project: $(resources.pipeline.build_pipeline.projectID)
pipeline: $(resources.pipeline.build_pipeline.pipelineID)
preferTriggeringPipeline: true
runVersion: "latestFromBranch"
runBranch: $(Build.SourceBranch)
path: "$(Pipeline.Workspace)/s/${{ parameters.service_name }}"
- bash: |
instance_id="$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
echo instance-id: "${instance_id}"
echo connect to: https://eu-west-2.console.aws.amazon.com/systems-manager/session-manager/${instance_id}
echo sudo su - ubuntu
or
echo ssh ubuntu@${instance_id}
echo working directory: $(System.DefaultWorkingDirectory)
displayName: print aws info
- template: ../components/aws-assume-role.yml
parameters:
role: "auto-ops"
profile: "apm_${{ parameters.aws_account }}"
aws_account: "${{ parameters.aws_account }}"
- task: UsePythonVersion@0
displayName: "Use Python ${{ parameters.python_version }}"
inputs:
versionSpec: ${{ parameters.python_version }}
- template: "../components/set-facts.yml"
parameters:
service_name: ${{ parameters.service_name }}
secret_file_ids:
- ${{ each secret_file_id in parameters.secret_file_ids }}:
- ${{ secret_file_id }}
secret_ids:
- ${{ parameters.aws_account }}/azure-devops/apigee-${{ parameters.apigee_organization }}/APIGEE_OTP_KEY
- ${{ parameters.aws_account }}/azure-devops/apigee-${{ parameters.apigee_organization }}/APIGEE_PASSWORD
- ${{ parameters.aws_account }}/azure-devops/MONITORING_API_KEY
- ${{ parameters.aws_account }}/access-tokens/github/repo-status-update/GITHUB_ACCESS_TOKEN
- ${{ parameters.aws_account }}/monitoring/status-endpoint-api-key
- ${{ each secret_id in parameters.secret_ids }}:
- ${{ secret_id }}
config_ids:
- /${{ parameters.aws_account }}/azure-devops/apigee-${{ parameters.apigee_organization }}/APIGEE_USERNAME
- /${{ parameters.aws_account }}/azure-devops/env-${{ parameters.environment }}/ENV_URL
- /${{ parameters.aws_account }}/azure-devops/env-${{ parameters.environment }}/IDENTITY_URL
- /${{ parameters.aws_account }}/azure-devops/GITHUB_USER
- ${{ each config_id in parameters.config_ids }}:
- ${{ config_id }}
aws_account: ${{ parameters.aws_account }}
apigee_organization: ${{ parameters.apigee_organization }}
- ${{ if parameters.notify }}:
- bash: |
if [[ -z $(NOTIFY_COMMIT_SHA) ]]; then
export NOTIFY_COMMIT_SHA=`echo "$(Build.SourceVersionMessage)" | cut -d' ' -f2`
echo "##vso[task.setvariable variable=NOTIFY_COMMIT_SHA]$NOTIFY_COMMIT_SHA"
else
echo "##[debug]Using already provided NOTIFY_COMMIT_SHA=$(NOTIFY_COMMIT_SHA)"
fi
displayName: Set NOTIFY_COMMIT_SHA
- template: '../components/update-github-status.yml'
parameters:
state: pending
description: "Deploy to ${{ parameters.environment }} started"
environment: ${{ parameters.environment }}
- ${{ each param in parameters }}:
- ${{ if not(containsValue(parameters._expose_blacklist, param.key)) }}:
- bash: |
set -euo pipefail
echo "##vso[task.setvariable variable=${{ upper(param.key) }}]${{ param.value }}"
displayName: Setting ${{ upper(param.key) }}=${{ param.value }}
- bash: |
set -euo pipefail
echo "For backward compatibility..."
echo "##vso[task.setvariable variable=APIGEE_ENVIRONMENT]${{ parameters.environment }}"
displayName: Setting APIGEE_ENVIRONMENT=${{ parameters.environment }}
- bash: |
set -euo pipefail
if [[ ! -z $(UTILS_PR_NUMBER) ]]; then
export PR_LABEL="utils-pr-$(UTILS_PR_NUMBER)"
echo "##[debug]Triggered from utils repository, PR_LABEL=${PR_LABEL}"
echo "##vso[task.setvariable variable=PR_LABEL]${PR_LABEL}"
else
echo "##[debug]PR_NUMBER=${{ parameters.pr_label }}"
echo "##vso[task.setvariable variable=PR_LABEL]${{ parameters.pr_label }}"
fi
displayName: Override PR_LABEL
- bash: |
set -euo pipefail
if [[ ! -z $(UTILS_PR_NUMBER) ]]; then
if [[ "${{ parameters.proxy_path }}" == "live" ]]; then
export FULLY_QUALIFIED_SERVICE_NAME="${{ parameters.service_name }}-$(PR_LABEL)"
echo "##vso[task.setvariable variable=FULLY_QUALIFIED_SERVICE_NAME]${FULLY_QUALIFIED_SERVICE_NAME}"
else
export FULLY_QUALIFIED_SERVICE_NAME="${{ parameters.service_name }}-$(PR_LABEL)-${{ parameters.proxy_path }}"
echo "##vso[task.setvariable variable=FULLY_QUALIFIED_SERVICE_NAME]${FULLY_QUALIFIED_SERVICE_NAME}"
fi
echo "##[debug]Triggered from utils repository, FULLY_QUALIFIED_SERVICE_NAME=${FULLY_QUALIFIED_SERVICE_NAME}"
else
echo "##[debug]FULLY_QUALIFIED_SERVICE_NAME=${{ parameters.fully_qualified_service_name }}"
echo "##vso[task.setvariable variable=FULLY_QUALIFIED_SERVICE_NAME]${{ parameters.fully_qualified_service_name }}"
fi
displayName: Override FULLY_QUALIFIED_SERVICE_NAME
- bash: |
set -euo pipefail
if [[ ! -z $(UTILS_PR_NUMBER) ]]; then
export SERVICE_BASE_PATH="${{ parameters.service_base_path }}"
export SERVICE_BASE_PATH=${SERVICE_BASE_PATH/-pr-*/}-$(PR_LABEL)
echo "##vso[task.setvariable variable=SERVICE_BASE_PATH]${SERVICE_BASE_PATH}"
echo "##[debug]Triggered from utils repository, SERVICE_BASE_PATH=${SERVICE_BASE_PATH}"
else
echo "##[debug]SERVICE_BASE_PATH=${{ parameters.service_base_path }}"
echo "##vso[task.setvariable variable=SERVICE_BASE_PATH]${{ parameters.service_base_path }}"
fi
displayName: Override SERVICE_BASE_PATH
- bash: |
set -euo pipefail
INFO=$(curl https://api-registry.prod.api.platform.nhs.uk:9000/api/${{ parameters.service_name }})
SHORT_NAME=$(echo $INFO | jq -r .short_name)
GUID=$(echo $INFO | jq -r .guid)
echo "##[debug]Fetched info from API Registry"
echo "##[debug]short_name: $SHORT_NAME"
echo "##[debug]guid: $GUID"
if [[ $SHORT_NAME != "${{ parameters.short_service_name }}" ]]; then
echo "##[warning]Short name provided to pipeline (${{ parameters.short_service_name }}) does not match name in registry ($SHORT_NAME)"
echo "##vso[task.logissue type=warning]Short name provided to pipeline (${{ parameters.short_service_name }}) does not match name in registry ($SHORT_NAME)"
fi
displayName: Check supplied names against API registry
continueOnError: true
- template: '../templates/deploy-service.yml'
parameters:
service_name: ${{ parameters.service_name }}
short_service_name: ${{ parameters.short_service_name }}
fully_qualified_service_name: $(FULLY_QUALIFIED_SERVICE_NAME)
service_base_path: $(SERVICE_BASE_PATH)
stage_name: ${{ parameters.stage_name }}
jinja_templates: ${{ parameters.jinja_templates }}
apigee_environment: ${{ parameters.environment }}
apigee_organization: ${{ parameters.apigee_organization }}
product_display_name: ${{ parameters.product_display_name }}
product_description: ${{ parameters.product_description }}
hosted_target_connection_path_suffix: ${{ parameters.hosted_target_connection_path_suffix }}
hosted_target_healthcheck_suffix: ${{ parameters.hosted_target_healthcheck_suffix }}
proxy_path: ${{ parameters.proxy_path }}
pre_template: ${{ parameters.pre_template }}
post_template: ${{ parameters.post_template }}
pre_deploy: ${{ parameters.pre_deploy }}
post_deploy: ${{ parameters.post_deploy }}
ping: ${{ parameters.ping }}
enable_monitoring: ${{ parameters.enable_monitoring }}
enable_status_monitoring : ${{ parameters.enable_status_monitoring }}
portal_api_requires_callback_url: ${{ parameters.portal_api_requires_callback_url }}
make_spec_visible: ${{ parameters.make_spec_visible }}
friendly_api_name: ${{ parameters.friendly_api_name }}
spec_file: ${{ parameters.spec_file }}
pr_label: $(PR_LABEL)
aws_account: ${{ parameters.aws_account }}
- ${{ if parameters.notify }}:
- template: '../components/update-github-status.yml'
parameters:
state: success
on_success: true
description: "Deploy to ${{ parameters.environment }} succeeded"
environment: ${{ parameters.environment }}
- template: '../components/update-github-status.yml'
parameters:
state: failure
on_failure: true
description: "Deploy to ${{ parameters.environment }} failed"
environment: ${{ parameters.environment }}