Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menjelaskan cara meneruskan data di antara langkah-langkah build. Jika Anda baru menggunakan Cloud Build, baca Panduan memulai build dan Ringkasan konfigurasi build terlebih dahulu.
Cloud Build menjalankan tugas Anda sebagai serangkaian langkah build, yang dijalankan
di lingkungan yang terisolasi dan di-container. Setelah setiap langkah, penampung akan dihapus.
Hal ini memungkinkan Anda memiliki alat dan lingkungan yang sama sekali berbeda untuk setiap langkah,
dan secara default, data apa pun yang dibuat dalam satu langkah tidak dapat mencemari langkah berikutnya. Namun,
terkadang Anda mungkin perlu mempertahankan status dari satu langkah build untuk digunakan di langkah
berikutnya.
Untuk kasus tersebut, Cloud Build menyediakan volume, yang merupakan jalur file baca-tulis
yang dapat Anda lampirkan ke langkah build apa pun. Volume mempertahankan kontennya
selama durasi build. Anda dapat menentukan volume sendiri atau menggunakan
/workspace, yang merupakan volume default yang disediakan Cloud Build
untuk Anda. Sebelum menjalankan build, Cloud Build mengekstrak kode sumber ke /workspace. Apa pun yang ditulis ke volume dan /workspace yang ditentukan pengguna oleh langkah apa pun akan tersedia untuk langkah berikutnya.
Meneruskan data menggunakan ruang kerja
Untuk meneruskan data antar-langkah build, simpan aset yang dihasilkan oleh langkah build di
/workspace dan aset ini akan tersedia untuk langkah build berikutnya.
Dalam contoh file konfigurasi build berikut, langkah build pertama menyimpan
string "First Value" di /workspace/first.txt dan nilai 2 di
/workspace/second.txt. Langkah build kedua membaca dan mencetak nilai di
/workspace/first.txt dan /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)
Meneruskan data menggunakan volume yang ditentukan pengguna
Daripada menggunakan volume /workspace default yang disediakan oleh Cloud Build,
Anda dapat menentukan volume Anda sendiri untuk mempertahankan data di antara langkah-langkah build.
Untuk menentukan dan menggunakan volume Anda sendiri:
Pada langkah build tempat Anda ingin menyimpan data:
Tambahkan blok volumes dan tetapkan kolom berikut:
name: Tetapkan nilai kolom ini ke nama volume yang diinginkan.
path: Tetapkan nilai kolom ini ke jalur file untuk menyimpan data Anda.
Simpan data di jalur file yang ditentukan di path.
Pada langkah build tempat Anda ingin menggunakan data:
Tambahkan blok volumes dengan nilai untuk name dan path.
Gunakan data dari jalur file yang ditentukan di path.
Dalam contoh file konfigurasi build berikut, langkah build pertama menentukan volume
bernama myvolume dan menyimpan data di /persistent_volume/file. Langkah build
kedua mencetak nilai yang disimpan di /persistent_volume/file.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-01 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)."]]