Dave Stanke
Jul 25 · 2 min read

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

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Dave Stanke

Written by

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.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade