Error running gcloud scripts in a pipeline in jenkins

4/10/2019

I create an application of Jenkins in Kubernetes using the marketplace click-to-deploy from the Google cloud platform when I want to use the GCloud command after installing the Gcloud plugin appears the Log message:

This is the pipeline.

def project = 'my-project'
def appName = 'my_app-name'
def zone = 'us-east1-d'
def feSvcName = "${appName}"
def imageTag = "gcr.io/${project}/${appName}:${env.BRANCH_NAME}.${env.BUILD_NUMBER}"

pipeline {
  agent  any
    stages {
      stage('Test') {
        steps {  
          sh " echo Test is not avalaible"
        }
      }
    stage('Build and push image with Container Builder') {
      steps {
        container('gcloud') {
          sh "cd .."
          sh "PYTHONUNBUFFERED=1 gcloud builds submit -t ${imageTag} ."
        }
      }
    }
    stage('Deploy Development') {
      // Canary branch
      when { branch 'develop' }
      steps {
        container('kubectl') {
          sh ("echo BRANCH develop is not avalaible")

        }
      }
    }
    stage('Deploy Test') {
      // Canary branch
      when { branch 'develop' }
      steps {
        container('kubectl') {
          sh ("echo BRANCH develop is not avalaible")

        }
      }
    }
    stage('Deploy Production') {
      // Production branchh
      when { branch 'master' }
      steps {
        container('kubectl') {
          sh ("echo BRANCH Master is not avalaible")

        }
      }
    }

  }
}

LOG

 > git rev-parse --is-inside-work-tree # timeout=10
Setting origin to https://source.developers.google.com/p/test-jalfonso/r/hello-app
 > git config remote.origin.url https://source.developers.google.com/p/test-jalfonso/r/hello-app # timeout=10
Fetching origin...
Fetching upstream changes from origin
 > git --version # timeout=10
 > git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials test-jalfonso
 > git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen branch in repository origin/test
Seen 2 remote branches
Obtained Jenkinsfile from 3e1b0fd042813f25f4761bec13a50646d7a3fccf
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-jalfonso_test@2
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential source:test-jalfonso
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository https://source.developers.google.com/p/test-jalfonso/r/hello-app
 > git init /var/jenkins_home/workspace/test-jalfonso_test@2 # timeout=10
Fetching upstream changes from https://source.developers.google.com/p/test-jalfonso/r/hello-app
 > git --version # timeout=10
using GIT_ASKPASS to set credentials test-jalfonso
 > git fetch --no-tags --progress https://source.developers.google.com/p/test-jalfonso/r/hello-app +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url https://source.developers.google.com/p/test-jalfonso/r/hello-app # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://source.developers.google.com/p/test-jalfonso/r/hello-app # timeout=10
Fetching without tags
Fetching upstream changes from https://source.developers.google.com/p/test-jalfonso/r/hello-app
using GIT_ASKPASS to set credentials test-jalfonso
 > git fetch --no-tags --progress https://source.developers.google.com/p/test-jalfonso/r/hello-app +refs/heads/*:refs/remotes/origin/*
Checking out Revision 3e1b0fd042813f25f4761bec13a50646d7a3fccf (test)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 3e1b0fd042813f25f4761bec13a50646d7a3fccf
Commit message: "test"
> git rev-list --no-walk f0d84e4bb46eff9acd4a79f93013f8eca20370c9 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ echo Test is not avalaible
Test is not avalaible
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build and push image with Container Builder)
[Pipeline] sh
+ echo Not available
Not available
[Pipeline] sh
+ gcloud builds submit -t gcr.io/my-project/my-app-name:master.23 .
/var/jenkins_home/workspace/test_master@tmp/durable-1d490c65/script.sh: 1: /var/jenkins_home/workspace/test_master@tmp/durable-1d490c65/script.sh: gcloud: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy Development)
Stage "Deploy Development" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy Test)
Stage "Deploy Test" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy Production)
Stage "Deploy Production" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

I tried to search the folder installation of the SDK of Google but I can't found any, I don't know if is an issue of the Deploy. I expected just upload my Dockerfile to the source repository but if I use a script with Gcloud appears this log, I installed all the plugins for Google auth, SDK, Kubernetes and I configure the Kubernetes in the Jenkins Config, I export the keys from Google Cloud.

-- Mr Halcolo
google-kubernetes-engine
jenkins
jenkins-plugins
kubernetes

1 Answer

4/15/2019

From your pipeline output I can see, that all your stages are running on Jenkins Master node (where 'gcloud' is not available by default), not like you would expect on dynamically created jenkins-slave Pod, on Kubernetes cluster.

To fix the problem quickly, just configure your Pod template explicitly in Jenkins pipeline code, here is an example of Pod template including 'gcloud' container:

def label = "gcloud-command-${UUID.randomUUID().toString()}"

podTemplate(label: label, yaml: """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: gcloud
    image: gcr.io/cloud-builders/gcloud
    command:
    - cat
    tty: true
"""
  ) {

  node(label) {
    stage('Test -  Execution of gcloud command') {
      container('gcloud') {
        sh "gcloud compute zones --help"
      }
    }

  }
}

Job output:

Running on jenkins-slave-33v1t-04zwp in /home/jenkins/workspace/run-jenkins-slave-on-k8s
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test -  Execution of gcloud command) (Test -  Execution of gcloud command)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ gcloud compute zones --help
NAME
    gcloud compute zones - list Google Compute Engine zones

SYNOPSIS
    gcloud compute zones COMMAND [GCLOUD_WIDE_FLAG ...]

DESCRIPTION
    List Google Compute Engine zones.

GCLOUD WIDE FLAGS
    These flags are available to all commands: --account, --configuration,
    --flags-file, --flatten, --format, --help, --impersonate-service-account,
    --log-http, --project, --quiet, --trace-token, --user-output-enabled,
    --verbosity. Run $ gcloud help for details.

COMMANDS
    COMMAND is one of the following:

     describe
        Describe a Google Compute Engine zone.

     list
        List Google Compute Engine zones.

NOTES
    These variants are also available:

        $ gcloud alpha compute zones
        $ gcloud beta compute zones

[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

Please verify if you have properly configured Jenkins Kubernetes Plugin, especially part of configuration related to Kubernetes Pod Template as described here.

-- Nepomucen
Source: StackOverflow