Cloud Build runs your tasks as a series of build steps, which execute
in isolated and containerized environments. After each step, the container is discarded.
This allows you to have totally different tools and environments for each step,
and by default, any data created in one step can't contaminate the next step. But
sometimes you may need to persist state from one step of a build to use in subsequent
steps.
For such cases, Cloud Build provides volumes, which are read-write
file paths that you can attach to any build step. Volumes retain their contents
throughout the duration of the build. You can define your own volume or use
/workspace, which is the default volume that Cloud Build
provides for you. Before executing a build, Cloud Build extracts the
source code to /workspace. Anything written to user-defined
volumes and /workspace by any step will be available to subsequent steps.
Passing data using workspaces
To pass data between build steps, store the assets produced by the build step in
/workspace and these assets will be available to any subsequent build steps.
In the following example build config file, the first build step stores the
string "First Value" in /workspace/first.txt and the value 2 in
/workspace/second.txt. The second build step reads and prints the values in
/workspace/first.txt and /workspace/second.txt.
YAML
steps:-id:"StoreValues"name:ubuntuentrypoint:bashargs:--c-|# Save a value to persistent volume mount: "/workspace"echo "First Value" > /workspace/first.txt &&
# Save anotherexpr 1 + 1 > /workspace/second.txt# In the next step, everything in the environment is discarded# EXCEPT items in "/workspace"-id:"ReadValues"name:ubuntuentrypoint:bashargs:--c-|# Read from "/workspace"echo "First we saved " $(cat /workspace/first.txt) &&
echo "Then we saved " $(cat /workspace/second.txt)
Instead of using the default /workspace volume provided by Cloud Build,
you can define your own volume to persist data between build steps.
To define and use your own volume:
In the build step where you want to store the data:
Add a volumes block and set the following fields:
name: Set the value of this field to the desired volume name.
path: Set the value of this field to the file path to store your data.
Store the data in the file path specified in path.
In the build step where you want to consume the data:
Add a volumes block with the values for name and path.
Consume the data from the file path specified in path.
In following example build config file, the first build step defines a volume
named myvolume and stores data in /persistent_volume/file. The second build
step prints the value stored in /persistent_volume/file.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eCloud Build utilizes build steps in isolated containers, which are discarded after each step, meaning data doesn't carry over by default.\u003c/p\u003e\n"],["\u003cp\u003eVolumes, either the default \u003ccode\u003e/workspace\u003c/code\u003e or user-defined, enable the persistence of data between Cloud Build steps.\u003c/p\u003e\n"],["\u003cp\u003eStoring data within \u003ccode\u003e/workspace\u003c/code\u003e during a build step makes the assets accessible to subsequent steps.\u003c/p\u003e\n"],["\u003cp\u003eTo use custom volumes, define a \u003ccode\u003evolumes\u003c/code\u003e block with a \u003ccode\u003ename\u003c/code\u003e and \u003ccode\u003epath\u003c/code\u003e in both the storing and consuming build steps.\u003c/p\u003e\n"]]],[],null,["# Passing data between build steps\n\nThis page explains how to pass data between build steps. If you're new to\nCloud Build, read the [Build quickstart](/build/docs/build-push-docker-image) and\nthe [Build configuration overview](/build/docs/build-config) first.\n\nCloud Build runs your tasks as a series of **build steps**, which execute\nin isolated and containerized environments. After each step, the container is discarded.\nThis allows you to have totally different tools and environments for each step,\nand by default, any data created in one step can't contaminate the next step. But\nsometimes you may need to persist state from one step of a build to use in subsequent\nsteps.\n\nFor such cases, Cloud Build provides **volumes** , which are read-write\nfile paths that you can attach to any build step. Volumes retain their contents\nthroughout the duration of the build. You can define your own volume or use\n**/workspace**, which is the default volume that Cloud Build\nprovides for you. Before executing a build, Cloud Build extracts the\nsource code to /workspace. Anything written to user-defined\nvolumes and /workspace by any step will be available to subsequent steps.\n\nPassing data using workspaces\n-----------------------------\n\nTo pass data between build steps, store the assets produced by the build step in\n`/workspace` and these assets will be available to any subsequent build steps.\n\nIn the following example build config file, the first build step stores the\nstring \"First Value\" in `/workspace/first.txt` and the value `2` in\n`/workspace/second.txt`. The second build step reads and prints the values in\n`/workspace/first.txt` and `/workspace/second.txt`. \n\n### YAML\n\n steps:\n - id: \"Store Values\"\n name: ubuntu\n entrypoint: bash\n args:\n - -c\n - |\n # Save a value to persistent volume mount: \"/workspace\"\n echo \"First Value\" \u003e /workspace/first.txt &&\n # Save another\n expr 1 + 1 \u003e /workspace/second.txt\n # In the next step, everything in the environment is discarded\n # EXCEPT items in \"/workspace\"\n - id: \"Read Values\"\n name: ubuntu\n entrypoint: bash\n args:\n - -c\n - |\n # Read from \"/workspace\"\n echo \"First we saved \" $(cat /workspace/first.txt) &&\n echo \"Then we saved \" $(cat /workspace/second.txt)\n\n### JSON\n\n {\n \"steps\": [\n {\n \"id\": \"Store Values\",\n \"name\": \"ubuntu\",\n \"entrypoint\": \"bash\",\n \"args\": [\n \"-c\",\n \"echo \\\"First Value\\\" \u003e /workspace/first.txt &&\\nexpr 1 + 1 \u003e /workspace/second.txt\\n\"\n ]\n },\n {\n \"id\": \"Read Values\",\n \"name\": \"ubuntu\",\n \"entrypoint\": \"bash\",\n \"args\": [\n \"-c\",\n \"echo \\\"First we saved \\\" $(cat /workspace/first.txt) &&\\necho \\\"Then we saved \\\" $(cat /workspace/second.txt)\\n\"\n ]\n }\n ]\n }\n\n| **Note:** The `-c` flag in the code snippets on this page is used to execute multi-line commands. Any string you pass after `-c` is treated as a command. For more information on running bash commands with `-c`, see the [bash documentation](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Invoking-Bash).\n\nPassing data using user-specified volumes\n-----------------------------------------\n\nInstead of using the default `/workspace` volume provided by Cloud Build,\nyou can define your own volume to persist data between build steps.\n\nTo define and use your own volume:\n\n- In the build step where you want to store the data:\n - Add a `volumes` block and set the following fields:\n - `name`: Set the value of this field to the desired volume name.\n - `path`: Set the value of this field to the file path to store your data.\n - Store the data in the file path specified in `path`.\n- In the build step where you want to consume the data:\n - Add a `volumes` block with the values for `name` and `path`.\n - Consume the data from the file path specified in `path`.\n\nIn following example build config file, the first build step defines a volume\nnamed `myvolume` and stores data in `/persistent_volume/file`. The second build\nstep prints the value stored in `/persistent_volume/file`. \n\n### YAML\n\n steps:\n - name: 'ubuntu'\n entrypoint: 'bash'\n args:\n - '-c'\n - |\n echo \"Hello, world!\" \u003e /persistent_volume/file\n volumes:\n - name: 'myvolume'\n path: '/persistent_volume'\n - name: 'ubuntu'\n entrypoint: 'bash'\n args:\n - '-c'\n - |\n cat /persistent_volume/file\n volumes:\n - name: 'myvolume'\n path: '/persistent_volume'\n\n### JSON\n\n {\n \"steps\": [\n {\n \"name\": \"ubuntu\",\n \"entrypoint\": \"bash\",\n \"args\": [\n \"-c\",\n \"echo \\\"Hello, world!\\\" \u003e /persistent_volume/file\\n\"\n ],\n \"volumes\": [\n {\n \"name\": \"myvolume\",\n \"path\": \"/persistent_volume\"\n }\n ]\n },\n {\n \"name\": \"ubuntu\",\n \"entrypoint\": \"bash\",\n \"args\": [\n \"-c\",\n \"cat /persistent_volume/file\\n\"\n ],\n \"volumes\": [\n {\n \"name\": \"myvolume\",\n \"path\": \"/persistent_volume\"\n }\n ]\n }\n ]\n }\n\nWhat's next\n-----------\n\n- Learn how to [start a build manually](/build/docs/running-builds/start-build-manually).\n- Learn how to [automate builds using triggers](/build/docs/automating-builds/create-manage-triggers).\n- Learn how to [run bash scripts](/build/docs/configuring-builds/run-bash-scripts) within build steps.\n- Learn how to [configure build step order](/build/docs/configuring-builds/configure-build-step-order)."]]