Cloud Build führt Ihre Aufgaben als eine Reihe von Build-Schritten aus, die in isolierten und containerisierten Umgebungen ausgeführt werden. Nach jedem Schritt wird der Container verworfen.
So können Sie für jeden Schritt völlig unterschiedliche Tools und Umgebungen verwenden, und standardmäßig können die in einem Schritt erstellten Daten den nächsten Schritt nicht beeinträchtigen. Manchmal müssen Sie jedoch den Status aus einem Build-Schritt beibehalten, um ihn in den nachfolgenden Schritten zu verwenden.
In solchen Fällen bietet Cloud Build Volumes, d. h. schreibgeschützte Dateipfade, die Sie an jeden Build-Schritt anhängen können. Volumes behalten ihren Inhalt während der gesamten Dauer des Builds bei. Sie können Ihr eigenes Volume definieren oder /workspace verwenden. Dies ist das Standard-Volume, das Cloud Build für Sie bereitstellt. Vor der Ausführung eines Builds extrahiert Cloud Build den Quellcode in /workspace. Alles, was in einem beliebigen Schritt in benutzerdefinierte Volumes und /workspace geschrieben wird, steht für nachfolgende Schritte zur Verfügung.
Daten mit Arbeitsbereichen übergeben
Speichern Sie die im Build-Schritt erzeugten Assets in /workspace, um Daten zwischen Build-Schritten zu übergeben. Diese Assets sind für alle nachfolgenden Build-Schritte verfügbar.
In der folgenden Build-Konfigurationsdatei wird im ersten Build-Schritt der String "First Value" in /workspace/first.txt und der Wert 2 in /workspace/second.txt gespeichert. Der zweite Build-Schritt liest und gibt die Werte in /workspace/first.txt und /workspace/second.txt aus.
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)
Anstatt das von Cloud Build bereitgestellte Standard-Volume /workspace zu verwenden, können Sie ein eigenes Volume definieren, um Daten zwischen Build-Schritten beizubehalten.
So definieren und verwenden Sie Ihr eigenes Volume:
Im Build-Schritt, in dem Sie die Daten speichern möchten:
Fügen Sie den Block volumes hinzu und legen Sie die folgenden Felder fest:
name: Legen Sie den Wert dieses Felds auf den gewünschten Volume-Namen fest.
path: Legen Sie den Wert dieses Felds auf den Dateipfad zum Speichern Ihrer Daten fest.
Speichern Sie die Daten im Dateipfad, der in path angegeben ist.
Im Build-Schritt, in dem Sie die Daten nutzen möchten:
Fügen Sie den Block volumes mit den Werten für name und path hinzu.
Verarbeiten Sie die Daten aus dem in path angegebenen Dateipfad.
In der folgenden Build-Konfigurationsdatei wird mit dem ersten Build-Schritt ein Volume mit dem Namen myvolume definiert und Daten werden in /persistent_volume/file gespeichert. Der zweite Build-Schritt gibt den in /persistent_volume/file gespeicherten Wert aus.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-18 (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)."]]