A Python Script to Deploy to Cloud Run

A Python Script to Deploy to Cloud Run

Here are the steps to programatically deploy an application packaged in a local 'sources' folder to Google Cloud Run using Python. (see full code)

  1. Validate project: Set the GCP project to deploy to (the script doesn't create one, it could)
  2. Ensure Storage bucket: We need to upload source code in a Google Cloud Storage bucket, so we first need to check if a bucket exists, creating it if necessary.
  3. Zip and upload source code: Zips a local folder and uploads the resulting zip file to the storage bucket.
  4. Ensure Artifact Registry: The source will need to be built into a container image, so we need to check if a Google Artifact Registry repository exists, creating it if necessary.
  5. Build container image: Trigger a Google Cloud Build job to transform the uploaded source zip file into a Docker container image and push it to the Artifact Registry.
  6. Deploy to Cloud Run: Finally, create, or update if it already exists, the newly built container image to a Google Cloud Run service.

The complete Python script is available on GitHub: steren/cloud-run-python-deploy.