Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Auf dieser Seite wird erläutert, wie Sie mit Cloud Build Node.js-Anwendungen erstellen und testen, erstellte Artefakte in einem npm-Repository in der Artifact Registry speichern und Informationen zur Buildherkunft generieren.
Mit Cloud Build können Sie jedes öffentlich verfügbare Container-Image zur Ausführung Ihrer Aufgaben verwenden. Im öffentlichen node-Image aus Docker Hub ist das Tool npm vorinstalliert. Sie können Cloud Build so konfigurieren, dass Ihr Node.js-Projekt mit diesem Tool erstellt wird.
Hinweise
Die Anleitung auf dieser Seite setzt voraus, dass Sie mit Node.js vertraut sind. Außerdem gilt:
Zum Ausführen der gcloud-Befehle auf dieser Seite müssen Sie die Google Cloud CLI installieren.
Mit npm erstellen
Damit die Aufgaben im Image node von Docker Hub ausgeführt werden, geben Sie in der Cloud Build-Konfigurationsdatei im Feld name die Image-URL an.
Cloud Build startet den im Feld name angegebenen Container mithilfe des Standardeinstiegspunkts des Images. Wenn Sie den Standardeinstiegspunkt überschreiben und definieren möchten, wie der Build-Schritt ausgeführt werden soll, wenn er ausgelöst wird, fügen Sie dem Build-Schritt ein entrypoint-Feld hinzu. Beim Image node in Docker Hub ist das Tool npm vorinstalliert. Geben Sie die Tools im Feld entrypoint an, um sie als Einstiegspunkt für den Build-Schritt aufzurufen.
Für die folgende Beispiel-Build-Konfigurationsdatei gilt:
Das Feld name gibt an, dass das Image node aus Docker Hub von Cloud Build zum Ausführen Ihrer Aufgabe verwendet wird. Wenn Sie das Image node angeben, können Sie entweder die Knotenversion weglassen, sodass der Standardwert :latest verwendet wird, oder eine Knotenversion festlegen, um eine bestimmte Version zu verwenden. Beispiel: name: node verwendet die neueste Version des Knotens und name: node:12 verwendet node:12.
Das Feld entrypoint gibt an, dass das Tool npm verwendet wird, wenn das Image node aufgerufen wird.
steps:
- name: 'node'
entrypoint: 'npm'
Node.js-Builds konfigurieren
Erstellen Sie im Stammverzeichnis des Projekts eine Konfigurationsdatei mit dem Namen cloudbuild.yaml.
Abhängigkeiten installieren: Bevor Sie Ihre Anwendung erstellen können, müssen Sie sicherstellen, dass alle Abhängigkeiten Ihres Projekts mit npm installiert werden. Sie können Abhängigkeiten mit dem install-Befehl innerhalb des npm-Build-Schritts installieren. Im Feld args eines Build-Schritts wird eine Liste von Argumenten abgerufen und an das Image übergeben, auf das im Feld „Name“ verwiesen wird. Fügen Sie in Ihrer Build-Konfigurationsdatei install zum args-Feld hinzu, um den install-Befehl aufzurufen:
Tests hinzufügen: Wenn Sie in der package.json ein test-Skript definiert haben, können Sie Cloud Build so konfigurieren, dass es das Skript ausführt, indem Sie test zum Feld args hinzufügen:
Benutzerdefinierte Befehle ausführen: Wenn die package.json benutzerdefinierte Befehle enthält, können Sie Cloud Build für die Ausführung dieser Befehle konfigurieren. Fügen Sie im Feld args als erstes Argument run hinzu, gefolgt vom Namen des benutzerdefinierten Befehls. Die folgende Build-Konfigurationsdatei enthält Argumente zum Ausführen eines benutzerdefinierten Befehls namens build:
LOCATION: der Speicherort Ihres Repositorys in Artifact Registry.
PROJECT_ID: die ID des Google Cloud Projekts, das Ihr Artifact Registry-Repository enthält.
REPOSITORY_NAME: der Name Ihres npm-Repositorys in der Artifact Registry.
PACKAGE_PATH: der Pfad zum lokalen Verzeichnis mit dem npm-Paket, das Sie in Artifact Registry hochladen möchten. Wir empfehlen, einen absoluten Pfad zu verwenden. Der Wert für PACKAGE_PATH kann . sein, um das aktuelle Arbeitsverzeichnis zu verwenden. Das Feld darf jedoch nicht weggelassen oder leer gelassen werden. Dieses Verzeichnis muss eine package.json-Datei enthalten.
Wenn Sie die Generierung der Provenienz aktivieren möchten, fügen Sie in der Konfigurationsdatei dem Abschnitt options den Wert requestedVerifyOption: VERIFIED hinzu.
Manchmal muss Ihr Projekt in mehreren Versionen von node funktionieren. Sie können Cloud Build-Trigger folgendermaßen erstellen und konfigurieren:
Geben Sie in der Build-Konfigurationsdatei die node-Version als eine Substitutionsvariable an.
Erstellen Sie einen Trigger für jede Version von node, mit der Sie Ihre Anwendung erstellen möchten.
Verwenden Sie in den einzelnen Triggereinstellungen das Feld für den Wert der Substitutionsvariable, um die Version von node für den jeweiligen Trigger anzugeben.
In den folgenden Schritten wird erläutert, wie Sie die node-Version mithilfe von triggerspezifischen Substitutionsvariablen angeben:
Fügen Sie Ihrem Repository-Stammverzeichnis eine Build-Konfigurationsdatei hinzu, die die node-Version als Substitutionsvariable angibt. Im folgenden Beispiel für eine Build-Konfigurationsdatei ist $_NODE_VERSION eine benutzerdefinierte Substitutionsvariable:
Wählen Sie oben auf der Seite im Drop-down-Menü zur Projektauswahl Ihr Projekt aus.
Klicken Sie auf Öffnen.
Klicken Sie auf Trigger erstellen.
Auf der Seite Erstellen Sie einen Trigger geben Sie die folgenden Einstellungen ein:
Geben Sie einen Namen für den Trigger ein.
Wählen Sie das Repository-Ereignis aus, das den Trigger aufrufen soll.
Wählen Sie das Repository aus, das Ihren Quellcode und die Build-Konfigurationsdatei enthält.
Geben Sie den regulären Ausdruck für den Zweig- oder Tag-Namen an, mit dem der Trigger gestartet wird.
Konfiguration: Wählen Sie die Build-Konfigurationsdatei aus, die Sie zuvor erstellt haben.
Klicken Sie unter Substitutionsvariablen auf Variable hinzufügen.
Geben Sie unter Variable die node-Versionsvariable an, die Sie in der Build-Konfigurationsdatei verwendet haben, und unter Wert die Version von node. Zum Beispiel: _NODE_VERSION und 12
Klicken Sie auf Erstellen, um den Build-Trigger zu speichern.
Sie können diese Trigger verwenden, um Ihren Code auf Basis der im Trigger angegebenen Version von node zu erstellen.
[[["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-09-04 (UTC)."],[[["\u003cp\u003eCloud Build can be used to build and test Node.js applications using the public \u003ccode\u003enode\u003c/code\u003e image from Docker Hub, which comes pre-installed with the \u003ccode\u003enpm\u003c/code\u003e tool.\u003c/p\u003e\n"],["\u003cp\u003eYou can configure Cloud Build to install dependencies, run tests, and execute custom commands defined in your \u003ccode\u003epackage.json\u003c/code\u003e file within the \u003ccode\u003enode\u003c/code\u003e environment.\u003c/p\u003e\n"],["\u003cp\u003eBuilt artifacts can be uploaded to an npm repository in Artifact Registry by specifying the repository details and package path in your \u003ccode\u003ecloudbuild.yaml\u003c/code\u003e configuration file.\u003c/p\u003e\n"],["\u003cp\u003eCloud Build allows for generating verifiable SLSA build provenance metadata to enhance the security of your continuous integration pipeline.\u003c/p\u003e\n"],["\u003cp\u003eYou can run tests on multiple Node.js versions by using substitution variables in the build config file and creating separate Cloud Build triggers for each version.\u003c/p\u003e\n"]]],[],null,["# Build and test Node.js applications\n\nThis page explains how to use Cloud Build to build and test `Node.js`\napplications, store built artifacts in an npm repository in Artifact Registry, and\ngenerate build provenance information.\n\nCloud Build enables you to use any publicly available container image\nto execute your tasks. The public\n[`node` image from Docker Hub](https://hub.docker.com/_/node/)\ncomes preinstalled with the `npm` tool. You can configure Cloud Build\nto build your `Node.js` project with this tool.\n\nBefore you begin\n----------------\n\nThe instructions on this page assume that you are familiar with `Node.js`. In\naddition:\n\n- Be familiar with [npm](https://docs.npmjs.com/about-npm).\n- Have your `Node.js` project handy, including `package.json` and `test.js` files.\n- Make sure your `package.json` file includes a `start` script and a `test` script.\n- Be familiar with [how to write a Cloud Build configuration file](/build/docs/build-config).\n- Have an npm repository in Artifact Registry. If you don't have one, [create a new repository](/artifact-registry/docs/repositories/create-repos).\n- To run the `gcloud` commands in this page, install the [Google Cloud CLI](/sdk).\n\nBuilding with `npm`\n-------------------\n\nTo execute your tasks in the `node` image from Docker Hub, specify the image URL\nin the `name` field in the [Cloud Build config file](/build/docs/build-config-file-schema).\nCloud Build starts the container specified in the `name`\nfield using the image's default entrypoint. To override the default entrypoint\nand define how the build step should be run when it is invoked, add an\n`entrypoint` field in your build step. The `node` image in Docker Hub comes\npreinstalled with the `npm` tool. Specify the tools in the `entrypoint` field to\ninvoke them as the entrypoint of your build step.\n\nIn the following example build config file:\n\n- The `name` field specifies that the `node` image from Docker Hub is used by Cloud Build to execute your task. When you're specifying the `node` image, you can either omit the node version to default to `:latest`, or specify a [node version](https://hub.docker.com/_/node/) to use a specific version. For example, `name: node` will use the latest version of node, and `name: node:12` will use `node:12`.\n- The `entrypoint` field specifies that the `npm` tool is used\n when the `node` image is invoked.\n\n steps:\n - name: 'node'\n entrypoint: 'npm'\n\nConfiguring `Node.js` builds\n----------------------------\n\n1. In your project root directory, create a\n config file named `cloudbuild.yaml`.\n\n2. **Install dependencies** : Before you can build your application, you must\n ensure that all of your project's dependencies are installed from `npm`. You\n can install dependencies using the `install` command within the `npm` build\n step. The `args` field of a build step takes a list of arguments and passes\n them to the image referenced by the name field. In your build config file,\n add `install` to the `args` field to invoke the `install` command:\n\n steps:\n - name: 'node'\n entrypoint: 'npm'\n args: ['install']\n\n3. **Add tests** : If you've defined a `test` script in your `package.json`, you\n can configure Cloud Build to run the script by adding `test` to the\n `args` field:\n\n steps:\n - name: 'node'\n entrypoint: 'npm'\n args: ['install']\n - name: 'node'\n entrypoint: 'npm'\n args: ['test']\n\n4. **Run custom commands** : If your `package.json` contains any custom commands,\n you can configure Cloud Build to run that command. In the `args`\n field, add `run` as the first argument followed by the name of the custom\n command. The following build config file has arguments to run a custom\n command called `build`:\n\n steps:\n - name: 'node'\n entrypoint: 'npm'\n args: ['install']\n - name: 'node'\n entrypoint: 'npm'\n args: ['test']\n - name: 'node'\n entrypoint: 'npm'\n args: ['run', 'build']\n\n5. **Upload to Artifact Registry**:\n\n In your config file, add the `npmPackages` field and specify your npm\n repository in Artifact Registry: \n\n artifacts:\n npmPackages:\n - repository: 'https://\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e-npm.pkg.dev/\u003cvar translate=\"no\"\u003ePROJECT-ID\u003c/var\u003e/\u003cvar translate=\"no\"\u003eREPOSITORY_NAME\u003c/var\u003e'\n packagePath: '\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nx\"\u003ePACKAGE_PATH\u003c/span\u003e\u003c/var\u003e'\n\n Replace the following values:\n - \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e: the [location](/artifact-registry/docs/repo-locations) for your repository in Artifact Registry.\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the ID of the Google Cloud project that contains your Artifact Registry repository.\n - \u003cvar translate=\"no\"\u003eREPOSITORY_NAME\u003c/var\u003e: the name of your npm repository in Artifact Registry.\n - \u003cvar translate=\"no\"\u003ePACKAGE_PATH\u003c/var\u003e: the path for the local directory containing the npm package that you want to upload to Artifact Registry. We recommend using an absolute path. Your \u003cvar translate=\"no\"\u003ePACKAGE_PATH\u003c/var\u003e value can be `.` to use the current working directory, but the field cannot be omitted or left empty. This directory must contain a `package.json` file.\n6. **Optional: Enable provenance generation**\n\n Cloud Build can generate verifiable\n [Supply chain Levels for Software Artifacts (SLSA)](https://slsa.dev/) build\n provenance metadata to help secure your continuous integration pipeline.\n\n To enable provenance generation, add\n [`requestedVerifyOption: VERIFIED`](/build/docs/build-config-file-schema#options)\n to the `options` section in your config file.\n7. **Start your build** : [manually](/build/docs/running-builds/start-build-manually) or\n [using build triggers](/build/docs/automating-builds/create-manage-triggers).\n\n Once your build completes, you can [view repository details](/artifact-registry/docs/repositories/list-repos)\n in Artifact Registry.\n\n You can also [view build provenance metadata](/build/docs/securing-builds/generate-validate-build-provenance#view) and [validate provenance](/build/docs/securing-builds/generate-validate-build-provenance#validate_provenance).\n\nRunning tests on multiple `node` versions\n-----------------------------------------\n\nSometimes it is necessary to ensure that your project works across multiple\nversions of `node`. You can create and configure\n[Cloud Build triggers](/build/docs/running-builds/create-manage-triggers)\nsuch that:\n\n- In your build config file, specify the `node` version as a [substitution variable](/build/docs/configuring-builds/substitute-variable-values).\n- Create one trigger for each version of `node` against which you want to build your application.\n- In each of the trigger settings, use the substitution variable value field to indicate the version of the `node` for that trigger.\n\nThe following steps explain how to specify the `node` version using trigger-specific\nsubstitution variables:\n\n1. In your repository root, add a build config file, which specifies the\n `node`version as a substitution variable. In the following example build\n config file, `$_NODE_VERSION` is a [user-defined substitution variable](/build/docs/configuring-builds/substitute-variable-values#using_user-defined_substitutions):\n\n steps:\n - name: 'node:$_NODE_VERSION'\n entrypoint: 'npm'\n args: ['install']\n - name: 'node:$_NODE_VERSION'\n entrypoint: 'npm'\n args: ['test']\n\n2. For each version of `node` you want to build against, create a build trigger\n using the following steps:\n\n 1. Open the **Triggers** page in the Google Cloud console:\n\n [Open Triggers page](https://console.cloud.google.com/cloud-build/triggers)\n 2. Select your project from the project selector drop-down menu at the top of\n the page.\n\n 3. Click **Open**.\n\n 4. Click **Create trigger**.\n\n On the **Create trigger** page, enter the following settings:\n 1. Enter a name for your trigger.\n\n 2. Select the repository event to start your trigger.\n\n 3. Select the repository that contains your source code and build\n config file.\n\n 4. Specify the regex for the branch or tag name that will start your\n trigger.\n\n 5. **Configuration**: Choose the build config file you created\n previously.\n\n 6. Under **Substitution variables** , click **Add variable**.\n\n 1. Under **Variable** , specify the `node` version variable you used in your build config file, and under **Value** specify the version of the `node`. For example, `_NODE_VERSION` and `12`.\n 5. Click **Create** to save your build trigger.\n\nYou can use these triggers to build your code on the version of `node` you\nspecified in the trigger.\n\nWhat's next\n-----------\n\n- Learn how to [view build results](/build/docs/view-build-results).\n- Learn how to [safeguard builds](/software-supply-chain-security/docs/safeguard-builds).\n- Learn how to [build container images](/build/docs/building/build-containers).\n- Learn how to [build Go applications](/build/docs/building/build-go).\n- Learn how to [perform blue/green deployments on Compute Engine](/build/docs/deploying-builds/deploy-compute-engine).\n- Learn how to [troubleshoot build errors](/build/docs/troubleshooting)."]]