如何从Jenkins文件中调用Groovy脚本? [英] How can I call a groovy script from a Jenkins file?

查看:1137
本文介绍了如何从Jenkins文件中调用Groovy脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Jenkinsfile中的内容分离到一个普通的脚本中.但是它无法调用以下脚本: 这是代码:

I am trying to separate out the contents from a Jenkinsfile into a groovy script to make. But it fails to call these scripts: Here is the code:

#!/usr/bin/env groovy

node('test-node'){

  stage('Checkout') {
    echo "${BRANCH_NAME} ${env.BRANCH_NAME}"
    scm Checkout

  }

  stage('Build-all-targets-in-parallel'){

    def workspace = pwd()
    echo workspace
    parallel(
      'first-parallel-target' :
       {
         // Load the file 'file1.groovy' from the current directory, into a variable called "externalMethod".
         //callScriptOne()
         def externalMethod = load("file1.groovy")
         // Call the method we defined in file1.
          externalMethod.firstTest()
       },
       'second-parallel-target' :
      {
         //callScriptTwo()
         def externalMethod = load("file2.groovy")
         // Call the method we defined in file1.
         externalMethod.testTwo()
        }
    )
  }
  stage('Cleanup workspace'){
    deleteDir()
  }
}

file.groovy

file.groovy

#!groovy

def firstTest(){

  node('test-node'){
    
    stage('build'){
      echo "Second stage"
    }
    
    stage('Cleanup workspace'){
      deleteDir()
    }
  }
}

看起来Jenkinsfile能够调用file1.groovy,但总是给我一个错误:

Looks like the Jenkinsfile is able to call file1.groovy but always gives me an error:

java.lang.NullPointerException: Cannot invoke method firstTest() on null object

推荐答案

如果要从外部文件在Jenkinsfile中使用可用的方法,则需要执行以下操作

If you want to have methods available in your Jenkinsfile from an external file you need to do the following

在您的file1.groovy中,返回对方法的引用

In your file1.groovy, return references to the methods

def firstTest() {
    // stuff here
}

def testTwo() {
    //more stuff here
}
...

return [
    firstTest: this.&firstTest,
    testTwo: this.&testTwo
]

编辑

evaluate似乎不是必需的

def externalMethod = evaluate readFile("file1.groovy")

def externalMethod = evaluate readTrusted("file1.groovy")

正如@Olia提到的

def externalMethod = load("file1.groovy")

应该工作

此处是 readTrusted .请注意,不允许使用参数替换(进行轻量级签出)

Here is a reference on readTrusted. Note that no parameter substitution is allowed (does a lightweight checkout)

通过轻量级结帐:

如果选择,请尝试直接从SCM获取管道脚本内容,而不执行完整的检出.这种模式的优点是效率高.但是,您将不会获得基于SCM的任何更改日志或轮询. (如果在构建过程中使用checkout scm,则将填充变更日志并初始化轮询.)在这种模式下,构建参数也不会被替换为SCM配置.只有选定的SCM插件支持此模式.

If selected, try to obtain the Pipeline script contents directly from the SCM without performing a full checkout. The advantage of this mode is its efficiency; however, you will not get any changelogs or polling based on the SCM. (If you use checkout scm during the build, this will populate the changelog and initialize polling.) Also build parameters will not be substituted into SCM configuration in this mode. Only selected SCM plugins support this mode.

至少对我有用

这篇关于如何从Jenkins文件中调用Groovy脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆