Men-deploy aplikasi dalam container ke Cloud Run menggunakan Cloud Build
Halaman ini menunjukkan cara menggunakan Cloud Build untuk men-deploy aplikasi dalam container ke Cloud Run.
Untuk mengikuti panduan langkah demi langkah tugas ini langsung di Cloud Shell Editor, klik Pandu saya:
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, Cloud Run, Artifact Registry, and Compute Engine 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, Cloud Run, Artifact Registry, and Compute Engine 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
- Buka jendela terminal. 
- Tetapkan variabel lingkungan untuk menyimpan project ID dan nomor project Anda: - PROJECT_ID=$(gcloud config list --format='value(core.project)') PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
- Berikan peran Cloud Run Admin ke akun layanan Cloud Build: - gcloud projects add-iam-policy-binding $PROJECT_ID \ --member=serviceAccount:$(gcloud projects describe $PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --role=roles/run.admin
- Berikan peran Storage Object User ke akun layanan Cloud Build: - gcloud projects add-iam-policy-binding $PROJECT_ID \ --member=serviceAccount:$(gcloud projects describe $PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --role="roles/storage.objectUser"
- Berikan peran IAM Service Account User ke akun layanan Cloud Build untuk akun layanan runtime Cloud Run: - gcloud iam service-accounts add-iam-policy-binding $(gcloud projects describe $PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --member=serviceAccount:$(gcloud projects describe $PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --role="roles/iam.serviceAccountUser" \ --project=$PROJECT_ID
- Buka jendela terminal (jika belum terbuka). 
- Buat direktori baru bernama - helloworlddan buka direktori tersebut:- mkdir helloworld cd helloworld
- Buat file bernama - cloudbuild.yamldengan konten berikut. File ini adalah file konfigurasi Cloud Build. File ini berisi petunjuk bagi Cloud Build untuk men-deploy image bernama- us-docker.pkg.dev/cloudrun/container/hellodi layanan Cloud Run bernama- cloudrunservice.
- Deploy image dengan menjalankan perintah berikut: - gcloud builds submit --region=us-west2 --config cloudbuild.yaml
- Buka halaman Cloud Run di konsol Google Cloud : 
- Pilih project Anda lalu klik Buka. - Anda akan melihat halaman Cloud Run Services. 
- Di tabel, temukan baris dengan nama cloudrunservice, lalu klik cloudrunservice. - Halaman Service details untuk cloudrunservice akan ditampilkan. 
- Untuk menjalankan image yang Anda deploy di cloudrunservice, klik URL:   
- Pelajari cara melakukan build menggunakan Cloud Build.
- Pelajari cara membuat file konfigurasi build dasar.
- Pelajari cara melihat hasil build.
- Pelajari cara lain untuk men-deploy ke Cloud Run.
- Pelajari cara men-deploy ke Google Kubernetes Engine.
- Pelajari lebih lanjut referensi DevOps.
- Eksplorasi program riset DevOps.
Memberikan izin
Cloud Build memerlukan izin Admin Cloud Run dan Pengguna Akun Layanan IAM sebelum dapat men-deploy image ke Cloud Run.
Men-deploy image bawaan
Anda dapat mengonfigurasi Cloud Build untuk men-deploy image yang telah dibuat sebelumnya dan disimpan di Artifact Registry ke Cloud Run.
Untuk men-deploy image yang telah dibuat sebelumnya:
Setelah build selesai, Anda akan melihat output yang mirip dengan berikut ini:
DONE
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ID                                    CREATE_TIME                DURATION  SOURCE                                                                                            IMAGES  STATUS
784653b2-f00e-4c4b-9f5f-96a5f115bef4  2020-01-23T14:53:13+00:00  23S       gs://cloudrunqs-project_cloudbuild/source/1579791193.217726-ea20e1c787fb4784b19fb1273d032df2.tgz  -       SUCCESS
Anda baru saja men-deploy image hello ke Cloud Run.