Stocker des artefacts de compilation dans Cloud Storage
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cette page explique comment stocker des artefacts de compilation dans Cloud Storage.
Nous vous recommandons d'utiliser Artifact Registry pour stocker les artefacts de compilation. Artifact Registry est un produitGoogle Cloud que vous pouvez intégrer à Cloud Build pour stocker et gérer en toute sécurité vos artefacts dans des dépôts privés ou publics.
Stocker des artefacts dans Artifact Registry vous permet de:
Gérez les métadonnées de conteneur et analysez les failles des conteneurs à l'aide de Artifact Analysis.
Pour savoir comment configurer Cloud Build pour stocker les paquets et les images de vos compilations dans Artifact Registry, consultez la section Stocker des artefacts dans Artifact Registry.
Stocker des artefacts dans Cloud Storage
Pour stocker des artefacts qui ne sont pas issus de conteneurs dans Cloud Storage, ajoutez un champ artifacts au fichier de configuration de compilation en indiquant l'emplacement du bucket où stocker l'artefact et le chemin d'accès à un ou plusieurs artefacts :
[STORAGE_LOCATION] : un bucket Cloud Storage ou un dossier du bucket dans lequel Cloud Build doit stocker l'artefact, par exemple gs://mybucket ou gs://mybucket/myproject/builds. Pour connaître les noms des buckets existants, consultez Liste des buckets ou Créer un bucket.
[ARTIFACT_PATH] : le chemin d'accès vers un ou plusieurs artefacts. [ARTIFACT_PATH] est associé à votre répertoire de travail. Il peut s'agir de /workspace, qui est le répertoire de travail par défaut de Cloud Build ou du répertoire de travail défini à l'aide du champ dir.
[STORAGE_LOCATION] : un bucket Cloud Storage ou un dossier du bucket dans lequel Cloud Build doit stocker l'artefact, par exemple gs://mybucket ou gs://mybucket/myproject/builds. Pour connaître les noms des buckets existants, consultez la section Lister les buckets ou Créer un bucket.
[ARTIFACT_PATH] : le chemin d'accès vers un ou plusieurs artefacts. [ARTIFACT_PATH] est associé à votre répertoire de travail. Il peut s'agir de /workspace, qui est le répertoire de travail par défaut de Cloud Build ou du répertoire de travail défini à l'aide du champ dir.
Tenez compte des mises en garde suivantes lorsque vous stockez des artefacts dans Cloud Storage :
Vous ne pouvez spécifier qu'un seul bucket pour importer des artefacts et devez en être le propriétaire. Vous pouvez spécifier un chemin d'accès de répertoire valide dans le bucket.
Vous pouvez importer autant d'artefacts que vous le souhaitez, mais ne pouvez spécifier que 100 chemins d'accès d'artefacts maximum.
Si vous importez un artefact dans un bucket comprenant déjà un artefact portant le même nom, le nouvel artefact remplace l'artefact existant. Vous pouvez activer la Gestion des versions des objets de votre bucket si vous ne souhaitez pas que le nouvel artefact remplace un artefact existant portant le même nom.
Une fois la compilation terminée, vous pouvez trouver les résultats de l'importation dans le fichier manifeste JSON situé à l'emplacement [STORAGE_LOCATION]/artifacts-$BUILD_ID.json.
Le fichier manifeste JSON contient les champs suivants :
location : indique l'emplacement dans Cloud Storage où un artefact est stocké et se présente sous la forme gs://[STORAGE_LOCATION]/[FILE_NAME]#[GENERATION_NUMBER]. Vous pouvez utiliser le numéro de génération pour identifier de manière unique une version des données dans le bucket Cloud Storage.
file_hash : spécifie le type de hachage et la valeur. Le type de hachage est toujours 2, ce qui signifie qu'un hachage MD5 a été effectué.
Exemples d'artefacts
Les exemples suivants montrent comment utiliser le champ Artifacts dans un fichier de configuration de compilation. Dans tous ces exemples, remplacez [VALUES_IN_BRACKETS] par les valeurs appropriées.
Importer des fichiers et des dossiers
Le fichier de configuration de compilation ci-dessous importe helloworld.class dans gs://[STORAGE_LOCATION]/ :
Pour importer plusieurs artefacts, indiquez le chemin d'accès à chaque artefact séparé par une virgule. L'exemple suivant importe HelloWorld.java, HelloWorld.class et cloudbuild.yaml dans gs://[STORAGE_LOCATION]/ :
Vous pouvez également importer des artefacts dans un chemin d'accès de répertoire valide dans le bucket. L'exemple suivant importe HelloWorld.java et HelloWorld.class dans gs://[BUCKET_NAME]/[FOLDER_NAME] :
Utiliser des caractères génériques pour importer plusieurs artefacts
Lorsque vous importez plusieurs artefacts, vous pouvez utiliser des caractères génériques dans paths pour spécifier plusieurs fichiers.
L'exemple suivant prend comme argument un fichier nommé classes, qui contient les noms des fichiers .java à compiler. Il importe ensuite tous les fichiers .class dans le bucket Cloud Storage spécifié :
Utiliser des variables de substitution dans l'emplacement du bucket
Vous pouvez utiliser des variables de substitution pour spécifier un dossier dans le bucket Cloud Storage. Si le dossier que vous avez spécifié n'existe pas, Cloud Build le crée pour vous.
L'exemple ci-dessous importe les artefacts dans un chemin d'accès Cloud Storage comprenant le nom de votre Google Cloud projet à partir duquel la compilation a été exécutée (par exemple gs://mybucket/myproject/):
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/08 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/08 (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)."]]