Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing gcloud command in Jenkins pipeline

I'm trying to run the gcloud command in a Jenkins declarative pipeline just like in the following example:

pipeline {
    agent any

   stages {
      stage('Run gcloud version') {

         steps {
            sh 'gcloud --version'           
         }
      }
   }
}

I downloaded the "GCloud SDK Plugin" and configured it like this (in "Global Tool Configuration" for Jenkins):

enter image description here

but when I try to build the pipeline using the above Jenkinsfile, I'm getting a 'gcloud: not found' error in the pipeline.

like image 262
Carlos Gomez Avatar asked Apr 26 '26 12:04

Carlos Gomez


2 Answers

I was able to run the command using the following Jenkinsfile:

pipeline {
   agent any

stages {
    stage('Run gcloud') {

        steps {
            withEnv(['GCLOUD_PATH=/var/jenkins_home/google-cloud-sdk/bin']) {
                sh '$GCLOUD_PATH/gcloud --version'
            }


         }
      }
   }
}

Note: I'm running Jenkins in kubernetes, so first I had to install the gcloud sdk in the Jenkins pod

like image 182
Carlos Gomez Avatar answered Apr 28 '26 06:04

Carlos Gomez


I am running Jenkins 2.176.2 in containers and the GCloud plugin was not able to install the SDK in the slave (agents) containers. I used the docker file to install it when deploying the agents:

RUN echo "deb http://packages.cloud.google.com/apt cloud-sdk-stretch main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list \
    && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - \
    && apt-get update -y && apt-get install google-cloud-sdk -y \
    && PATH=$PATH:/root/google-cloud-sdk/bin
like image 20
TudorIftimie Avatar answered Apr 28 '26 08:04

TudorIftimie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!