Menetapkan variabel lingkungan build (deploy sumber)
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Variabel lingkungan build adalah key-value pair yang memungkinkan Anda meneruskan informasi konfigurasi ke buildpack saat men-deploy dari kode sumber. Misalnya, pada waktu build, Anda mungkin ingin menyesuaikan
opsi compiler, menentukan sertifikat waktu build, mengonfigurasi parameter, dan sebagainya.
Halaman ini menunjukkan cara menetapkan variabel lingkungan build yang tersedia pada
waktu build, dan relevan bagi developer platform yang men-deploy
layanan atau
fungsi Cloud Run dari sumber. Flag gcloud CLI variabel lingkungan build didukung untuk deployment sumber (--source), dan tidak didukung untuk deployment image penampung (--image).
Anda dapat menggunakan variabel lingkungan untuk konfigurasi layanan atau fungsi, tetapi kami tidak
menyarankan variabel lingkungan sebagai cara untuk menyimpan rahasia seperti kredensial database atau kunci API.
Simpan nilai sensitif di luar kode sumber dan variabel lingkungan Anda. Untuk
menyimpan secret, sebaiknya gunakan Secret Manager.
Untuk mengonfigurasi layanan yang mengakses secret yang disimpan di Secret Manager,
lihat Mengonfigurasi secret.
Service Account User (roles/iam.serviceAccountUser) di identitas layanan Cloud Run
Klik untuk melihat peran yang diperlukan untuk akun layanan Cloud Build
Cloud Build secara otomatis menggunakan akun layanan default Compute Engine sebagai akun layanan Cloud Build default untuk mem-build kode sumber dan resource Cloud Run, kecuali jika Anda mengganti perilaku ini. Agar
Cloud Build dapat mem-build sumber, minta administrator untuk memberikan
Cloud Run Builder
(roles/run.builder) ke akun layanan default Compute Engine
di project Anda:
Ganti PROJECT_NUMBER dengan nomor project Google Cloud, dan PROJECT_ID dengan project ID Google Cloud. Untuk mengetahui petunjuk mendetail tentang cara menemukan project ID dan nomor project,
lihat Membuat
dan mengelola project.
Pemberian peran builder Cloud Run ke akun layanan default Compute Engine memerlukan waktu beberapa menit untuk disebarkan.
KEY1=VALUE1,KEY2=VALUE2 dengan daftar nama variabel yang dipisahkan koma dan nilainya yang di-deploy bersama fungsi yang memungkinkan Anda meneruskan informasi konfigurasi ke buildpack.
Jika Anda men-deploy fungsi, tambahkan flag --function dengan titik masuk fungsi dari kode sumber Anda.
Memperbarui variabel lingkungan build
Anda dapat memperbarui variabel lingkungan build untuk layanan yang ada. Ini adalah
pendekatan non-destruktif yang mengubah atau menambahkan variabel lingkungan build, tetapi
tidak menghapus variabel lingkungan build.
gcloud
Untuk memperbarui variabel lingkungan build untuk layanan yang ada, gunakan
flag --update-build-env-vars:
[[["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-09-04 UTC."],[],[],null,["# Set build environment variables (source deploy)\n\nBuild environment variables are key-value pairs that let you pass configuration\ninformation to [buildpacks](/docs/buildpacks/overview) when deploying\nfrom source code. For example, at build time, you might want to customize\ncompiler options, specify build-time certificates, configure parameters, and so\nforth.\n\nThis page shows how to set build environment variables that are available at\nbuild time, and is relevant for platform developers who are deploying\nCloud Run [services](/run/docs/deploying-source-code) or\n[functions](/run/docs/deploy-functions) from source. The build environment\nvariable gcloud CLI flags are supported for\n[source deployments](/run/docs/deploying-source-code) (`--source`), and not\nsupported for [container image deployments](/run/docs/deploying) (`--image`).\n\nYou can use environment variables for service or function configuration, but we don't\nrecommend them as a way to store secrets such as database credentials or API keys.\nStore sensitive values outside both your source code and environment variables. To\nstore secrets, we recommend using Secret Manager.\nTo configure services that access secrets stored in Secret Manager,\nsee [Configure secrets](/run/docs/configuring/secrets).\n\nBefore you begin\n----------------\n\n- Enable the Cloud Run Admin API and the Cloud Build API:\n\n ```bash\n gcloud services enable run.googleapis.com \\\n cloudbuild.googleapis.com\n ```\n\n After the Cloud Run Admin API is enabled, the Compute Engine default service account is\n automatically created.\n\n### Required roles\n\nYou or your administrator must grant the deployer account and the\nCloud Build service account the following IAM roles. \n\n#### Click to view required roles for the deployer account\n\nTo get the permissions that you need to build and deploy from source,\nask your administrator to grant you the following IAM\nroles:\n\n- [Cloud Run Source Developer](/iam/docs/understanding-roles#run.sourceDeveloper) (`roles/run.sourceDeveloper`) on your project\n- [Service Usage Consumer](/iam/docs/understanding-roles#serviceusage.serviceUsageConsumer) (`roles/serviceusage.serviceUsageConsumer`) on your project\n- [Service Account User](/iam/docs/understanding-roles#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the Cloud Run service identity \n\n#### Click to view required roles for the Cloud Build service account\n\nCloud Build automatically uses the\n[Compute Engine default service\naccount](/build/docs/cloud-build-service-account) as the default Cloud Build service account to build your source code and\nCloud Run resource, unless you override this behavior. For\nCloud Build to build your sources, ask your administrator to grant\n[Cloud Run Builder](/iam/docs/understanding-roles#run.builder)\n(`roles/run.builder`) to the Compute Engine default\nservice account on your project: \n\n```bash\n gcloud projects add-iam-policy-binding PROJECT_ID \\\n --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \\\n --role=roles/run.builder\n \n```\n\nReplace \u003cvar translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e with your Google Cloud\nproject number, and \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your Google Cloud\nproject ID. For detailed instructions on how to find your project ID, and project number,\nsee [Creating\nand managing projects](/resource-manager/docs/creating-managing-projects#identifying_projects).\n\nGranting the Cloud Run builder role to the Compute Engine default service account\ntakes a couple of minutes to [propagate](/iam/docs/access-change-propagation).\n| **Note:**\n|\n|\n| The [`iam.automaticIamGrantsForDefaultServiceAccounts` organization policy constraint](/resource-manager/docs/organization-policy/restricting-service-accounts#disable_service_account_default_grants)\n| prevents the Editor role from being automatically granted to default service accounts. If you\n| created your organization after May 3, 2024, this constraint is enforced by\n| default.\n|\n|\n| We strongly recommend that you enforce this constraint to disable the automatic role grant. If you\n| disable the automatic role grant, you must decide which roles to grant to the default service\n| accounts, and then [grant these roles](/iam/docs/granting-changing-revoking-access)\n| yourself.\n|\n|\n| If the default service account already has the Editor role, we recommend that you replace the\n| Editor role with less permissive roles.To safely modify the service account's roles, use [Policy Simulator](/policy-intelligence/docs/simulate-iam-policies) to see the impact of\n| the change, and then [grant and revoke the\n| appropriate roles](/iam/docs/granting-changing-revoking-access).\n\nFor a list of IAM roles and permissions that are associated with\nCloud Run, see\n[Cloud Run IAM roles](/run/docs/reference/iam/roles)\nand [Cloud Run IAM permissions](/run/docs/reference/iam/permissions).\nIf your Cloud Run service interfaces with\nGoogle Cloud APIs, such as Cloud Client Libraries, see the\n[service identity configuration guide](/run/docs/configuring/services/service-identity).\nFor more information about granting roles, see\n[deployment permissions](/run/docs/reference/iam/roles#additional-configuration)\nand [manage access](/iam/docs/granting-changing-revoking-access).\n\nMaximum number of build environment variables and limits\n--------------------------------------------------------\n\nYou can define up to 100 build environment\nvariables, with a total key and value limit of 64 KiB.\n\nSet build environment variables\n-------------------------------\n\nYou can set build environment variables to establish new variables or completely\nreplace existing build variables.\n**Note:** Keys must start with an uppercase ASCII letter, and consist of uppercase ASCII letters, digits, and underscores. You should avoid creating variable keys with a `GOOGLE_*` prefix. [View all supported buildpacks environment variables](/docs/buildpacks/service-specific-configs). \n\n### gcloud\n\nTo set build environment variables when deploying a service from\nsource code, use the `--set-build-env-vars` flag: \n\n```bash\ngcloud run deploy SERVICE \\\n --source . \\\n --set-build-env-vars KEY1=VALUE1,KEY2=VALUE2\n```\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with name of your Cloud Run service.\n- \u003cvar translate=\"no\"\u003eKEY1=VALUE1,KEY2=VALUE2\u003c/var\u003e with the comma-separated list of variable names and their values that are deployed alongside a function that let you pass configuration information to buildpacks.\n\nIf you are deploying a function, add the `--function` flag with the function\nentry point from your source code.\n\nUpdate build environment variables\n----------------------------------\n\nYou can update build environment variables for existing services. This is a\nnon-destructive approach that changes or adds build environment variables, but\ndoes not delete the build environment variables. \n\n### gcloud\n\nTo update build environment variables for existing services, use the\n`--update-build-env-vars` flag: \n\n```bash\ngcloud run deploy SERVICE \\\n --source . \\\n --update-build-env-vars KEY1=VALUE1,KEY2=VALUE2\n```\n\nIf you are deploying a function, add the `--function` flag with the function\nentry point from your source code.\n\nDelete build environment variables\n----------------------------------\n\nYou can delete build environment variables for existing services. \n\n### gcloud\n\nTo remove build environment variables for existing services, use the\n`--remove-build-env-vars` flag: \n\n```bash\ngcloud run deploy SERVICE \\\n --source . \\\n --remove-build-env-vars KEY1=VALUE1,KEY2=VALUE2\n```\n\nAlternatively, you can clear build environment variables by using the\n`--clear-build-env-vars` for existing services: \n\n```bash\ngcloud run deploy SERVICE \\\n --source . \\\n --clear-build-env-vars KEY1=VALUE1,KEY2=VALUE2\n```\n\nIf you are deploying a function, add the `--function` flag with the function\nentry point from your source code.\n\nUse a build environment variables file\n--------------------------------------\n\nYou can use a build environment variables file for existing functions. \n\n### gcloud\n\nTo set build environment variables from a file, use the\n`--build-env-vars-file` flag: \n\n```bash\ngcloud run deploy SERVICE \\\n --source . \\\n --build-env-vars-file FILE_NAME.yaml\n```\n\nReplace \u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e.yaml where the contents of the file, which\nshould look as follows: \n\n KEY1: VALUE1\n KEY2: VALUE2\n\nIf you are deploying a function, add the `--function` flag with the function\nentry point from your source code."]]