-
Notifications
You must be signed in to change notification settings - Fork 7
/
cloudbuild.yml
46 lines (38 loc) · 1.53 KB
/
cloudbuild.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
steps:
# build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/votewell/api', '.']
# push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/votewell/api']
# Deploy container image to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
args: ['beta', 'run', 'deploy', 'api', '--image', 'gcr.io/votewell/api', '--region', 'us-east1','--platform', 'managed', '--quiet']
# clean out old images
- name: 'gcr.io/cloud-builders/gcloud-slim'
entrypoint: 'bash'
args:
- '-c'
- |
set -e
apt update && apt install -y jq
images="api"
for image in ${images[@]}; do
echo "Cleaning image ${image}"
c=0
image="gcr.io/$PROJECT_ID/${image}"
number_keep_images=3
date=$(gcloud container images list-tags $image --limit=unlimited \
--sort-by=~TIMESTAMP --format=json | TZ=/usr/share/zoneinfo/UTC jq -r '.['$number_keep_images'].timestamp.datetime | sub("(?<before>.*):"; .before ) | strptime("%Y-%m-%d %H:%M:%S%z") | mktime | strftime("%Y-%m-%d")')
for digest in $(gcloud container images list-tags $image --limit=unlimited --sort-by=~TIMESTAMP \
--filter="timestamp.datetime < '${date}'" --format='get(digest)'); do
(
set -x
gcloud container images delete -q --force-delete-tags "${image}@${digest}"
)
c=c+1
done
echo "Deleted ${c} images for ${image}." >&2
done
images:
- gcr.io/votewell/api