在jenkins的管道中运行gcloud脚本时出错

时间:2019-04-10 14:02:34

标签: jenkins kubernetes jenkins-plugins google-kubernetes-engine

当我想在安装Gcloud插件出现日志消息后使用GCloud命令时,使用Google云平台上的市场点击部署在Kubernetes中创建Jenkins应用程序。

这是管道

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

        }
      }
    }

  }
}

日志

 > 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

我尝试搜索Google SDK的文件夹安装,但是找不到任何文件夹,我不知道这是否是Deploy问题。 我希望只是将Dockerfile上传到源存储库,但是如果我在Gcloud中使用脚本显示此日志,则安装了Google auth,SDK,Kubernetes的所有插件,并在Jenkins Config中配置Kubernetes,我从Google导出了密钥云。

1 个答案:

答案 0 :(得分:0)

从管道输出中,我可以看到,所有阶段都在Jenkins Master节点上运行(默认情况下'gcloud'不可用),这与Kubernetes集群上动态创建的jenkins-slave Pod所期望的不同。

要快速解决此问题,只需在Jenkins管道代码中显式配置您的Pod模板,这是一个包含“ gcloud”容器的Pod模板示例:

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

  }
}

作业输出:

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

请验证您是否已正确配置Jenkins Kubernetes插件,尤其是与here中所述的Kubernetes Pod模板相关的部分配置。

相关问题