Cloud Run Bootstrap Tool
APACHE-2.0 License
Note: This tool is not officially part of the Cloud Run product and provided as-is as a convenience utility for getting started. Google does not guarantee long-term maintenance for this project.
(c)loud (r)un (b)ootstrap (t)ool can be used to simplify initialization of Cloud Run applications along with supporting Google Cloud services. The core goal of crbt is to provide a super easy way to setup a Cloud Run project that has source code management and CI/CD so that you can just focus on your code (and will even give you some template code to get you started, if desired).
Another important aspect is that everything should be able to be set up interactively to make it really simple to use (no need to memorize flags or parameters), but also to provide non-interactive usage for advanced usage (like providing the ability to redeploy your services the exact same way to other GCP projects).
Everything crbt does can be performed manually without crbt, as the tool wraps around the Google Cloud SDK Command-line interface. In fact, executing crbt with --dryrun
will show you exactly what you'd need to execute yourself, if you prefer!
Disclaimer: This is not an officially supported Google project.
Beyond the Cloud Run service deployment itself, crbt can perform the following:
For the Cloud Run service deployment, crbt can perform the following:
https://service.example.com
instead of https://service-esjxjixyz-ue.a.run.app
).app.json
customization file, including environment variables, options, and more.It also provides functionality to:
app.json
file that can be used to non-interactive re-deploy the service..crbt
configuration file.crbt can be ran three separate ways:
--region us-east1
).app.json
: Anything that is prompted for interactively can be set through the app.json
customization JSON file.crbt can also deploy source code three separate ways:
[*]: The code should be a Cloud Run service respecting the official Code Requirements.
Sure!
Well, that seemed simple... what all did we do behind the scenes?
We also:
.crbt
crbt destroy
Cool, but what if I didn't want to do that interactively, but use those same parameters in the example? We could've ran: crbt init --template web-expressjs-helloworld --build commit --region us-east1 --map jan19-01.esquared.dev
See more examples in the Examples section below..
Binaries are available through Releases.
First you will need to install Node.js and npm. Installing Node.js should install npm as well.
Execute the following commands to setup for testing purposes (creates symlink):
npm install
npm link
The crbt
command should now be accessible.
First you will need to install Node.js and npm. Installing Node.js should install npm as well.
Execute the following command to build a standalone binary:
npm run package
A crbt
binary will be placed inside the bin/
folder. Call it directly or place it with in your system path (e.g. cp bin/crbt /usr/local/bin/
).
The command crbt --help
lists the available commands and crbt <command> --help
shows more details for an individual command.
Below is a brief list of the available commands and their function:
Command | Description |
---|---|
init | Setup a new application in the current directory. This command will create a crbt.conf configuration file in your current directory. It will also look for an app.json for customization parameters. |
customize:domain | Add or remove custom domain mappings. |
customize:envvars | Re-parse the app.json file, and re-prompt for environment variables. |
deploy | Deploys the Cloud Run service within the current directory. Relies on a cloudbuild.yaml file and the local project directory. This is the method to deploy the service if automatic Cloud Build triggers were not selected during initialization. |
destroy [feature] | Destroys deployed services, but does not delete local code. [feature] can be all (Default) or cloudrun . |
list | List available built-in templates for Cloud Run services. |
status | Output current configuration status. |
trigger:create:scheduler | Create a new Cloud Scheduler trigger for the Cloud Run service. |
trigger:delete:scheduler | Delete a Cloud Scheduler trigger for the Cloud Run service. |
help | Display help information about the CLI or specific commands. |
Below is a brief list of each command's options:
Usage without all flags defined (either through command-line arguments or app.json
) will interactively prompt for unconfigured flags. The flags --local
, --sourcerepo
, and --template
define where the source code should come from, and these are mutually exclusive. Some options are specific to the platform location (e.g. managed
or gke
).
Option | Description |
---|---|
-n, --name [name] | Application name (default: current directory name) |
-p, --platform [name] | Deploy to Cloud Run (managed) or Cloud Run on Anthos on Google Cloud (gke) [managed, gke] |
-r, --region [region] | GCP Region (platform-specific: managed) [asia-northeast1, europe-west1, us-central1, us-east1] |
-c, --cluster [name] | GKE Cluster to use with Anthos (platform-specific: gke) |
-z, --clusterzone [zone] | GKE Cluster location to use with Anthos (platform-specific: gke) |
-t, --template [name] | Template for Cloud Run services. List of templates can be specified with crbt list . This is mutually exclusive with --sourcerepo . |
-s, --sourcerepo [repoUrl] | Git repository URL for project source to copy. Should be in format such as https://github.com/GoogleCloudPlatform/cloud-run-hello.git . This is mutually exclusive with --template . |
-l, --local | Use local source within current directory. |
-b, --build [trigger] | Use Cloud Build build trigger; [trigger] can be commit to build and deploy on commit or none to not automatically perform any Cloud Build activities. |
-m, --map [domain] | Create custom domain mapping for Cloud Run service. none to skip prompt. (platform-specific: managed) |
-e, --existing | Allow existing files to exist in the project directory (Typically, --template wants things to be empty.) |
-d, --dryrun | Only show commands and save configuration, but do not execute them in GCP. |
-v, --verbose | Verbose mode |
This command allows adding or removing custom domain mappings to the service.
Option | Description |
---|---|
-m, --map [domain] | Create custom domain mapping for Cloud Run service. none to remove all custom mappings. (platform-specific: managed) |
-v, --verbose | Verbose mode |
This command re-parse the app.json
file and re-prompts for environment variables, and then updates the environment variables.
Option | Description |
---|---|
-v, --verbose | Verbose mode |
This command deploys the Cloud Run service within the current directory by utilizing the cloudbuild.yaml
file created during initialization. This must be ran inside each Cloud Run service's folder to deploy each service. To destroy these services, crbt destroy
must be ran inside each folder, as well.
Option | Description |
---|---|
-v, --verbose | Verbose mode |
Destroys deployed services. [feature] can be:
all
: (Default) Deletes all services defined within the .crbt
file.cloudrun
: Deletes the Cloud Run services. The behavior of this command depends on which file is leveraged to delete the services. The command prefers to leverage the .crbt
file to delete all services, but if no .crbt
file then it checks for a cloudbuild.yaml
in the current directory and deletes that service (in the event of multiple services that need to be deleted, this command would need to be ran in each).Option | Description |
---|---|
-y, --yes | Confirm destroying entire project with no prompts. |
-p, --preserve | Do not delete .crbt as the project is destroyed. The .crbt file can be used to rebuild. |
-r, --rep | Override and specify Cloud Source Repositories repo to delete. |
-v, --verbose | Verbose mode |
This command does not have any options.
This command does not have any options.
Create a new Cloud Scheduler trigger for the Cloud Run service that calls the service based on cron format.
Option | Description |
---|---|
-s, --schedule | Schedule on which the job will be executed. (unix-cron format) |
-m, --method | HTTP method to use for the request [GET, PUT, POST] |
-b, --body | Data payload to be included as the body of the HTTP request. (only valid for PUT, POST methods) |
-a, --account | Service account used to authenticate to the Cloud Run service (name only, not email) |
-d, --dryrun | Only show commands, but do not execute them in GCP |
-v, --verbose | Verbose mode |
Additional Cloud Scheduler trigger documentation:
Remove a Cloud Scheduler trigger for the Cloud Run service.
Option | Description |
---|---|
-v, --verbose | Verbose mode |
If you include an app.json
at the root of your repository, it allows you customize the experience such as defining an alternative service name, or prompting for additional environment variables.
Including an app.json
file within the root of the project will provide customization parameters to the init
command. The app.json
command is meant to be backwards compatible with the Cloud Run Button's app.json
format, while also adding additional features (Note: Currently hooks
are not supported.).
A .crbt
file generated from a deployment can be copied into an app.json
to rebuild the service based on how it was previously configured.
For example:
{
"name": "foo-app",
"env": {
"BACKGROUND_COLOR": {
"description": "specify a css color",
"value": "#fefefe",
"required": false
},
"TITLE": {
"description": "title for your site"
},
"APP_SECRET": {
"description": "secret"
},
"VERSION": "1.0"
},
"options": {
"allow-unauthenticated": false
}
}
Reference:
name
: (optional, default: current directory name)--name
.region
: (optional)--region
.cloudbuild
: (optional)commit
to build and deploy on commit or none
to not automatically perform any Cloud Build activities. Equivalent to passing --build
.mapping
: (optional)test.example.com
or none
to skip.env
: (optional)description
: (optional)value
: (optional)required
, (optional, default: true
)generator
, (optional)secret
options
: (optional)allow-unauthenticated
: (optional, default: true
)hooks
: (optional)GOOGLE_CLOUD_PROJECT
, GOOGLE_CLOUD_REGION
, and K_SERVICE
. Command outputs are shown as they are executed.precreate
: (optional)commands
: (array of strings)postcreate
: (optional)commands
: (array of strings)Additional values can be added to the app.json
, or may be added in the example of using a .crbt
file as an app.json
; however, they are ignored and not utilized.
Template | Description |
---|---|
web-expressjs-helloworld | Simple Hello World using NodeJS and Express.js for a frontend. |
web-expressjs-fancystore | Fancy Store site using NodeJS and Express.js for both frontend and API. |
backend-gcloud-dataflow | Show GCP Dataflow jobs that exceed a maximum duration. |
api-expressjs-datafiles | API using Express.js to serve data files. |
The following examples are non-interactive commands that will create different permutations of deployments. All of the below could also be done with just running crbt init
and answering prompts, as well.
Deploy a service based on the publicly available example Cloud Run Hello World GitHub repository, named hellorun, with Cloud Build CI/CD in US-East1 region, and map the domain test.example.com
to the service:
crbt init -n hellorun -b commit -p managed -r us-east1 --sourcerepo https://github.com/GoogleCloudPlatform/cloud-run-hello.git -m test.example.com
Create service, named after the local directory, with Cloud Build CI/CD in US-East1 region by leveraging the api-expressjs-datafiles
template, and map the domain test.example.com
to the service:
crbt init --template api-expressjs-datafiles --build commit --platform managed --region us-east1 --map test.example.com
Create a service, named testapp, without Cloud Build CI/CD in US-Central1 region by leveraging the web-expressjs-helloworld
template:
crbt init -n testapp -t web-expressjs-helloworld -b none -p managed -r us-central1 -m none
crbt deploy
(Needed due to no automatic build/deploy)
Deploy a service using the source within the current directory, with Cloud Build CI/CD in US-Central1 region, inheritting the name from the local directory:
crbt init --local -b commit -p managed -r us-east1 -m none
Create service, named after the local directory, with Cloud Build CI/CD onto the GKE Cluster central
within the us-central1-b
zone by leveraging the web-expressjs-helloworld
template:
crbt init --platform gke --cluster central --clusterzone us-central1-b --template web-expressjs-helloworld --build commit
Create service, named exampleapp, without Cloud Build CI/CD onto the GKE Cluster central
within the us-central1-b
zone by leveraging the web-expressjs-fancystore
template:
crbt init --name exampleapp --platform gke --cluster central --clusterzone us-central1-b --template web-expressjs-fancystore --build none
crbt deploy
(needed due to no automatic build/deploy)
crbt destroy --yes
.crbt
): crbt destroy --preserve
crbt destroy cloudrun
crbt status
Output:
name: newapplication
platform: managed
region: us-east1
project:
name: es-crtest8
id: 594320120000
repo:
name: newapplication
cloudrun:
newapplication:
url: https://newapplication-esjxjixyzy-ue.a.run.app
options:
allow-unauthenticated: true
cloudbuild:
newapplication-trigger: a389dcd2-a700-xxxx-99d2-20dce913f241
mapping: jan19-01.esquared.dev
crbt trigger:create:scheduler --schedule "0 * * * *" --method GET
Output Example:
=== Cloud Scheduler Trigger Creation
[ > ] Services API enabled: cloudscheduler
[ > ] Services API enabled: appengine
[ ! ] App Engine app not found. Attempting to create...
[ ! ] App Engine app created in: us-east1
[ ? ] What service account would you like to use (name only, not email)? feb17-01-invoker
[ ! ] Service account not found. Attempting to create...
[ > ] Service Account created: [email protected]
[ > ] Cloud Run IAM Policy Binding added ([email protected]): roles/run.invoker
[ > ] Using schedule: 0 * * * *
[ > ] Using method: GET
[ > ] Cloud Scheduler trigger created.
[ > ] Configuration saved to file (.crbt): trigger -> serviceAccount -> [email protected]
[ > ] Configuration saved to file (.crbt): trigger -> name -> scheduler-feb17-01
[ > ] Configuration saved to file (.crbt): trigger -> type -> scheduler
Yes, run crbt init
with the --dryrun
flag -- this will generate a .crbt
configuration file based on your parameters. You can then rename the .crbt
file to app.json
and run crbt init --local
(which will automatically pull answers from app.json
) to run the same commands that were shown during the --dryrun
. You can still override anything in app.json
by passing an init flag such as --region us-central
. The crbt init --dryrun
will work both interactive and non-interactive modes.
No, nothing created depends on crbt long-term. You can delete the configuration file, but it means crbt destroy
will no longer work if you want to use crbt to clean everything up later.
The .crbt
file generated from deployment can be renamed to the app.json
file and crbt init --local
will pull all of the settings chosen previously. If you changed projects within your gcloud
settings, this will all be re-provisioned in the new project.
git remote -v
Expected output:
origin https://source.developers.google.com/p/es-crtest1/r/test6 (fetch)
origin https://source.developers.google.com/p/es-crtest1/r/test6 (push)
Add the GitHub repo as a push location:
git remote set-url --add --push origin https://github.com/earlgay/testRepo.git
Add the Cloud Source Repositories repo back as a push location (using path determined earlier):
git remote set-url --add --push origin https://source.developers.google.com/p/es-crtest1/r/test6
Check that both locations are now shown as push locations:
git remote -v
Expected output:
origin https://source.developers.google.com/p/es-crtest1/r/test6 (fetch)
origin https://github.com/earlgay/testRepo.git (push)
origin https://source.developers.google.com/p/es-crtest1/r/test6 (push)
Now, when doing git push origin master
it will push to both repositories. Please note that crbt destroy
does not include cleaning up any 3rd party repositories that may be added in this manner.
crbt destroy
and in some cases manual cleanup of deployed services.Project is inspired from the following other Google projects:
Sample templates based on:
Template | Reference |
---|---|
web-expressjs-fancystore | microservices-demo |
web-expressjs-helloworld | cloud-run-hello |
api-expressjs-datafiles | monolith-to-microservices |
backend-gcloud-dataflow | dataflow-time-limit |