Git Builder在CloudBuild中没有使用Github进行身份验证



我正在使用一个为Microservices Monorepo项目设计的CloudBuild文件。

代码运行一个bash脚本,使用Git构建器来确定Monorepo中的哪些目录已经提交给它们。这将输出一个文本文件,其中包含在最后一次提交中进行更改的每个目录的名称,以便在稍后的管道步骤中使用。下面是代码的摘录:

steps:
- name: 'gcr.io/cloud-builders/git'
entrypoint: /bin/bash
args: 
- -c
- |
git fetch --depth=2 origin main
echo hello
git  log
git --no-pager diff --name-only HEAD^ HEAD | grep "/" | cut -d/ -f1 | sort | uniq > /workspace/diff.txt

在私有存储库上运行此代码时,在git fetch --depth=2 origin-main行上给出以下错误:

fatal: could not read Username for 'https://github.com': No such device or address

CloudBuild代理在前面的步骤中成功地克隆了repo,我可以在构建日志中确认这一点。我认为这是Git构建器没有被赋予与主CloudBuild代理相同的OAuth令牌的问题。我尝试使用具有不同权限级别的不同CloudBuilder服务帐户,但没有成功。如有任何建议,我将不胜感激。

资源

以上摘自以下文章和存储库:

  • https://medium.com/google cloud/google cloudbuild -构建特定-微-服务- - - monorepo - 63 a7f961ac63
  • https://github.com/ajiteshk/MicroServicesCI-In-MonoRepo

存在一个与此错误相关的问题,在编写此错误时没有解决方案:

  • https://github.com/ajiteshk/MicroServicesCI-In-MonoRepo/issues/1

从历史上看,Cloud Build用于克隆存储库的凭据还没有提供给用户用于cloudbuild.yaml步骤。

你有两个选择:

  • 设置SSH密钥以访问私有存储库
  • 使用第二代存储库API获取Cloud Build使用的凭据并使用这些凭据。git真的应该通过Cloud Build来设置这个,但它现在没有这样设置。您仍然可以使用以下命令获取凭证:curl -s -H "Content-Type: application/json" -H "Authorization: Bearer $(gcloud auth print-access-token)" https://cloudbuild.googleapis.com/v2/projects/PROJECT-ID/locations/REGION/connections/CONNECTION/repositories/REPOSITORY:accessReadToken.