Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1
Add the orbs
stanza below your version, invoking the orb:
1
2
orbs:
gcp-cloud-run: circleci/gcp-cloud-run@1.0.2
Use gcp-cloud-run
elements in your existing workflows and jobs.
Automatically build and deploy a docker container using the Cloud Run managed platform.
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
jobs:
build_and_deploy_gke:
docker:
- image: cimg/base:stable
steps:
- checkout
- cloudrun/init
- cloudrun/create_gke_cluster:
cluster-name: example-cluster-${CIRCLE_BUILD_NUM}
enable-stackdriver-kubernetes: true
machine-type: n1-standard-4
scopes: cloud-platform
zone: us-east1
- cloudrun/build:
tag: gcr.io/${GOOGLE_PROJECT_ID}/test-${CIRCLE_SHA1}
- cloudrun/deploy:
cluster: example-cluster-${CIRCLE_BUILD_NUM}
cluster-location: us-east1
image: gcr.io/${GOOGLE_PROJECT_ID}/test-${CIRCLE_SHA1}
platform: gke
service-name: example-service-${CIRCLE_BUILD_NUM}
orbs:
cloudrun: circleci/gcp-cloud-run@x.y
version: 2.1
workflows:
build_and_deploy_to_gke_workflow:
jobs:
- build_and_deploy_gke
Automatically build and deploy a docker container using the Cloud Run managed platform.
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
jobs:
build_and_deploy:
docker:
- image: cimg/base:stable
steps:
- checkout
- cloudrun/init
- cloudrun/build:
tag: gcr.io/${GOOGLE_PROJECT_ID}/test-${CIRCLE_SHA1}
- cloudrun/deploy:
image: gcr.io/${GOOGLE_PROJECT_ID}/test-${CIRCLE_SHA1}
platform: managed
region: us-east1
service-name: example-service
unauthenticated: true
- run:
command: >
# A simple example of how a deployed managed service could be
verified or further tested.
# This step will send request our "API" and fail if there is
unexpected output.
GCP_API_RESULTS=$(curl -s "$GCP_DEPLOY_ENDPOINT")
if [ "$GCP_API_RESULTS" != "Hello World!" ]; then
echo "Result is unexpected"
echo 'Result: '
curl -s "$GCP_DEPLOY_ENDPOINT"
exit 1;
fi
name: Test managed deployed service.
orbs:
cloudrun: circleci/gcp-cloud-run@x.y
version: 2.1
workflows:
build_and_deploy_to_managed_workflow:
jobs:
- build_and_deploy
'Build your container image using Cloud Run Build. Learn more: https://cloud.google.com/run/docs/quickstarts/build-and-deploy'
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
args | Add any additional arguments not explicitly defined as a parameter. Find additional arguments here: https://cloud.google.com/sdk/gcloud/reference/beta/builds/submit | No | '' | string |
config | The YAML or JSON file to use as the build configuration file. | No | '' | string |
source | The location of the source to build. The location can be a directory on a local disk or a gzipped archive file (.tar.gz) in Google Cloud Storage. If a dockerfile is present in the working directory, this parameter may be left blank. | No | '' | string |
tag | The tag to use with a "docker build" image creation. Cloud Build will run a remote "docker build -t $TAG .", where $TAG is the tag provided by this flag. The tag must be in the gcr.io/* or *.gcr.io/* namespaces. Specify a tag if you want Cloud Build to build using a Dockerfile instead of a build config file. If you specify a tag in this command, your source must include a Dockerfile. For instructions on building using a Dockerfile see https://cloud.google.com/cloud-build/docs/quickstart-docker. | No | '' | string |
'Create a cluster and enable it for Cloud Run for Anthos on Google Cloud Learn more: https://cloud.google.com/run/docs/quickstarts/prebuilt-deploy-gke'
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
addons | Addons (https://cloud.google.com/kubernetes-engine/reference/rest/v1/projects.zones.clusters#AddonsConfig) are additional Kubernetes cluster components. Addons specified by this flag will be enabled. The others will be disabled. | No | HorizontalPodAutoscaling,HttpLoadBalancing,Istio,CloudRun | string |
args | Add any additional arguments not explicitly defined as a parameter. Find additional arguments here: https://cloud.google.com/sdk/gcloud/reference/beta/container/clusters/create#--enable-stackdriver-kubernetes | No | '' | string |
cluster-name | The name of the cluster | Yes | - | string |
enable-stackdriver-kubernetes | Enable Stackdrive logging for Kubernetes (https://cloud.google.com/monitoring/kubernetes-engine/legacy-stackdriver/logging) | No | false | boolean |
machine-type | The type of machine to use for nodes. The list of predefined machine types is available using the following command. | No | n1-standard-1 | string |
scopes | Specifies scopes for the node instances. | No | gke-default | string |
zone | Compute zone (e.g. us-central1-a) for the cluster. | No | '' | string |
Delete an existing GCloud cluster.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
args | Add any additional arguments not explicitly defined as a parameter. Find additional arguments here: https://cloud.google.com/sdk/gcloud/reference/beta/container/clusters/create#--enable-stackdriver-kubernetes | No | '' | string |
cluster-name | The name of the cluster | Yes | - | string |
zone | Compute zone (e.g. us-central1-a) for the cluster. | No | '' | string |
'Deploy your containerized application to Google Cloud Run. Learn more: https://cloud.google.com/run/docs/deploying For the managed service: After deployment, the service URL will be accessible via the $GCP_DEPLOY_ENDPOINT environment variable.'
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
args | Add any additional arguments not explicitly defined as a parameter. Find additional arguments here: https://cloud.google.com/sdk/gcloud/reference/beta/run/deploy | No | '' | string |
cluster | ID of the cluster or fully qualified identifier for the cluster. Required on "gke" and "kubernetes" platforms. | No | '' | string |
cluster-location | Zone in which the cluster is located. Required on "gke" and "kubernetes" platforms. | No | '' | string |
image | Name of the container image to deploy (e.g. gcr.io/cloudrun/hello:latest). | No | '' | string |
platform | Target platform | No | managed | enum |
region | Required on "managed" platform. | No | '' | string |
service-name | What is the name of the service being deployed? | Yes | - | string |
unauthenticated | Whether to enable allowing unauthenticated access to the service. | No | false | boolean |
Install and initialize the GCP CLI for Google Cloud Run. Ensure to set the required environment variables.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
gcloud-service-key | Required environment variable. Name of environment variable storing the full service key JSON file for the Google project. | No | GCLOUD_SERVICE_KEY | env_var_name |
google-compute-zone | Required environment variable. The Google compute zone to connect with via the gcloud CLI. | No | GOOGLE_COMPUTE_ZONE | env_var_name |
google-project-id | Required environment variable. The Google project ID to connect with via the gcloud CLI. | No | GOOGLE_PROJECT_ID | env_var_name |
Base CircleCI docker image.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
tag | Pick a specific cimg/base image tag: https://hub.docker.com/r/cimg/base/tags
| No | stable | string |
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
commands:
build:
description: |
'Build your container image using Cloud Run Build. Learn more: https://cloud.google.com/run/docs/quickstarts/build-and-deploy'
parameters:
args:
default: ""
description: 'Add any additional arguments not explicitly defined as a parameter.
Find additional arguments here: https://cloud.google.com/sdk/gcloud/reference/beta/builds/submit'
type: string
config:
default: ""
description: The YAML or JSON file to use as the build configuration file.
type: string
source:
default: ""
description: The location of the source to build. The location can be a directory
on a local disk or a gzipped archive file (.tar.gz) in Google Cloud Storage.
If a dockerfile is present in the working directory, this parameter may
be left blank.
type: string
tag:
default: ""
description: The tag to use with a "docker build" image creation. Cloud Build
will run a remote "docker build -t $TAG .", where $TAG is the tag provided
by this flag. The tag must be in the gcr.io/* or *.gcr.io/* namespaces.
Specify a tag if you want Cloud Build to build using a Dockerfile instead
of a build config file. If you specify a tag in this command, your source
must include a Dockerfile. For instructions on building using a Dockerfile
see https://cloud.google.com/cloud-build/docs/quickstart-docker.
type: string
steps:
- run:
command: |
# if no source parameter is set, check for a docker file and fail if none is found.
<<^ parameters.source >>
if [ -f dockerfile ]; then
echo "No docker image in $(pwd). You may need to specify a source location."
exit 1
fi
<</ parameters.source >>
gcloud builds submit \
<<# parameters.source>><< parameters.source >><</ parameters.source>> \
<<# parameters.tag>>--tag << parameters.tag >><</ parameters.tag>> \
<<# parameters.config>>--config << parameters.config >><</ parameters.config>> \
<< parameters.args >>
name: Cloud Run Build
create_gke_cluster:
description: |
'Create a cluster and enable it for Cloud Run for Anthos on Google Cloud Learn more: https://cloud.google.com/run/docs/quickstarts/prebuilt-deploy-gke'
parameters:
addons:
default: HorizontalPodAutoscaling,HttpLoadBalancing,Istio,CloudRun
description: 'Addons (https://cloud.google.com/kubernetes-engine/reference/rest/v1/projects.zones.clusters#AddonsConfig)
are additional Kubernetes cluster components. Addons specified by this flag
will be enabled. The others will be disabled. '
type: string
args:
default: ""
description: 'Add any additional arguments not explicitly defined as a parameter.
Find additional arguments here: https://cloud.google.com/sdk/gcloud/reference/beta/container/clusters/create#--enable-stackdriver-kubernetes'
type: string
cluster-name:
description: The name of the cluster
type: string
enable-stackdriver-kubernetes:
default: false
description: Enable Stackdrive logging for Kubernetes (https://cloud.google.com/monitoring/kubernetes-engine/legacy-stackdriver/logging)
type: boolean
machine-type:
default: n1-standard-1
description: The type of machine to use for nodes. The list of predefined
machine types is available using the following command.
type: string
scopes:
default: gke-default
description: Specifies scopes for the node instances.
type: string
zone:
default: ""
description: Compute zone (e.g. us-central1-a) for the cluster.
type: string
steps:
- run:
command: |
gcloud config set run/platform gke
gcloud config set project $GOOGLE_PROJECT_ID
gcloud beta container clusters create <<parameters.cluster-name>> \
--addons=<<parameters.addons>> \
--machine-type=<<parameters.machine-type>> \
--zone=<<parameters.zone>> \
--scopes <<parameters.scopes>> \
<<# parameters.enable-stackdriver-kubernetes >>--enable-stackdriver-kubernetes<</ parameters.enable-stackdriver-kubernetes >> << parameters.args >>
name: Create GKE Cluster
delete_gke_cluster:
description: |
Delete an existing GCloud cluster.
parameters:
args:
default: ""
description: 'Add any additional arguments not explicitly defined as a parameter.
Find additional arguments here: https://cloud.google.com/sdk/gcloud/reference/beta/container/clusters/create#--enable-stackdriver-kubernetes'
type: string
cluster-name:
description: The name of the cluster
type: string
zone:
default: ""
description: Compute zone (e.g. us-central1-a) for the cluster.
type: string
steps:
- run:
command: |
CLOUDSDK_CORE_DISABLE_PROMPTS=1
gcloud config set run/platform gke
gcloud config set project $GOOGLE_PROJECT_ID
gcloud container clusters delete <<parameters.cluster-name>> --zone=<<parameters.zone>> --no-user-output-enabled << parameters.args >>
echo "Clusters deleted."
name: Delete GKE Cluster
no_output_timeout: 20m
deploy:
description: |
'Deploy your containerized application to Google Cloud Run. Learn more: https://cloud.google.com/run/docs/deploying For the managed service: After deployment, the service URL will be accessible via the $GCP_DEPLOY_ENDPOINT environment variable.'
parameters:
args:
default: ""
description: 'Add any additional arguments not explicitly defined as a parameter.
Find additional arguments here: https://cloud.google.com/sdk/gcloud/reference/beta/run/deploy'
type: string
cluster:
default: ""
description: ID of the cluster or fully qualified identifier for the cluster.
Required on "gke" and "kubernetes" platforms.
type: string
cluster-location:
default: ""
description: Zone in which the cluster is located. Required on "gke" and "kubernetes"
platforms.
type: string
image:
default: ""
description: Name of the container image to deploy (e.g. gcr.io/cloudrun/hello:latest).
type: string
platform:
default: managed
description: Target platform
enum:
- managed
- gke
type: enum
region:
default: ""
description: Required on "managed" platform.
type: string
service-name:
description: What is the name of the service being deployed?
type: string
unauthenticated:
default: false
description: Whether to enable allowing unauthenticated access to the service.
type: boolean
steps:
- run:
command: |
PLATFORM=<<parameters.platform>>
case $PLATFORM in
managed)
echo 'Platform: Managed'
# Check for required parameters
GCP_REGION=<<parameters.region>>
GCP_SERVICE_NAME=<<parameters.service-name>>
if [ -z $GCP_REGION ]; then
echo 'The region parameter is required for the "managed" platform.'
exit 1
fi
if [ -z $GCP_SERVICE_NAME ]; then
echo 'The service-name parameter is required for the "managed" platform.'
exit 1
fi
# End of parameter check
# Deployment command
gcloud beta run deploy <<parameters.service-name>> \
--image <<parameters.image>> \
--region <<parameters.region>> \
<<# parameters.unauthenticated>>--allow-unauthenticated<</parameters.unauthenticated>> \
<<^ parameters.unauthenticated>>--no-allow-unauthenticated<</parameters.unauthenticated>> \
--platform managed
echo
echo "Service deployed"
echo
GET_GCP_DEPLOY_ENDPOINT=$(gcloud beta run services describe <<parameters.service-name>> --platform <<parameters.platform>><<# parameters.region>> --region <<parameters.region>><</ parameters.region>> --format="value(status.address.url)")
echo "export GCP_DEPLOY_ENDPOINT=$GET_GCP_DEPLOY_ENDPOINT" >> $BASH_ENV
source $BASH_ENV
echo $GCP_DEPLOY_ENDPOINT
;;
gke)
echo 'Platform: GKE'
# Check for required parameters
GCP_CLUSTER=<<parameters.cluster>>
GCP_CLUSTER_LOCATION=<<parameters.cluster-location>>
if [ -z $GCP_CLUSTER ]; then
echo 'The cluster parameter is required for the "gke" platform.'
exit 1
fi
if [ -z $GCP_CLUSTER_LOCATION ]; then
echo 'The cluster-location parameter is required for the "gke" platform.'
exit 1
fi
# End of parameter check
# Deployment command
echo "Ensure all required APIs are enabled"
echo
gcloud services enable container.googleapis.com containerregistry.googleapis.com cloudbuild.googleapis.com
echo
gcloud beta run deploy <<parameters.service-name>>\
--cluster <<parameters.cluster>> \
--cluster-location <<parameters.cluster-location>> \
--image <<parameters.image>> \
--platform gke
echo
echo "Service deployed"
echo
;;
esac
name: Cloud Run deploy
init:
description: |
Install and initialize the GCP CLI for Google Cloud Run. Ensure to set the required environment variables.
parameters:
gcloud-service-key:
default: GCLOUD_SERVICE_KEY
description: Required environment variable. Name of environment variable storing
the full service key JSON file for the Google project.
type: env_var_name
google-compute-zone:
default: GOOGLE_COMPUTE_ZONE
description: Required environment variable. The Google compute zone to connect
with via the gcloud CLI.
type: env_var_name
google-project-id:
default: GOOGLE_PROJECT_ID
description: Required environment variable. The Google project ID to connect
with via the gcloud CLI.
type: env_var_name
steps:
- gcp-cli/install
- gcp-cli/initialize:
gcloud-service-key: <<parameters.gcloud-service-key>>
google-compute-zone: <<parameters.google-compute-zone>>
google-project-id: <<parameters.google-project-id>>
- run:
command: gcloud components install beta && gcloud components update
name: Prep Cloud Run components
description: |
'Build and deploy stateless images to Google Cloud Run as serverless applications. Repo: https://github.com/CircleCI-Public/gcp-cloud-run-orb Home: https://cloud.google.com/run/'
examples:
build_and_deploy_on_gke:
description: |
Automatically build and deploy a docker container using the Cloud Run managed platform.
usage:
jobs:
build_and_deploy_gke:
docker:
- image: cimg/base:stable
steps:
- checkout
- cloudrun/init
- cloudrun/create_gke_cluster:
cluster-name: example-cluster-${CIRCLE_BUILD_NUM}
enable-stackdriver-kubernetes: true
machine-type: n1-standard-4
scopes: cloud-platform
zone: us-east1
- cloudrun/build:
tag: gcr.io/${GOOGLE_PROJECT_ID}/test-${CIRCLE_SHA1}
- cloudrun/deploy:
cluster: example-cluster-${CIRCLE_BUILD_NUM}
cluster-location: us-east1
image: gcr.io/${GOOGLE_PROJECT_ID}/test-${CIRCLE_SHA1}
platform: gke
service-name: example-service-${CIRCLE_BUILD_NUM}
orbs:
cloudrun: circleci/gcp-cloud-run@x.y
version: 2.1
workflows:
build_and_deploy_to_gke_workflow:
jobs:
- build_and_deploy_gke
build_and_deploy_on_managed:
description: |
Automatically build and deploy a docker container using the Cloud Run managed platform.
usage:
jobs:
build_and_deploy:
docker:
- image: cimg/base:stable
steps:
- checkout
- cloudrun/init
- cloudrun/build:
tag: gcr.io/${GOOGLE_PROJECT_ID}/test-${CIRCLE_SHA1}
- cloudrun/deploy:
image: gcr.io/${GOOGLE_PROJECT_ID}/test-${CIRCLE_SHA1}
platform: managed
region: us-east1
service-name: example-service
unauthenticated: true
- run:
command: |
# A simple example of how a deployed managed service could be verified or further tested.
# This step will send request our "API" and fail if there is unexpected output.
GCP_API_RESULTS=$(curl -s "$GCP_DEPLOY_ENDPOINT")
if [ "$GCP_API_RESULTS" != "Hello World!" ]; then
echo "Result is unexpected"
echo 'Result: '
curl -s "$GCP_DEPLOY_ENDPOINT"
exit 1;
fi
name: Test managed deployed service.
orbs:
cloudrun: circleci/gcp-cloud-run@x.y
version: 2.1
workflows:
build_and_deploy_to_managed_workflow:
jobs:
- build_and_deploy
executors:
default:
description: |
Base CircleCI docker image.
docker:
- image: cimg/base:<<parameters.tag>>
parameters:
tag:
default: stable
description: |
Pick a specific cimg/base image tag: https://hub.docker.com/r/cimg/base/tags
type: string
orbs:
gcp-cli: circleci/gcp-cli@1.8.3
kube: circleci/kubernetes@0.10.0
version: 2.1