要加快构建速度,可以重复使用以前构建的结果。您可以将以前构建的结果复制到 Google Cloud Storage 存储分区、使用该结果进行更快速的计算,然后将新结果复制回存储分区。如果您的构建需要很长时间并生成少量文件,可以使用此方法,这些文件无需较长时间来复制到 Google Cloud Storage 以及从 Google Cloud Storage 中复制。
steps:-name:gcr.io/cloud-builders/gsutilargs:['cp','gs://mybucket/results.zip','previous_results.zip']# operations that use previous_results.zip and produce new_results.zip-name:gcr.io/cloud-builders/gsutilargs:['cp','new_results.zip','gs://mybucket/results.zip']
使用上面的构建配置构建您的代码:
gcloud builds submit --config cloudbuild.yaml .
JSON
在构建配置文件中添加相关指令,以执行以下操作:
从 Google Cloud Storage 存储分区中复制以前构建的结果。
使用当前构建的结果。
将新结果复制回存储分区。
{"steps":[{"name":"gcr.io/cloud-builders/gsutil","args":["cp","gs://mybucket/results.zip","previous_results.zip"]},{// operations that use previous_results.zip and produce new_results.zip},{"name":"gcr.io/cloud-builders/gsutil","args":["cp","new_results.zip","gs://mybucket/results.zip"]}]}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eReduce container image size by separating the application build process from the assembly of the runtime container, which minimizes the inclusion of unnecessary files.\u003c/p\u003e\n"],["\u003cp\u003eUtilize Docker's \u003ccode\u003e--cache-from\u003c/code\u003e argument to speed up image builds by leveraging previously built images as a cache source, although this is most effective when changes occur in the later layers of a Docker build.\u003c/p\u003e\n"],["\u003cp\u003eEmploy Google Cloud Storage to cache build results across builds, enabling the reuse of previous outputs to accelerate current build processes, which is especially useful for time-consuming builds that produce small files.\u003c/p\u003e\n"],["\u003cp\u003eUse a \u003ccode\u003e.gcloudignore\u003c/code\u003e file to prevent the upload of unneeded files during the build process, such as documentation, third-party dependencies, and compiled binaries, reducing upload times and improving build consistency.\u003c/p\u003e\n"]]],[],null,["# Best practices for speeding up builds\n\nThis page provides best practices for speeding up Cloud Build builds.\n\nBuilding leaner containers\n--------------------------\n\nWhen you containerize an application, files that are not needed at runtime, such\nas build-time dependencies and intermediate files, can be inadvertently included\nin the container image. These unneeded files can increase the size of the\ncontainer image and add extra time and cost as the image moves between your\ncontainer image registry and your container runtime.\n\nTo help reduce the size of your container image, separate the building of the\napplication, along with the tools used to build it, from the assembly of the\nruntime container.\n\nFor more information, see [Building leaner containers](/build/docs/building-leaner-containers).\n\nUsing a cached Docker image\n---------------------------\n\nThe easiest way to increase the speed of your Docker image build is by\nspecifying a cached image that can be used for subsequent builds. You can\nspecify the cached image by adding the `--cache-from` argument in your build\nconfig file, which will instruct Docker to build using that image as a cache\nsource.\n\nEach Docker image is made up of stacked layers. Using `--cache-from` rebuilds\nall the layers from the changed layer until the end of the build; therefore\nusing `--cache-from` is not beneficial if you change a layer in the earlier\nstages of your Docker build.\n\nIt is recommended that you always use `--cache-from` for your builds, but keep\nthe following caveats in mind:\n\n- You need a previously built Docker image to cache from.\n- You can use `--cache-from` only for Docker builds; you cannot use it for builders that create other kind of artifacts.\n- The cached image must be retrieved from a registry, which may add to the time it takes to build.\n\nThe following steps explain how to build using a previously cached image: \n\n### YAML\n\n1. In your build config, add instructions to:\n\n - Pull the cached image from Container Registry. Notice that the `docker\n pull` build step below sets the `entrypoint` to `bash`, which allows you to run the command and ignore the returned error. This is required when you build an image for the first time, and `docker pull` does not have an existing image to pull.\n - Add a `--cache-from` argument to use that image for rebuilds.\n\n steps:\n - name: 'gcr.io/cloud-builders/docker'\n entrypoint: 'bash'\n args: ['-c', 'docker pull gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest || exit 0']\n - name: 'gcr.io/cloud-builders/docker'\n args: [\n 'build',\n '-t', 'gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest',\n '--cache-from', 'gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest',\n '.'\n ]\n images: ['gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest']\n\n where \\[IMAGE_NAME\\] is the name of your image.\n2. Build your image using the above build config:\n\n gcloud builds submit --config cloudbuild.yaml .\n\n### JSON\n\n1. In your build config, add instructions to:\n\n - Pull the cached image from Container Registry. Notice that the `docker\n pull` build step below sets the `entrypoint` to `bash`, which allows you to run the command and ignore any returned errors. This is required when you build an image for the first time, and `docker pull` does not have an existing image to pull.\n - Add a `--cache-from` argument to use that image for rebuilds.\n\n {\n \"steps\": [\n {\n \"name\": \"gcr.io/cloud-builders/docker\",\n \"entrypoint\": \"bash\",\n \"args\": [\"-c\", \"docker pull gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest || exit 0\"]\n },\n {\n \"name\": \"gcr.io/cloud-builders/docker\",\n \"args\": [\n \"build\",\n \"-t\",\n \"gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest\",\n \"--cache-from\",\n \"gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest\",\n \".\"\n ]\n }\n ],\n \"images\": [\"gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest\"]\n }\n\n where \\[IMAGE_NAME\\] is the name of your image.\n2. Build your image using the above build config:\n\n gcloud builds submit --config cloudbuild.json .\n\nCaching directories with Google Cloud Storage\n---------------------------------------------\n\nTo increase the speed of a build, reuse the results from a previous build. You\ncan copy the results of a previous build to a Google Cloud Storage bucket, use\nthe results for faster calculation, and then copy the new results back to the\nbucket. Use this method when your build takes a long time and produces a small\nnumber of files that does not take time to copy to and from Google Cloud\nStorage.\n\nUnlike `--cache-from`, which is only for Docker builds, Google Cloud Storage\ncaching can be used for\n[any builder supported by Cloud Build](https://github.com/GoogleCloudPlatform/cloud-builders).\n\nUse the following steps to cache directories using Google Cloud Storage: \n\n### YAML\n\n1. In your build config file, add instructions to:\n\n - Copy the results of a previous build from the Google Cloud Storage bucket.\n - Use the results for the current build.\n - Copy the new results back into the bucket.\n\n steps:\n - name: gcr.io/cloud-builders/gsutil\n args: ['cp', 'gs://mybucket/results.zip', 'previous_results.zip']\n # operations that use previous_results.zip and produce new_results.zip\n - name: gcr.io/cloud-builders/gsutil\n args: ['cp', 'new_results.zip', 'gs://mybucket/results.zip']\n\n2. Build your code using the above build config:\n\n gcloud builds submit --config cloudbuild.yaml .\n\n### JSON\n\n1. In your build config file, add instructions to:\n\n - Copy the results of a previous build from the Google Cloud Storage bucket.\n - Use the results for the current build.\n - Copy the new results back into the bucket.\n\n {\n \"steps\": [\n {\n \"name\": \"gcr.io/cloud-builders/gsutil\",\n \"args\": [\"cp\", \"gs://mybucket/results.zip\", \"previous_results.zip\"]\n },\n {\n // operations that use previous_results.zip and produce new_results.zip\n },\n {\n \"name\": \"gcr.io/cloud-builders/gsutil\",\n \"args\": [\"cp\", \"new_results.zip\", \"gs://mybucket/results.zip\"]\n }\n ]\n }\n\n2. Build your code using the above build config:\n\n gcloud builds submit --config cloudbuild.json .\n\nAvoiding the upload of unnecessary files\n----------------------------------------\n\nWhen a build is triggered, your code directory is uploaded for use by\nCloud Build.\n\nYou can exclude files not needed by your build with a\n[`.gcloudignore`](/sdk/gcloud/reference/topic/gcloudignore)\nfile to optimize the upload time.\n\nExamples of commonly excluded files include:\n\n- Documentation and sample code for project developers\n- Generated scaffolding code, binaries, `*.jar` files, or compiled web assets used for development.\n- Vendored, third-party dependencies for local development\n\nTo prepare a `.gcloudignore` file to address these cases, create a file in your\nproject root with contents such as: \n\n .git\n dist\n node_modules\n vendor\n *.jar\n\nExcluding compiled code and third-party dependencies also results in a more\nconsistent build process and a reduced risk of accidental deployment of code\nthat is still under active development.\n\nWhat's next\n-----------\n\n- Learn how to [increase vCPU for builds](/build/docs/optimize-builds/increase-vcpu-for-builds)."]]