Archiviazione degli artefatti della build in Cloud Storage
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina spiega come archiviare gli elementi delle build in Cloud Storage.
Ti consigliamo di utilizzare Artifact Registry per archiviare gli elementi di compilazione. Artifact Registry è un
Google Cloud prodotto che puoi integrare con Cloud Build
per archiviare e gestire in modo sicuro i tuoi artefatti in repository privati o pubblici.
La memorizzazione degli elementi in Artifact Registry ti consente di:
Gestisci i metadati dei container e cerca le vulnerabilità dei container con
Artifact Analysis.
Per archiviare gli artefatti non contenitore in Cloud Storage, aggiungi un campo artifacts
nel file di configurazione di compilazione con la posizione del bucket in cui archiviare
l'artefatto e il percorso di uno o più artefatti:
[STORAGE_LOCATION]: un bucket Cloud Storage o una cartella all'interno del
bucket in cui Cloud Build deve archiviare l'elemento, ad esempio
gs://mybucket o gs://mybucket/myproject/builds. Per trovare i nomi
di quelli esistenti, consulta elencare i bucket
o creare un nuovo bucket.
[ARTIFACT_PATH]: percorso di uno o più elementi. [ARTIFACT_PATH] è relativo alla tua directory di lavoro. Può essere /workspace,
che è la directory di lavoro predefinita di Cloud Build o la directory di lavoro impostata utilizzando il campo dir.
[STORAGE_LOCATION]: un bucket Cloud Storage o una cartella all'interno del
bucket in cui Cloud Build deve archiviare l'elemento, ad esempio
gs://mybucket o gs://mybucket/myproject/builds. Per trovare i nomi dei
bucket esistenti, consulta la sezione su come elencare i bucket o su come
creare un nuovo bucket.
[ARTIFACT_PATH]: percorso di uno o più elementi. [ARTIFACT_PATH] è relativo alla tua directory di lavoro. Può essere /workspace,
che è la directory di lavoro predefinita di Cloud Build o la directory di lavoro impostata utilizzando il campo dir.
Tieni presente i seguenti accorgimenti quando archivi gli elementi in Cloud Storage:
Puoi specificare un solo bucket per caricare gli elementi e devi essere il
proprietario del bucket. Puoi specificare un percorso di directory valido nel bucket.
Puoi caricare un numero qualsiasi di elementi, ma puoi specificare fino a cento percorsi degli elementi.
Se carichi un elemento in un bucket che contiene già un elemento con lo stesso
nome, il nuovo elemento sostituirà quello esistente. Puoi attivare il controllo delle versioni degli oggetti per il tuo bucket se non vuoi che l'elemento più recente sostituisca un elemento esistente con lo stesso nome.
Al termine della compilazione, puoi trovare i risultati del caricamento nel
file manifest JSON in [STORAGE_LOCATION]/artifacts-$BUILD_ID.json.
Il file manifest JSON contiene i seguenti campi:
location: specifica la posizione in Cloud Storage in cui è archiviato un elemento e ha il formatogs://[STORAGE_LOCATION]/[FILE_NAME]#[GENERATION_NUMBER]. Puoi utilizzare il
numero di generazione per identificare in modo univoco
una versione dei dati nel bucket Cloud Storage.
file_hash: specifica il tipo di hash e il valore. Il tipo di hash è sempre 2, il che specifica che è stato eseguito l'hash MD5.
Esempi di artefatti
Gli esempi riportati di seguito mostrano come utilizzare il campo Artifacts in un file di configurazione della build. In tutti questi esempi, sostituisci [VALUES_IN_BRACKETS] con i valori appropriati.
Caricamento di file e cartelle
Il file di configurazione di build riportato di seguito carica helloworld.class ings://[STORAGE_LOCATION]/:
Per caricare più di un elemento, specifica il percorso di ciascun elemento separato da una virgola. Il seguente esempio carica HelloWorld.java, HelloWorld.class e cloudbuild.yaml in gs://[STORAGE_LOCATION]/:
Puoi anche caricare gli elementi in un percorso di directory valido nel bucket. L'esempio seguente carica HelloWorld.java e HelloWorld.class in gs://[BUCKET_NAME]/[FOLDER_NAME]:
Utilizzo di caratteri jolly per caricare più di un elemento
Quando carichi più elementi, puoi utilizzare
caratteri jolly in paths per specificare più
file.
L'esempio seguente prende come argomento un file denominato classes, che contiene i nomi dei file .java da compilare. Carica quindi qualsiasi file .class
nel bucket Cloud Storage specificato:
Utilizzo di variabili di sostituzione nella posizione del bucket
Puoi utilizzare le variabili di sostituzione per specificare una cartella all'interno del bucket Cloud Storage. Se la cartella specificata non esiste, Cloud Build la creerà per te.
L'esempio seguente carica gli elementi in un percorso Cloud Storage che include il nome del Google Cloud progetto da cui è stata eseguita la compilazione (ad esempio gs://mybucket/myproject/):
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eThis document explains how to store non-container build artifacts in Cloud Storage by adding an \u003ccode\u003eartifacts\u003c/code\u003e field to your build config file, which includes the storage location and artifact paths.\u003c/p\u003e\n"],["\u003cp\u003eWhen storing artifacts in Cloud Storage, you can only specify one bucket, you must be the owner, you can upload up to one hundred artifact paths, and new artifacts with the same name will replace the old one unless object versioning is enabled.\u003c/p\u003e\n"],["\u003cp\u003eThe build's upload results are stored in a JSON manifest file with the \u003ccode\u003elocation\u003c/code\u003e of the artifact and its \u003ccode\u003efile_hash\u003c/code\u003e available once the build is successful.\u003c/p\u003e\n"],["\u003cp\u003eYou can upload single or multiple artifacts, including folders, using specified paths or wildcard characters in the build configuration.\u003c/p\u003e\n"],["\u003cp\u003eThe storage location in Cloud Storage can include substitution variables, allowing for dynamic path creation, like including the project ID in the folder name.\u003c/p\u003e\n"]]],[],null,["# Storing build artifacts in Cloud Storage\n\nThis page explains how you can store build artifacts in Cloud Storage.\n\nWe recommend using Artifact Registry for storing build artifacts. Artifact Registry is a\nGoogle Cloud product that you can integrate with Cloud Build\nto securely store and manage your artifacts in private or public repositories.\nStoring artifacts in Artifact Registry enables you to:\n\n- Manage container metadata and scan for container vulnerabilities with [Artifact Analysis](/artifact-analysis/docs).\n- Enforce deployment policies with [Binary Authorization](/binary-authorization/docs).\n- [Use image streaming in Google Kubernetes Engine](/kubernetes-engine/docs/how-to/image-streaming), which is particularly beneficial for large container images.\n\nFor instructions on configuring Cloud Build to store packages and images\nfrom your builds in Artifact Registry, see\n[Store artifacts in Artifact Registry](/build/docs/building/store-artifacts-in-artifact-registry).\n\nStoring artifacts in Cloud Storage\n----------------------------------\n\n**To store non-container artifacts in Cloud Storage** , add an `artifacts`\nfield in your build config file with the location of the bucket to store the\nartifact and the path to one or more artifacts: \n\n### YAML\n\n artifacts:\n objects:\n location: [STORAGE_LOCATION]\n paths: [[ARTIFACT_PATH],[ARTIFACT_PATH], ...]\n\nWhere,\n\n- `[STORAGE_LOCATION]`: A Cloud Storage bucket or a folder within the bucket where Cloud Build must store the artifact, such as `gs://mybucket` or `gs://mybucket/myproject/builds`. To find the names of existing buckets, see [list buckets](/storage/docs/listing-buckets) or [create a new bucket](/storage/docs/creating-buckets).\n- `[ARTIFACT_PATH]`: Path to one or more artifacts. `[ARTIFACT_PATH]` is relative to your working directory. This can be either `/workspace`, which is Cloud Build's default working directory or the working directory you set using the [`dir`](/build/docs/build-config-file-schema#build_steps) field.\n\n### JSON\n\n {\n \"artifacts\": {\n \"objects\": {\n \"location\": [\n \"[STORAGE_LOCATION]\"\n ],\n \"paths\": [\n [\n \"[ARTIFACT_PATH]\"\n ],\n [\n \"[ARTIFACT_PATH]\"\n ],\n \"...\"\n ]\n }\n }\n }\n\nWhere,\n\n- `[STORAGE_LOCATION]`: A Cloud Storage bucket or a folder within the bucket where Cloud Build must store the artifact, such as `gs://mybucket` or `gs://mybucket/myproject/builds`. To find the names of existing buckets, see [list buckets](/storage/docs/listing-buckets) or [create a new bucket](/storage/docs/creating-buckets).\n- `[ARTIFACT_PATH]`: Path to one or more artifacts. `[ARTIFACT_PATH]` is relative to your working directory. This can be either `/workspace`, which is Cloud Build's default working directory or the working directory you set using the [`dir`](/build/docs/build-config-file-schema#build_steps) field.\n\nNote the following caveats when storing artifacts in Cloud Storage:\n\n- You can specify only one bucket to upload the artifacts and you must be the\n owner of the bucket. You can specify a valid directory path in the bucket.\n\n- You can upload any number of artifacts, but you can specify only up to one\n hundred artifact paths.\n\n- If you upload an artifact to a bucket that already has an artifact with the same\n name, the new artifact will replace the existing artifact. You can enable\n [Object Versioning](/storage/docs/object-versioning) for your bucket if you\n don't want the newer artifact to replace an existing artifact with the same\n name.\n\nAfter the build completes successfully, you can find the upload results in the\nJSON manifest file located at `[STORAGE_LOCATION]/artifacts-$BUILD_ID.json`.\n\nThe JSON manifest file has the following fields:\n\n- `location`: this specifies the location in Cloud Storage where an artifact is stored and is of the form `gs://[STORAGE_LOCATION]/[FILE_NAME]#[GENERATION_NUMBER]`. You can use the [generation number](/storage/docs/metadata#generation-number) to uniquely identify a version of the data in Cloud Storage bucket.\n- `file_hash`: this specifies the hash type and the value. The hash type is always 2, which specifies that MD5 hash was performed.\n\nArtifacts examples\n------------------\n\nThe following examples show how you can use the `Artifacts` field in a build\nconfig file. In all of these examples replace `[VALUES_IN_BRACKETS]` with the\nappropriate values.\n\n### Uploading files and folders\n\nThe build config file below uploads `helloworld.class` to\nthe`gs://[STORAGE_LOCATION]/`: \n\n### YAML\n\n steps:\n - name: 'gcr.io/cloud-builders/javac'\n args: ['HelloWorld.java']\n artifacts:\n objects:\n location: 'gs://[STORAGE_LOCATION]/'\n paths: ['HelloWorld.class']\n\n### JSON\n\n {\n \"steps\": [\n {\n \"name\": \"gcr.io/cloud-builders/javac\",\n \"args\": [\n \"HelloWorld.java\"\n ]\n }\n ],\n \"artifacts\": {\n \"objects\": {\n \"location\": \"gs://[STORAGE_LOCATION]/\",\n \"paths\": [\n \"HelloWorld.class\"\n ]\n }\n }\n }\n\nTo upload more than one artifact, specify the path to each artifact separated by\na comma. The following example uploads `HelloWorld.java`, `HelloWorld.class`,\nand `cloudbuild.yaml` to `gs://[STORAGE_LOCATION]/`: \n\n### YAML\n\n steps:\n - name: 'gcr.io/cloud-builders/javac'\n args: ['HelloWorld.java']\n artifacts:\n objects:\n location: 'gs://[STORAGE_LOCATION]/'\n paths: ['HelloWorld.java', 'HelloWorld.class', 'cloudbuild.yaml']\n\n### JSON\n\n {\n \"steps\": [\n {\n \"name\": \"gcr.io/cloud-builders/javac\",\n \"args\": [\n \"HelloWorld.java\"\n ]\n }\n ],\n \"artifacts\": {\n \"objects\": {\n \"location\": \"gs://[STORAGE_LOCATION]/\",\n \"paths\": [\n \"HelloWorld.java\",\n \"HelloWorld.class\",\n \"cloudbuild.yaml\"\n ]\n }\n }\n }\n\nYou can also upload the artifacts to a valid directory path in the bucket. The\nfollowing example uploads `HelloWorld.java` and `HelloWorld.class` to\n`gs://[BUCKET_NAME]/[FOLDER_NAME]`: \n\n### YAML\n\n steps:\n - name: 'gcr.io/cloud-builders/javac'\n args: ['HelloWorld.java']\n artifacts:\n objects:\n location: 'gs://[BUCKET_NAME]/[FOLDER_NAME]'\n paths: ['HelloWorld.java', 'HelloWorld.class']\n\n### JSON\n\n {\n \"steps\": [\n {\n \"name\": \"gcr.io/cloud-builders/javac\",\n \"args\": [\n \"HelloWorld.java\"\n ]\n }\n ],\n \"artifacts\": {\n \"objects\": {\n \"location\": \"gs://[BUCKET_NAME]/[FOLDER_NAME]\",\n \"paths\": [\n \"HelloWorld.java\",\n \"HelloWorld.class\"\n ]\n }\n }\n }\n\n### Using wildcard characters to upload more than one artifact\n\nWhen uploading multiple artifacts, you can use\n[wildcard characters](/storage/docs/wildcards) in `paths` to specify multiple\nfiles.\n\nThe following example takes as an argument a file named `classes`, which\ncontains the names of the `.java` files to compile. It then uploads any `.class`\nfile to the specified Cloud Storage bucket: \n\n### YAML\n\n steps:\n - name: 'gcr.io/cloud-builders/javac'\n args: ['@classes']\n artifacts:\n objects:\n location: 'gs://[STORAGE_LOCATION]/'\n paths: ['*.class']\n\n### JSON\n\n {\n \"steps\": [\n {\n \"name\": \"gcr.io/cloud-builders/javac\",\n \"args\": [\n \"@classes\"\n ]\n }\n ],\n \"artifacts\": {\n \"objects\": {\n \"location\": \"gs://[STORAGE_LOCATION]/\",\n \"paths\": [\n \"*.class\"\n ]\n }\n }\n }\n\n### Using substitution variables in the bucket location\n\nYou can use [substitution\nvariables](/build/docs/configuring-builds/substitute-variable-values)\nto specify a folder within the Cloud Storage bucket. If the folder you've\nspecified doesn't exist, Cloud Build will create it for you.\n\nThe example below uploads the artifacts to a Cloud Storage path that\nincludes the name of your Google Cloud project from which the build was run\n(such as gs://mybucket/myproject/): \n\n### YAML\n\n steps:\n - name: 'gcr.io/cloud-builders/javac'\n args: ['@classes']\n artifacts:\n objects:\n location: 'gs://[BUCKET_NAME]/$PROJECT_ID'\n paths: ['helloworld.class']\n\n### JSON\n\n {\n \"steps\": [\n {\n \"name\": \"gcr.io/cloud-builders/javac\",\n \"args\": [\n \"@classes\"\n ]\n }\n ],\n \"artifacts\": {\n \"objects\": {\n \"location\": \"gs://[BUCKET_NAME]/$PROJECT_ID\",\n \"paths\": [\n \"helloworld.class\"\n ]\n }\n }\n }\n\nWhat's next\n-----------\n\n- Learn how to [build `Go` projects](/build/docs/building/build-go).\n- Learn how to start a build [manually](/build/docs/running-builds/start-build-manually) and [using triggers](/build/docs/running-builds/automate-builds).\n- Learn how to [troubleshoot build errors](/build/docs/troubleshooting)."]]