Anda dapat mengonfigurasi variabel lingkungan yang ditetapkan selama proses build image container.
Anda juga dapat menyesuaikan image container dengan memperluas image build dan pengoperasian.
Build lokal
Untuk build lokal, Anda harus menginstal Pack CLI dan Docker.
Sebelum memulai
- Instal Docker Community Edition (CE)
di workstation Anda. Docker digunakan oleh
packsebagai builder image OCI. - Instal Pack CLI.
- Instal alat Kontrol sumber Git untuk mengambil aplikasi contoh dari GitHub.
Menetapkan variabel lingkungan
Untuk menetapkan variabel lingkungan untuk build lokal, tambahkan flag --env ke
perintah pack untuk setiap variabel lingkungan.
Anda dapat menggunakan satu atau beberapa variabel lingkungan yang didukung oleh runtime.
pack build SERVICE_NAME \
--env ENVIRONMENT_VARIABLE
Ganti:
SERVICE_NAMEdengan nama layanan untuk aplikasi atau fungsi Anda.ENVIRONMENT_VARIABLEdengan variabel lingkungan yang ingin Anda tetapkan selama waktu build.
- Contoh
Untuk menetapkan variabel lingkungan
GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app"di image container, jalankan perintah berikut:pack build my-app \ --builder gcr.io/buildpacks/builder:v1 \ --env GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app" --env MY-LOCAL-ENV-VARIABLE
Build jarak jauh
Untuk menetapkan variabel lingkungan untuk build jarak jauh, gunakan
deskripsi project project.toml.
Deskripsi project project.toml digunakan oleh Cloud Build saat
image container Anda dibangun.
Sebelum memulai
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build and Artifact Registry APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build and Artifact Registry APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init - Pastikan Google Cloud project Anda memiliki akses ke repositori image container.
Untuk mengonfigurasi akses ke repositori Docker di Artifact Registry:
- Buat repositori Docker baru di lokasi yang sama dengan project Google Cloud Anda.
Ganti:gcloud artifacts repositories create REPO_NAME \ --repository-format=docker \ --location=REGION --description="DESCRIPTION"
REPO_NAMEdengan nama yang Anda pilih untuk repositori Docker.REGIONdengan lokasi di atau yang terdekat dengan lokasi project Google Cloud Anda.DESCRIPTIONdengan deskripsi pilihan Anda.
Misalnya, untuk membuat repositori
dockerdius-west2dengan deskripsi "repository Docker", jalankan:gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \ --location=us-west2 --description="Docker repository"
- Pastikan repositori Anda telah dibuat:
gcloud artifacts repositories list
Anda akan melihat nama yang dipilih untuk repositori Docker Anda dalam daftar.
- Buat repositori Docker baru di lokasi yang sama dengan project Google Cloud Anda.
- Clone repositori contoh ke komputer lokal Anda:
git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
- Ubah ke direktori yang berisi kode contoh aplikasi:
Go
cd buildpack-samples/sample-go
Java
cd buildpack-samples/sample-java-gradle
Node.js
cd buildpack-samples/sample-node
PHP
cd buildpack-samples/sample-php
Python
cd buildpack-samples/sample-python
Ruby
cd buildpack-samples/sample-ruby
.NET
cd buildpack-samples/sample-dotnet
- Dalam direktori root layanan Anda, buat atau update
deskripsi project
project.tomluntuk menyertakan bagian[[build.env]]dan variabel lingkungan yang didukung:[[build.env]] name = "ENVIRONMENT_VARIABLE_NAME" value = "ENVIRONMENT_VARIABLE_VALUE"
Ganti:
ENVIRONMENT_VARIABLE_NAMEdengan nama variabel lingkungan yang didukung.ENVIRONMENT_VARIABLE_VALUEdengan nilai yang sesuai untuk variabel lingkungan yang ditentukan.
Contoh
[[build.env]] name = "GOOGLE_ENTRYPOINT" value = "gunicorn -p :8080 main:app"
- Gunakan
gclouduntuk mengirimkan kode sumber aplikasi ke Cloud Build:gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
Ganti:
LOCATIONdengan nama region repositori container Anda. Contoh:us-west2-docker.pkg.devPROJECT_IDdengan ID project Google Cloud Anda.REPO_NAMEdengan nama repositori Docker Anda.IMAGE_NAMEdengan nama image container Anda.
Contoh: Jika mendownload aplikasi contoh, Anda menjalankan perintah yang sesuai:
Go
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-go
Java
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-java-gradle
Node.js
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-node
PHP
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-php
Python
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-python
Ruby
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-ruby
.NET
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-dotnet
-
Pastikan bahwa sampel aplikasi berhasil dipublikasikan ke
REPO_NAME:gcloud artifacts docker images list LOCATION-docker.pkg.dev/project-id/REPO_NAME
Ganti:
LOCATIONdengan nama region repositori container Anda. Contoh:us-west2-docker.pkg.devPROJECT_IDdengan ID project Google Cloud Anda.REPO_NAMEdengan nama repositori Docker Anda.