Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate with a Google Service Account in Jenkins pipeline

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' ....
like image 976
stefanbuck Avatar asked Jul 27 '17 15:07

stefanbuck


2 Answers

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}")
  }
like image 64
Julien Deruere Avatar answered Oct 25 '22 20:10

Julien Deruere


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.

like image 40
stefanbuck Avatar answered Oct 25 '22 19:10

stefanbuck