-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudbuild.yaml
45 lines (37 loc) · 2.5 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
# In order to merge all the deployment steps into a single command, we can use Cloud Build.
# This file defines the steps that will be followed by Cloud Build to build our application.
# Check this documentation on how to tag and push to Artifact Registry:
# https://cloud.google.com/artifact-registry/docs/docker/pushing-and-pulling#pushing
# I have moved all the hardcoded names to `substitutions`.
# The idea is that Cloud Build will take the variables from the `substitutions` attribute if no other values have been specified.
# More on this: https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values#using_user-defined_substitutions
# With this change any user should be able to select the region they want as well as the repositories and image names by changing those values.
steps:
# Use Docker to build and tag the image.
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', '${_ARTIFACT_REGION}-docker.pkg.dev/$PROJECT_ID/${_ARTIFACT_REPO}/${_DOCKER_IMAGE}', '.']
# Push the Docker image to Artifact Registry. This way we publish the image to the rest of the team and Cloud Run will be able to retrieve it.
- name: 'gcr.io/cloud-builders/docker'
args: ['push', '${_ARTIFACT_REGION}-docker.pkg.dev/$PROJECT_ID/${_ARTIFACT_REPO}/${_DOCKER_IMAGE}']
# Deploy the Docker image to Cloud Run. Note that it uses the same parameters that we were using to deploy.
# With Cloud Build we have managed to have all the parameters in one place and reduce the margin of error.
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args: [
'run', 'deploy', '${_RUN_SERVICE}',
'--image', '${_ARTIFACT_REGION}-docker.pkg.dev/$PROJECT_ID/${_ARTIFACT_REPO}/${_DOCKER_IMAGE}',
'--region', '${_RUN_REGION}',
'--platform', 'managed',
'--port=8000']
# List the Docker images used. If a specified image is not produced during the build, the build will fail.
images:
- ${_ARTIFACT_REGION}-docker.pkg.dev/$PROJECT_ID/${_ARTIFACT_REPO}/${_DOCKER_IMAGE}
# List of substitutions. When Cloud Build encounters a ${_} variable, it takes its default value from here.
# I have abstracted the values I had and merged them all below. This has the advantage of being quite quick in changing any settings.
# As per the $PROJECT_ID, gcloud will take the one passed to it or the one marked as default.
substitutions:
_ARTIFACT_REGION: europe-west2
_RUN_REGION: europe-west2
_RUN_SERVICE: fastapi
_ARTIFACT_REPO: fastapi-repository
_DOCKER_IMAGE: fastapi