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

How to authenticate with a Google Service Account in Jenkins pipeline

我想在 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' ....

我无法让 'Google Service Account from private key' 正常工作,但是在 Jenkins 中使用 'Secret File' 类型的凭据,并上传我的 google 服务帐户 JSON 可以。

您需要将您的服务帐户 JSON 文件上传为 机密文件 。 那么:

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}")
  }