Menetapkan variabel lingkungan untuk mengonfigurasi build

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

  1. Instal Docker Community Edition (CE) di workstation Anda. Docker digunakan oleh pack sebagai builder image OCI.
  2. Instal Pack CLI.
  3. 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_NAME dengan nama layanan untuk aplikasi atau fungsi Anda.
  • ENVIRONMENT_VARIABLE dengan 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

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. 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 role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Instal Google Cloud CLI.

  6. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  7. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  8. 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 role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. Instal Google Cloud CLI.

  12. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  13. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  14. Pastikan project Google Cloud Anda memiliki akses ke repositori image container.

    Untuk mengonfigurasi akses ke repositori Docker di Artifact Registry:

    1. Buat repositori Docker baru di lokasi yang sama dengan project Google Cloud Anda.
      gcloud artifacts repositories create REPO_NAME \
      --repository-format=docker \
      --location=REGION --description="DESCRIPTION"
      Ganti:
      • REPO_NAME dengan nama yang Anda pilih untuk repositori Docker.
      • REGION dengan lokasi di atau yang terdekat dengan lokasi project Google Cloud Anda.
      • DESCRIPTION dengan deskripsi pilihan Anda.

      Misalnya, untuk membuat repositori docker di us-west2 dengan deskripsi "repository Docker", jalankan:

      gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \
      --location=us-west2 --description="Docker repository"
    2. Pastikan repositori Anda telah dibuat:
      gcloud artifacts repositories list

      Anda akan melihat nama yang dipilih untuk repositori Docker Anda dalam daftar.

Opsional: Mendownload contoh aplikasi

  1. Clone repositori contoh ke komputer lokal Anda:
    git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
  2. 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

Membangun aplikasi dengan variabel lingkungan

  1. Dalam direktori root layanan Anda, buat atau update deskripsi project project.toml untuk menyertakan bagian [[build.env]] dan variabel lingkungan yang didukung:
    [[build.env]]
        name = "ENVIRONMENT_VARIABLE_NAME"
        value = "ENVIRONMENT_VARIABLE_VALUE"

    Ganti:

    • ENVIRONMENT_VARIABLE_NAME dengan nama variabel lingkungan yang didukung.
    • ENVIRONMENT_VARIABLE_VALUE dengan nilai yang sesuai untuk variabel lingkungan yang ditentukan.

    Contoh

    [[build.env]]
        name = "GOOGLE_ENTRYPOINT"
        value = "gunicorn -p :8080 main:app"
  2. Gunakan gcloud untuk mengirimkan kode sumber aplikasi ke Cloud Build:
    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

    Ganti:

    • LOCATION dengan nama region repositori container Anda. Contoh: us-west2-docker.pkg.dev
    • PROJECT_ID dengan ID project Google Cloud Anda.
    • REPO_NAME dengan nama repositori Docker Anda.
    • IMAGE_NAME dengan 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
  3. Pastikan bahwa sampel aplikasi berhasil dipublikasikan ke REPO_NAME:
    gcloud artifacts docker images list LOCATION-docker.pkg.dev/project-id/REPO_NAME

    Ganti:

    • LOCATION dengan nama region repositori container Anda. Contoh: us-west2-docker.pkg.dev
    • PROJECT_ID dengan ID project Google Cloud Anda.
    • REPO_NAME dengan nama repositori Docker Anda.