Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menjelaskan cara mengonfigurasi Cloud Build untuk membangun dan menguji aplikasi Python Anda, mengupload artefak ke Artifact Registry, membuat informasi asal, dan menyimpan log pengujian di Cloud Storage.
Cloud Build memungkinkan Anda menggunakan image container yang tersedia secara publik untuk menjalankan tugas. Image python publik dari Docker Hub sudah diinstal sebelumnya dengan alat python dan pip. Anda dapat mengonfigurasi Cloud Build agar menggunakan alat ini untuk menginstal dependensi, mem-build, dan menjalankan pengujian unit menggunakan alat ini.
Sebelum memulai
Petunjuk di halaman ini mengasumsikan bahwa Anda sudah memahami Python. Selain itu:
Enable the Cloud Build, Artifact Registry, and Cloud Storage APIs.
Bagian ini membahas contoh file konfigurasi build untuk aplikasi Python. File ini memiliki
langkah-langkah build untuk mengelola persyaratan penginstalan, menambahkan pengujian unit, dan setelah pengujian lulus, untuk membangun dan men-deploy aplikasi.
Persyaratan penginstalan: Image python dari Docker Hub sudah diinstal sebelumnya
dengan pip. Untuk menginstal dependensi dari pip, tambahkan langkah build dengan kolom berikut:
name: Tetapkan nilai kolom ini ke python atau python:<tag> untuk menggunakan
image python dari Docker Hub untuk tugas ini. Untuk melihat daftar tag yang tersedia
untuk image Python lainnya, lihat referensi Docker Hub untuk image python.
entrypoint: Menetapkan kolom ini akan menggantikan titik entri default gambar
yang dirujuk di name. Tetapkan nilai kolom ini ke pip untuk memanggil pip
sebagai entrypoint langkah build dan menjalankan perintah pip.
args: Kolom args dari langkah build mengambil daftar argumen dan
meneruskannya ke image yang dirujuk oleh kolom name. Teruskan argumen
untuk menjalankan perintah pip install di kolom ini. Flag --user dalam perintah pip install
memastikan bahwa langkah-langkah build berikutnya dapat mengakses modul
yang diinstal dalam langkah build ini.
Langkah build berikut menambahkan argumen untuk menginstal persyaratan:
Menambahkan pengujian unit: Jika telah menentukan pengujian unit di aplikasi menggunakan framework pengujian seperti pytest, Anda dapat mengonfigurasi Cloud Build untuk menjalankan pengujian dengan menambahkan kolom berikut dalam langkah build:
name: Tetapkan nilai kolom ini ke python untuk menggunakan image python dari
Docker Hub untuk tugas Anda.
entrypoint: Tetapkan nilai kolom ini ke python untuk menjalankan perintah python.
args: Tambahkan argumen untuk menjalankan perintah python pytest.
Langkah build berikut menyimpan output log pytest ke file XML JUNIT.
Nama file ini dibuat menggunakan $SHORT_SHA, versi singkat ID commit yang terkait
dengan build Anda.
Langkah build berikutnya akan menyimpan log dalam file ini ke Cloud Storage.
Untuk mengaktifkan pembuatan provenance, tambahkan
requestedVerifyOption: VERIFIED
ke bagian options dalam file konfigurasi Anda.
Menyimpan log pengujian ke Cloud Storage: Anda dapat mengonfigurasi Cloud Build untuk menyimpan log pengujian apa pun di Cloud Storage dengan menentukan lokasi bucket dan jalur yang ada ke log pengujian.
Langkah build berikut menyimpan log pengujian yang Anda simpan dalam file JUNIT XML ke bucket Cloud Storage:
[[["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-08-18 UTC."],[[["\u003cp\u003eCloud Build can be configured to build and test Python applications using the publicly available \u003ccode\u003epython\u003c/code\u003e Docker image, which includes pre-installed \u003ccode\u003epython\u003c/code\u003e and \u003ccode\u003epip\u003c/code\u003e tools.\u003c/p\u003e\n"],["\u003cp\u003eYou can use Cloud Build to install Python dependencies, run unit tests with frameworks like \u003ccode\u003epytest\u003c/code\u003e, and save the test logs to a JUNIT XML file.\u003c/p\u003e\n"],["\u003cp\u003eCloud Build allows you to upload your built Python artifacts to Artifact Registry by specifying the repository details in your build configuration file, and you can specify where to save the built artifacts.\u003c/p\u003e\n"],["\u003cp\u003eYou can optionally enable verifiable build provenance metadata using Supply chain Levels for Software Artifacts (SLSA) by adding \u003ccode\u003erequestedVerifyOption: VERIFIED\u003c/code\u003e to your build configuration file.\u003c/p\u003e\n"],["\u003cp\u003eYou can save any test logs to a Cloud Storage bucket by specifying a bucket location and the paths to the log files in your build configuration.\u003c/p\u003e\n"]]],[],null,["# Build and test Python applications\n\nThis page describes how to configure Cloud Build to build and test your Python applications, upload your artifacts to Artifact Registry, generate provenance information, and save your test logs in Cloud Storage.\n\nCloud Build enables you to use any publicly available container image\nto execute your tasks. The public\n[`python` image from Docker Hub](https://hub.docker.com/_/python/)\ncomes preinstalled with `python` and `pip` tools. You can configure Cloud Build\nuse these tools to install dependencies, build, and run unit tests using these tools.\n\nBefore you begin\n----------------\n\nThe instructions on this page assume that you are familiar with Python. In addition:\n\n-\n\n\n Enable the Cloud Build, Artifact Registry, and Cloud Storage APIs.\n\n\n [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=cloudbuild.googleapis.com,artifactregistry.googleapis.com,storage.googleapis.com&redirect=https://cloud.google.com/build/docs/building/build-python)\n- To run the `gcloud` commands on this page, install [Google Cloud CLI](/sdk).\n- Have your Python project handy.\n- Have a Python repository in Artifact Registry. If you don't have one, then [create a new repository](/artifact-registry/docs/repositories/create-repos).\n- If you want to store test logs in Cloud Storage, [create a bucket in Cloud Storage](/storage/docs/creating-buckets).\n\n### Required IAM permissions\n\n- To store test logs in Logging, grant the\n [Storage Object Creator (`roles/storage.objectCreator`)](/iam/docs/understanding-roles#storage-roles)\n role for the Cloud Storage bucket to your build service account.\n\n- To store built images in Artifact Registry, grant the [Artifact Registry Writer\n (`roles/artifactregistry.writer`) role](/artifact-registry/docs/access-control#grant)\n to the your build service account.\n\nFor instructions on granting these roles see\n[Granting a role using the IAM page](/build/docs/securing-builds/configure-access-for-cloud-build-service-account#granting_a_role_using_the_iam_page).\n\nConfiguring Python builds\n-------------------------\n\nThis section walks through an example build config file for a Python app. It has\nbuild steps to manage installation requirements, add unit tests, and after the tests pass, to build and deploy the app.\n\n1. In your project root directory, create [Cloud Build config file](/build/docs/build-config)\n named `cloudbuild.yaml`.\n\n2. **Install requirements** : The `python` image from Docker Hub comes preinstalled\n with `pip`. To install dependencies from `pip`, add a build step with the\n following fields:\n\n - `name`: Set the value of this field to `python` or `python:\u003ctag\u003e` to use the python image from Docker Hub for this task. To see a list of available tags for other Python images, see the [Docker Hub reference for the python image](https://hub.docker.com/_/python/tags).\n - `entrypoint`: Setting this field overrides the default entrypoint of the image referenced in `name`. Set the value of this field to `pip` to invoke `pip` as the entrypoint of the build step and run `pip` commands.\n - `args`: The `args` field of a build step takes a list of arguments and passes them to the image referenced by the `name` field. Pass the arguments to run the `pip install` command in this field. `--user` flag in the `pip install` command ensures that the subsequent build steps can access the modules installed in this build step.\n\n The following build step adds arguments to install requirements: \n\n steps:\n - name: 'python'\n entrypoint: 'python'\n args: ['-m', 'pip', 'install', '--upgrade', 'pip']\n - name: python\n entrypoint: python\n args: ['-m', 'pip', 'install', 'build', 'pytest', 'Flask', '--user']\n\n3. **Add unit tests** : If you've defined unit tests in your application using a\n testing framework such as `pytest`, you can configure Cloud Build\n to run the tests by adding the following fields in a build step:\n\n - `name`: Set the value of this field to `python` to use the python image from Docker Hub for your task.\n - `entrypoint`: Set the value of this field to `python` to run `python` commands.\n - `args`: Add the arguments for running the `python pytest` command.\n\n The following build step saves the `pytest` log output to a JUNIT XML file.\n The name of this file is constructed using `$SHORT_SHA`, [the short version of the commit ID associated\n with your build](/build/docs/configuring-builds/substitute-variable-values#using_default_substitutions).\n A subsequent build step will save the logs in this file to Cloud Storage. \n\n - name: 'python'\n entrypoint: 'python'\n args: ['-m', 'pytest', '--junitxml=${SHORT_SHA}_test_log.xml']\n\n4. **Build** : In your build config file, define the builder and the `args` to build your application:\n\n - `name`: Set the value of this field to `python` to use the python image from Docker Hub for your task.\n - `entrypoint`: Set the value of this field to `python` to run `python` commands.\n - `args`: Add the arguments for executing your build.\n\n The following build step starts the build: \n\n - name: 'python'\n entrypoint: 'python'\n args: ['-m', 'build']\n\n5. **Upload to Artifact Registry**:\n\n In your config file, add the `pythonPackages` field and specify your Python repository in Artifact Registry: \n\n artifacts:\n pythonPackages:\n - repository: 'https://\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e-python.pkg.dev/\u003cvar translate=\"no\"\u003ePROJECT-ID\u003c/var\u003e/\u003cvar translate=\"no\"\u003eREPOSITORY\u003c/var\u003e'\n paths: ['dist/*']\n\n Replace the following values:\n - \u003cvar translate=\"no\"\u003ePROJECT-ID\u003c/var\u003e is the ID of the Google Cloud project that contains your Artifact Registry repository.\n - \u003cvar translate=\"no\"\u003eREPOSITORY\u003c/var\u003e is the ID of the repository.\n - \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e is the regional or multi-regional [location](/artifact-registry/docs/repo-locations) for the repository.\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. **Save test logs to Cloud Storage**: You can configure Cloud Build\n to store any test logs in Cloud Storage by specifying an existing bucket\n location and path to the test logs.\n The following build step stores the test logs that you saved in the JUNIT XML\n file to a Cloud Storage bucket:\n\n artifacts:\n objects:\n location: 'gs://${_BUCKET_NAME}/'\n paths:\n - '${SHORT_SHA}_test_log.xml'\n\n8. **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\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 and containerize Python applications](/build/docs/building/build-containerize-python).\n- Learn how to [use private dependencies](/artifact-registry/docs/configure-cloud-build#python).\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)."]]