How to authenticate with a Google Service Account in Jenkins pipeline

给你一囗甜甜゛ 提交于 2019-12-05 23:16:06

问题


I want to use gcloud in Jenkins pipeline and therefore I have to authenticate first with the Google Service account. I'm using the https://wiki.jenkins.io/display/JENKINS/Google+OAuth+Plugin which holds my Private Key Credentials. I'm stuck with loading the credentials into the pipeline:

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

I also tried it with from file, but without luck.

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

The log says:

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

回答1:


You need to upload your Sevice Account JSON file as a secret file. Then:

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



回答2:


I couldn't get the 'Google Service Account from private key' working, but using the 'Secret File' type of credential in Jenkins, and uploaded my google service account JSON works.




回答3:


The attached image is self-explanatory! Let me know if you don't understand it! Btw, you've to install the above mentioned plugin (which i assume You have done) and navigate to Jenkins>Configure Global Security and you can find these options there.



来源:https://stackoverflow.com/questions/45355007/how-to-authenticate-with-a-google-service-account-in-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!