Membuat fungsi dengan buildpack

Panduan ini menunjukkan cara menggunakan buildpack dengan kode sumber fungsi Anda untuk membuat image container. Misalnya, gunakan buildpack untuk membuat fungsi Cloud Run yang ingin Anda deploy di Cloud Run.

Ada dua metode untuk membangun image container dengan buildpack:

  • Build secara lokal dengan CLI pack untuk menguji fungsi Anda secara lokal dan membuat prototipe perubahan dengan cepat sebelum deployment.
  • Membangun dari jarak jauh dengan Cloud Build. Mem-build dengan Cloud Build berguna untuk fungsi yang memiliki proses build yang memerlukan banyak resource dan juga dapat membantu melindungi supply chain software Anda.

Mengonfigurasi project Anda untuk membuat fungsi

Untuk membuat fungsi dengan buildpack:

  • Sertakan library Functions Framework.

  • Tetapkan variabel lingkungan GOOGLE_FUNCTION_TARGET ke nama fungsi yang Anda gunakan sebagai titik entri. Anda dapat melakukannya dengan menyertakan project.toml di folder yang sama dengan kode sumber Anda. File project.toml harus memiliki konfigurasi berikut:

[[build.env]]
    name = "GOOGLE_FUNCTION_TARGET"
    value =  "ENTRY_POINT"

Ganti ENTRY_POINT dengan metode fungsi.

Untuk mengetahui detail tentang cara menggunakan variabel lingkungan dengan Cloud Run Functions, lihat Mengonfigurasi layanan Cloud Run Functions.

Build lokal

Pack adalah alat CLI yang dikelola oleh project CNB untuk mendukung penggunaan buildpack. Gunakan CLI pack untuk mem-build fungsi secara lokal menjadi image container.

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.

Mem-build fungsi secara lokal

Anda menggunakan perintah pack build dan menentukan --builder=gcr.io/buildpacks/builder builder default untuk membuat image container secara lokal.

pack build --builder=gcr.io/buildpacks/builder IMAGE_NAME

Ganti IMAGE_NAME dengan nama image container Anda.

Anda juga dapat menyesuaikan image container dengan memperluas build dan menjalankan image.

Mem-build fungsi contoh secara lokal

Contoh berikut menunjukkan cara membuat sampel secara lokal.

  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-functions-framework-go

    Java

    cd buildpack-samples/sample-functions-framework-java-mvn

    Node.js

    cd buildpack-samples/sample-functions-framework-node

    Python

    cd buildpack-samples/sample-functions-framework-python

    Ruby

    cd buildpack-samples/sample-functions-framework-ruby
  3. Gunakan pack untuk membuat fungsi contoh:

    Go

    pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-go

    Java

    pack build --builder gcr.io/buildpacks/builder:v1 sample-functions-java-mvn

    Node.js

    pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-node

    Python

    pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-python

    Ruby

    pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-ruby
  4. Jalankan image menggunakan docker:

    Go

    docker run -p8080:8080 sample-functions-framework-go

    Java

    docker run -it -ePORT=8080 -p8080:8080 sample-functions-java-mvn

    Node.js

    docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-node

    Python

    docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-python

    Ruby

    docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-ruby
  5. Kunjungi fungsi yang sedang berjalan dengan mengakses localhost:8080.

Build jarak jauh

Gunakan Cloud Build untuk mem-build fungsi Anda menjadi image container, dan Artifact Registry sebagai repositori container untuk menyimpan dan men-deploy setiap image.

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.

Membangun fungsi dari jarak jauh

Anda menggunakan perintah gcloud builds submit untuk mem-build dan mengupload image container ke repositori.

Anda dapat memilih untuk menentukan image container dalam perintah itu sendiri atau menggunakan file konfigurasi.

Membangun dengan perintah

Untuk membangun tanpa file konfigurasi, tentukan flag image:

gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

Ganti:

  • LOCATION dengan nama region repositori container Anda, misalnya, us-west2
  • PROJECT_ID dengan ID project Google Cloud Anda.
  • REPO_NAME dengan nama repositori Docker Anda.
  • IMAGE_NAME dengan nama image container Anda.

Contoh:

gcloud builds submit --pack image=us-west2-docker.pkg.dev/my-project-id/my-buildpacks-docker-repo

Membangun dengan file konfigurasi

Anda dapat menggunakan file konfigurasi untuk menentukan detail konfigurasi repositori image. File konfigurasi menggunakan format file YAML dan harus menyertakan langkah build yang menggunakan CLI pack.

  1. Buat nama file YAML cloudbuild.yaml yang menyertakan URI repositori image container Anda.

     options:
       logging: CLOUD_LOGGING_ONLY
       pool: {}
     projectId: PROJECT_ID
     steps:
     - name: gcr.io/k8s-skaffold/pack
       entrypoint: pack
       args:
       - build
       - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
       - --builder
       - gcr.io/buildpacks/builder:latest
       - --network
       - cloudbuild
       - --publish
     - name: gcr.io/cloud-builders/docker
       entrypoint: pack
       args:
       - pull
       - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
     images:
     - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
    

    Ganti:

    • LOCATION dengan nama region repositori container Anda, misalnya, us-west2.
    • PROJECT_ID dengan ID project Google Cloud Anda.
    • REPO_NAME dengan nama repositori Docker Anda.
    • IMAGE_NAME dengan nama image container Anda.
  2. Bangun aplikasi.

    Jika file konfigurasi diberi nama cloudbuild.yaml, jalankan perintah berikut:

    gcloud builds submit .
    

Contoh: Mem-build fungsi contoh dari jarak jauh

Contoh berikut menunjukkan cara membangun sampel dari jarak jauh, dan memverifikasi bahwa image container telah dikirim ke repositori Anda di Artifact Registry.

  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-functions-framework-go

    Java

    cd buildpack-samples/sample-functions-framework-java-mvn

    Node.js

    cd buildpack-samples/sample-functions-framework-node

    Python

    cd buildpack-samples/sample-functions-framework-python

    Ruby

    cd buildpack-samples/sample-functions-framework-ruby
  3. Gunakan gcloud untuk mengirimkan kode sumber aplikasi ke Cloud Build:

    Go

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-go

    Java

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-gradle

    Node.js

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-node

    Python

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-python

    Ruby

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-ruby

    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.

  4. Pastikan fungsi contoh 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, misalnya, us-west2.
    • PROJECT_ID dengan ID project Google Cloud Anda.
    • REPO_NAME dengan nama repositori Docker Anda.

Langkah Berikutnya