Mauricio
Mauricio

Reputation: 3129

How to clone a (second gen) 2nd Gen Google Cloud Repository from Cloud Build?

I need to write a bash script to run inside my Google Cloud Build's build process. This bash script needs to clone a 2nd Gen Repository (since now former Source Repositories is deprecated), but I can't find it anywhere how to perform this. Does gen2 Repository works with gcloud source repos clone?

If I run gcloud source repos list --project <my-project> it only lists legacy repositories.

Running gcloud source repos clone my-repo --project=my-project also doesn't work despite my-repo being a properly connected repository from Github.

Upvotes: 0

Views: 106

Answers (2)

Mauricio
Mauricio

Reputation: 3129

I achieved my goal of cloning the repository directly from Github using a Github's Oauth Access Token generated by the Google Cloud Build when creating the Cloud Build's Host Connection.

So when you create a Host Connection on Cloud Build, it generates a secret at Google Cloud Secret Manager, which store the Github Access Token.

So what I did is that I used this token to clone my repository. The exact steps were:

  1. Create a Host Connection on Cloud Build (if none exists alrady)
  2. Check at Google Cloud Secret Manager that a secret was created with the name <host-connection-name>-github-oauthtoken-<random-id>
  3. Grant your Google Cloud Build's Service Account (<project-number>@cloudbuild.gserviceaccount.com), access to Google Cloud Secret Manager.
  4. Then, clone the Github repository:
GITHUB_ACCESS_TOKEN=$(gcloud secrets versions access latest --secret="<host-connection-name>-github-oauthtoken-<random-id>")

git clone https://mau21mau:${GITHUB_ACCESS_TOKEN}@github.com/Sheetgo/sheetgo-env.git || { echo "Failed to clone repository"; exit 1; }

Upvotes: 0

McMaco
McMaco

Reputation: 324

I agree with @guillaume blaquiere, Cloud Source repository 2nd gen does not exist.

This is the documentation with regards to Cloud Build repositories (2nd gen).

With Cloud Build repositories (2nd gen), you can create and manage repository connections programmatically. You can set up a single connection for a repository and use Secret Manager secrets from that connection to programmatically set up additional connections across regions and projects. You can also set up connections using Terraform, in addition to the Google Cloud console, gcloud command-line tool, and the API. You must create a host connection prior to linking repositories when using Cloud Build repositories (2nd gen).

Cloud Build repositories (2nd gen) can be used with the following providers:

You can invoke builds on commits and pull requests. You can also invoke builds manually, on a Pub/Sub topic, or on an incoming webhook event.

Upvotes: 0

Related Questions