Problem statement:
How to import private repos in GCP cloud functions ?
Recently bumped into this scenario where we need to use private repos in cloud function and it’s import is failing
We are using
- Go 1.16
- cloud trigger
- cloud build
Solution
go mod vendor
We need to use vendor command to specify dependency
documentation here https://pkg.go.dev/cmd/go#hdr-Vendor_Directories
Detail
step 1 : Lets be explicit
Assumption that your GOPRIVATE is set properly & cloudbuild.yml is able to checkout repos
step 2:
Add a steps in cloudbuild.yml to import dependency via vendor
steps:
- id: setup go vendor
name: golang:1.16
entrypoint: /bin/bash
args:
- -c
- |
go mod vendor secretEnv: [ 'GIT_TOKEN' ]
step 3:
We need to use vendor for dependency and ignore go.mod, for that we need to add file .gcloudignore to project source directory
step 4:
Deploy your cloud function having private repos and we should see successful deployment
Reference:
https://cloud.google.com/functions/docs/writing/specifying-dependencies-go