2017-08-10 74 views
1

我正在使用Jenkins共享库。我应该在哪里将函数放入管道文件中?

我的计划是能够将每个Jenkinsfile中的行数降至最低。

我希望能够做一些事情,如:

buildProduct(),它会发出它驻留在共享库管道建设。

我standardPipeline.groovy文件看起来像这样:

import com.company.utils.pipelineFunctions; 
import com.company.utils.Git; 

def ris = new pipelineFunctions() 

def run_in_stage(String stage_name, Closure command){ 
    ris.run_in_stage(stage_name, command, emailadd) 
} 

def call(body) { 

     def config = [:] 
     body.resolveStrategy = Closure.DELEGATE_FIRST 
     body.delegate = config 
     body() 

     node { 
      // Clean workspace before doing anything 
      deleteDir() 


      try { 
       def ris = new pipelineFunctions() 
       //run_in_stage('Clone', { 
       run_in_stage('Clone', { 
        checkout scm 
       }) 

       stage ('Build') { 
        sh "echo 'building ${config.projectName} ...'" 
       } 
       stage ('Tests') { 
        parallel 'static': { 
         sh "echo 'shell scripts to run static tests...'" 
        }, 
        'unit': { 
         sh "echo 'shell scripts to run unit tests...'" 
        }, 
        'integration': { 
         sh "echo 'shell scripts to run integration tests...'" 
        } 
       } 
       stage ('Deploy') { 
        sh "echo 'deploying to server ${config.serverDomain}...'" 
        sh "echo Itai ganot" 
       } 
      } catch (err) { 
       currentBuild.result = 'FAILED' 
       throw err 
      } 
     } 
    } 

的pipelineFunctions.groovy文件看起来像这样:

package com.company.utils; 
import com.company.utils.Git; 

def run_in_stage(String stage_name, Closure command, String sendTo){ 

    def gitTool = new Git() 

    String ulink = gitTool.getCommitter() 
    String jlink = "(<${env.BUILD_URL}|Open>)" 

    println "============================================================" 
    stage (stage_name) { 
     try { 
      command() 
      if (currentBuild.result == 'FAILURE') { 
       error "Build failed, see log for further details." 
      } 
      println "============================================================" 
     } catch (Exception ex) { 
      def except = "${ex}" 
      String emailadd = ulink+'@company.com' 
      if (currentBuild.result == null) { 
      currentBuild.result = "FAILURE" } 
         this.notifyStatus(stage_name, currentBuild.result, except) 
      echo "Pipeline failed at stage: ${stage_name}" 
      throw ex 
     } 
    } 
} 

return this; 

当我运行失败,出现以下错误编译:

groovy.lang.MissingPropertyException: No such property: ris for class: groovy.lang.Binding 

我想了解我应该在哪里放置run_in_stage函数def因为我把它放在哪里,这会导致构建失败。

任何想法我做错了什么?

回答

0

standardPipeline.groovy中,您需要在run_in_stage方法内声明ris以便能够在那里使用它。例如: -

import com.company.utils.pipelineFunctions; 
import com.company.utils.Git; 

// This instance may no longer be necessary, not sure if it's used outside run_in_stage 
def ris = new pipelineFunctions() 

def run_in_stage(String stage_name, Closure command){ 
    def ris = new pipelineFunctions() 
    String emailadd = '[email protected]' // edit: for 3rd param 
    ris.run_in_stage(stage_name, command, emailadd) 
} 

编辑:我跑这在注释未指定的错误的报告后,我已经更新了上面的什么我需要添加一个例子。

以上的工作对我罚款(除去一些其他的东西,这是不可用或不相关)后,从下面的管道脚本调用时:

// just loading above shared library explicitly 
@Library('jenkins-pipeline-library-test') _ 

StandardPipeline { 
    println 'something' 
} 

编辑2:这里有什么共同的例子如果对每个文件的放置位置有任何疑问,应该使用库项目结构:https://github.com/grdryn/jenkins-pipeline-library-test

+0

我仍然在run_in_stage函数内的“def ris = new pipelineFunctions()”中收到错误 –

+0

@ItaiGanot我已经重新创建了上述和RU它。我得到的唯一错误是非常明确的:'groovy.lang.MissingPropertyException:没有这样的属性:emailadd类:StandardPipeline'。我已经添加了上述修正(只是在范围内声明了一个名为'emailadd'的东西)。这是你遇到的错误还是其他的东西? –

相关问题