2017-08-10 102 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'的東西)。這是你遇到的錯誤還是其他的東西? –