Menghubungkan ke repositori Bitbucket Server

Halaman ini menjelaskan cara menghubungkan ke repositori Bitbucket Server ke Cloud Build.

Sebelum memulai

  • Enable the Cloud Build, Secret Manager, and Compute Engine APIs.

    Enable the APIs

Menghubungkan ke repositori Bitbucket Server

Konsol

Untuk menghubungkan repositori Bitbucket Server ke Cloud Build menggunakan Google Cloud Console:

  1. Buka halaman Repositories di Konsol Google Cloud.

    Buka halaman Repositori

  2. Di bagian atas halaman, pilih tab generasi ke-1.

  3. Klik Connect Repository.

  4. Pilih Bitbucket Server.

    Anda akan melihat panel Connect Repository. Selesaikan langkah-langkah berikut untuk menghubungkan repositori Bitbucket Server Anda:

    1. Region: Pilih region tempat koneksi Anda berada.

    2. Di bagian Select source code management provider, pilih Bitbucket Server.

    3. Host connection: Pilih nama koneksi host Anda dari menu drop-down.

    4. Klik Lanjutkan.

    5. Di bagian Select Repository, pilih repositori Bitbucket Server yang ingin dihubungkan ke Cloud Build.

    6. Setelah memilih akun dan repositori Bitbucket Server, baca pernyataan penyangkalan izin dan pilih kotak centang di sebelahnya untuk menunjukkan bahwa Anda menyetujui persyaratan yang ditampilkan.

    7. Klik Connect untuk menghubungkan repositori Anda.

    Saat repositori terhubung, Cloud Build akan mengonfigurasi webhook di repositori di instance Bitbucket Server Anda. Kemudian, repositori akan mengirimkan webhook untuk memanggil pemicu Bitbucket Server yang sesuai ketika Anda membuat perubahan pada repositori. Repositori juga dapat dihubungkan beberapa kali dengan beberapa koneksi host. Untuk mempelajari cara mengelola webhook, lihat Mengelola webhook.

    Untuk menemukan webhook, Anda memerlukan ID webhook. ID webhook untuk setiap repositori yang terhubung dapat ditemukan di BitbucketServerConfig.

    Masukkan perintah berikut untuk mengambil ID webhook:

      gcloud alpha builds enterprise-config bitbucketserver describe CONFIG_NAME  --region=REGION
    

    Dengan keterangan:

    • CONFIG_NAME adalah nama konfigurasi Server Bitbucket
    • REGION adalah region koneksi host Bitbucket Server
  5. Klik Done. Secara opsional, klik Create a trigger untuk membuat pemicu.

API

Untuk menghubungkan repositori Bitbucket Server ke Cloud Build menggunakan API, selesaikan langkah-langkah berikut:

  1. Gunakan template JSON berikut untuk menghubungkan repositori Anda:

    {
      "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME",
      "requests": {
        "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME",
       "bitbucketServerConnectedRepository": {
          "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME",
          "repo": {
            "projectKey": "PROJECT_KEY",
            "repoSlug": "REPO_SLUG"
           }
         }
      }
    }
    

    Dengan keterangan:

    • PROJECT_NUMBER adalah nomor project dari project Cloud Anda.
    • REGION adalah region yang terkait dengan konfigurasi Bitbucket Server Anda.
    • BITBUCKET_SERVER_CONFIG_NAME adalah nama konfigurasi Server Bitbucket Anda.
    • PROJECT_KEY adalah kunci project Bitbucket Server Anda. Jika ingin menghubungkan repositori pribadi, kunci project Anda harus berisi simbol tilde (~) sebelum nama pengguna. Contohnya, ~${USERNAME}. URL lengkap untuk repositori host mungkin terlihat seperti https://${HOST_URI}/projects/~${USER_NAME}/repos/${REPO_SLUG}.
    • REPO_SLUG adalah slug dari repositori Bitbucket Server Anda.
  2. Masukkan perintah curl berikut di terminal Anda:

    curl -X POST -H "Authorization: Bearer "$(gcloud auth print-access-token) -H "Content-Type: application/json; charset=utf-8" -H "x-goog-user-project: PROJECT_NUMBER" https://cloudbuild.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME/connectedRepositories:batchCreate -d @requests.json
    

    Dengan keterangan:

    • PROJECT_NUMBER adalah nomor project Cloud Anda.
    • PROJECT_ID adalah project ID Cloud Anda.
    • REGION adalah region yang terkait dengan konfigurasi Bitbucket Server Anda.
    • BITBUCKET_SERVER_CONFIG_NAME adalah nama konfigurasi Server Bitbucket Anda.

    Jika berhasil, isi respons memuat instance Operation yang baru dibuat.

  3. Masukkan perintah curl berikut di terminal Anda:

    curl -X GET -H "Authorization: Bearer "$(gcloud auth print-access-token) -H "Content-Type: application/json; charset=utf-8"  -H "x-goog-user-project: PROJECT_NUMBER" https://cloudbuild.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID
    

    Dengan keterangan:

    • PROJECT_NUMBER adalah nomor project Cloud Anda.
    • PROJECT_ID adalah project ID Cloud Anda.
    • REGION adalah region yang terkait dengan konfigurasi Bitbucket Server Anda.
    • OPERATION_ID adalah ID operasi pembuatan konfigurasi Bitbucket Server Anda. Anda dapat menemukan ID Operasi di kolom name dalam respons Anda. Format kolom name dalam respons Anda terlihat seperti berikut: projects/project-id/locations/region/operations/operation-id.

    Anda mungkin perlu terus menjalankan perintah GetOperation API hingga respons berisi done: true, yang menunjukkan operasi selesai. Jika repositori Bitbucket Server berhasil terhubung, Anda dapat melihat repositori yang terhubung di kolom response.bitbucketServerConnectedRepositories. Jika tidak, lihat kolom error untuk laporan error mendetail.

Langkah selanjutnya