does anyone use cloud build to deploy cloud run? w...
# google-cloud
a
does anyone use cloud build to deploy cloud run? we add environment variables during build that aren’t knowable during pulumi up, e.g. the code version being deployed. this causes problems because when we run
pulumi up
, those environment variables are removed by pulumi because they’re not defined in our pulumi cloud run profile
g
So caveat that I haven't used Cloud Build with Pulumi just yet. However, are the variables that aren't knowable during pulumi up defined in code somewhere that you could reference the same code with your Pulumi program?
a
they’re things like the build tag, which aren’t knowable until the build is run, which is after code has been committed
g
ahh, got it. Looks like there's some default substitutions; they all should be available to your Pulumi program just as they would be for any of your code: https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values. They'd then get set with this key: https://www.pulumi.com/registry/packages/gcp/api-docs/cloudbuild/trigger/#substitutions_python (I've got it set to Python for me, but you can flip to another language with the language switcher at the top of the page)
And then I think I'd call them as outputs to get passed to Cloud Run. If you set the
dependsOn
flag for your Cloud Run call to make it depend on the Cloud Build output, then applied that output to your code, it should work. Something like this (note I haven't tested this yet):
Copy code
gcp.cloudbuild.Trigger(..., substitutions={"_VERSION": "blah"}, ...)
gcp.cloudrun.Service(..., template=gcp.cloudrun.ServiceTemplateArgs(spec=gcp.cloudrun.ServiceTemplateSpecArgs(containers=[gcp.cloudrun.ServiceTemplateSpecContainerArgs(image=<from above>, ...)], ...), ...), ...)
The image's args and environment variables are passed in via the values here: https://www.pulumi.com/registry/packages/gcp/api-docs/cloudrun/service/#servicetemplatespeccontainer
a
brilliant, this should work for our use case, thanks a ton @great-queen-39697!
hmm, another issue seems to be the Image attribute (cloudrun.ServiceTemplateSpecContainerArgs.Image) - cloud build sets it to a concrete image value (e.g. us.gcr.io/myproject/myservice:some-hash) which i dont think i can get prior to build
g
Ah, I'm pretty sure that's an output you get from running the cloudbuild step (meaning it's returned as a Pulumi output). Try appending something like this (this is Python), assuming you assign the cloudbuild.trigger call to a variable `image`:
pulumi.export('id', image.id)
and see if it dumps the correct string to your Pulumi terminal output. If it doesn't, I'll hunt down the correct one