如何在Jenkins管道中使用Google服务账号进行身份验证

18

我想在Jenkins管道中使用gcloud,因此我首先需要使用Google服务账户进行身份验证。我正在使用https://wiki.jenkins.io/display/JENKINS/Google+OAuth+Plugin插件来保存我的私钥凭据。但我无法将凭据加载到管道中:

withCredentials([[$class: 'MultiBinding', credentialsId: 'my-creds', variable: 'GCSKEY']]) {
    sh "gcloud auth activate-service-account --key-file=${GCSKEY}"
}

我也尝试了从文件中读取,但没有成功。

withCredentials([file(credentialsId:'my-creds', variable: 'GCSKEY')]) {

日志显示:

org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException: Credentials 'my-creds' is of type 'Google Service Account from private key' ....
2个回答

53

您需要将服务帐户 JSON 文件上传为secret file。 然后:

withCredentials([file(credentialsId: 'key-sa', variable: 'GC_KEY')]) {
    sh("gcloud auth activate-service-account --key-file=${GC_KEY}")
    sh("gcloud container clusters get-credentials prod --zone northamerica-northeast1-a --project ${project}")
  }

10
为什么每次我读到这篇文章都不能点赞?@stackoverflow请解决。 - Shailyn Ortiz
4
请更加明确和清晰地表达,应该将JSON文件上传为保密文件,而不是作为Google服务帐户。 - Sagi
这对我也有效,但我应该使用 '${GC_KEY}'。 - Grubhart
我得到了这个错误:ERROR: (gcloud.auth.activate-service-account) 刷新您当前的身份验证令牌时出现问题:('invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values in the JWT claim.', - Ares91
2个错误:期望'StringCredentials',期望'FileCredentials',使用相同的配置。 - Tiago Medici

4

我无法让“从私钥获取Google服务帐户”起作用,但是在Jenkins中使用“秘密文件”类型的凭据,并上传我的Google服务帐户JSON文件可以解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接