我应该将函数放在管道文件中的哪个位置?

时间:2017-08-10 09:21:34

标签: jenkins groovy jenkins-pipeline

我正在使用Jenkins共享库。

我的计划是能够将每个Jenkins文件中的行数减少到最小值。

我希望能够做到这样的事情:

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函数定义,因为无论我放在哪里,都会导致构建失败。

知道我做错了吗?

0 个答案:

没有答案