Problem

If you build golang source code that uses any modules hosted in the github private repository with cloudbuild, cloudbuild will be failed in go build because it will not be able to get private repository’s module.
Sample cloudbuild.yaml:

Copy to Clipboard

When you execute this cloudbuild.yaml, you will get an following error.

Copy to Clipboard

How to resolve this problem

  1. Generate a key pair

  2. Add the publich key to private repository as a deploy key

  3. Generate encrypted private key using Cloud KMS : https://cloud.google.com/cloud-build/docs/access-private-github-repos

  4. Add following configuration to cloudbuild.yaml
    a) Decrypt encrypted private key
    b) Add github’ signature to ~/.ssh/known_hosts
    c) Add git config to use ssh access instead of https access

Sample cloudbuild.yaml:

Copy to Clipboard