Running One-Off/Admin Jobs for Cloud Run Services
Disclaimer: This is not officially supported by Google Cloud.
Cloud Run hosts services that handle HTTP requests but sometimes you need a way to run one-off / admin jobs with the same settings and container image. At this time the managed Cloud Run service doesn’t provide a way to run those kinds of processes and running them through HTTP handlers can result in short timeouts (5 minutes by default, max 60 minutes), the possibility to accidentally run them more than once, and potential security issues. Whether you need this for schema migrations, management tasks, or something else, I’ve made it easy for you. Click to launch a tool in Cloud Shell that will walk you through it:
Want to see more about how it works? Watch this short demo:
Under the covers this CLI just queries some information about a Cloud Run service and then starts a GCE (Google Compute Engine) instance running the same container as your Cloud Run service, passing it the service’s config including Cloud SQL connection info. There is a little bit of special sauce in setting up the Cloud SQL proxy and shutting down the compute instance once the container process has exited. Aside from that, this is just a helper tool for using the GCE create-with-container command.
On the security side of things, the GCE instance uses the same service account as the Cloud Run service so the permissions are the same.
Hopefully this helps you use Cloud Run more effectively! All of the source is on GitHub so feel free to check it out, file bugs, and make it better.