How to pass data between Cloud Build steps

Dave Stanke
Google Cloud - Community
2 min readJul 25, 2019

TL;DR Save in-memory data to the /workspace volume mount, and it will be available to all subsequent build steps.

Visualization by Aliya Baptista • www.aliyabaptista.com

Google Cloud Build runs your build as a series of steps which execute in isolated containerized environments. After each step, the container is discarded. Poof! This lets you have totally different tools and environments for each step, and any cruft created in one step can’t contaminate the next step.

But sometimes that cruft is actually important data… you may need to persist state from one step of a build to use in subsequent steps. A common pattern would be to stash it in an environment variable, but that’s not going to work here, because each step runs in a separate environment (and therefore has separate environment vars).

This won’t work. Don’t do it.

(See “Mastering Cloud Build Syntax” for details about the “breakout syntax” used here.)

Do this instead!

While everything inside each step’s container is discarded, there is something that persists: volume mounts. These read-write paths can be attached to any build step, and they will retain their contents throughout the duration of the build. You can define your own volumes, but for this purpose, it’s easiest to use the one that Cloud Build automatically provides for you: /workspace. Anything written to that path by any step will be available to the steps that come after.

TIP: You can try these snippets without any other source; save the contents to a file, then run gcloud builds submit --config=<filename> --no-source

But what if I have A LOT of values to pass? That could get awful tedious.

Yes it sure could. Here’s a way to simplify: name any variables you want to persist with a common prefix. Then you can write all of them in one line of script, and subsequently read all of them in one line:

In other situations, you might find it more efficient to use a structured data format: suppose you’re reading from an API that returns JSON. Write that output as a file to /workspace, then in subsequent steps you can read items from that file. (jq is likely to come in handy here!)

Got other cool tips for getting the most out of Cloud Build? Share them in the comments!

h/t to Edrienne and Guillermina for inspiring this technique

--

--

Dave Stanke
Google Cloud - Community

DevOps Advocate at Google. My home is in Jersey City. My office is in New York. My opinions are all over the map, and may not reflect those of my employer.