-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudbuild.yaml
68 lines (62 loc) · 2.48 KB
/
cloudbuild.yaml
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
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
steps:
# Docker Build
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t',
'us-central1-docker.pkg.dev/${PROJECT_ID}/${_REPO_NAME}/helloworld-empathy',
'.']
# Docker Push
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'us-central1-docker.pkg.dev/${PROJECT_ID}/${_REPO_NAME}/helloworld-empathy']
# Deploy to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
args: ['run', 'deploy', 'helloworld-empathy',
'--image=us-central1-docker.pkg.dev/${PROJECT_ID}/${_REPO_NAME}/helloworld-empathy',
'--region', 'us-central1', '--platform', 'managed']
# Get Cloud Run URL
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: '/bin/bash'
args:
- '-c'
- |
cloud_run_url=$(gcloud run services describe helloworld-empathy \
--format 'value(status.url)' --platform managed --region us-central1)
echo $cloud_run_url > _service_url
# Get identity token
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: '/bin/bash'
args:
- '-c'
- |
get_id_token() {
curl -X POST -H "content-type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-d "{\"audience\": \"$(cat _service_url)\"}" \
"https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${_SERVICE_ACCOUNT}:generateIdToken" | \
python3 -c "import sys, json; print(json.load(sys.stdin)['token'])"
}
echo $(get_id_token) > _id_token
# Make an authenticated request to the Cloud Run URL and verify response is as expected
- name: 'gcr.io/cloud-builders/curl'
entrypoint: '/bin/bash'
args:
- '-c'
- |
set -e
msg=$(curl -si --show-error -H "Authorization: Bearer $(cat _id_token)" $(cat _service_url))
if grep -q "Hello World" <<< "$msg"; then
echo "Test: PASSED"
else echo $msg && exit 1
fi