简体   繁体   English

如何从Jenkins文件中调用groovy脚本?

[英]How can I call a groovy script from a Jenkins file?

I am trying to separate out the contents from a Jenkinsfile into a groovy script to make. 我试图将Jenkins文件中的内容分成一个groovy脚本来制作。 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()
    }
  }
}

Looks like the Jenkinsfile is able to call file1.groovy but always gives me an error: 看起来Jenkinsfile能够调用file1.groovy但总是给我一个错误:

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

It looks like you missed return within the scripts you are loading: 看起来您在正在加载的脚本中错过了返回:

return this

Please, check it here: https://jenkins.io/doc/pipeline/steps/workflow-cps/#load-evaluate-a-groovy-source-file-into-the-pipeline-script 请在此处查看: https//jenkins.io/doc/pipeline/steps/workflow-cps/#load-evaluate-a-groovy-source-file-into-the-pipeline-script

So, your called loaded file will have a structure like: 因此,您调用的已加载文件将具有以下结构:

def exampleMethod() {
    //do something
}

def otherExampleMethod() {
    //do something else
}
return this

This way you shouldn't get null object 这样你就不应该得到null对象

If you want to have methods available in your Jenkinsfile from an external file you need to do the following 如果要从外部文件中获取Jenkinsfile可用的方法,则需要执行以下操作

In your file1.groovy , return references to the methods file1.groovy ,返回对方法的引用

def firstTest() {
    // stuff here
}

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

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

EDIT 编辑

evaluate does not seem to be required 似乎没有要求evaluate

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

or 要么

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

And as mentioned by @Olia 正如@Olia所提到的那样

def externalMethod = load("file1.groovy")

should work 应该管用

Here is a reference on readTrusted . 这是readTrusted的参考。 Note that no parameter substitution is allowed (does a lightweight checkout) 请注意,不允许参数替换(轻量级结账)

From lightweight checkout: 从轻量级结账:

If selected, try to obtain the Pipeline script contents directly from the SCM without performing a full checkout. 如果选中,请尝试直接从SCM获取管道脚本内容,而不执行完整检出。 The advantage of this mode is its efficiency; 这种模式的优点是效率高; however, you will not get any changelogs or polling based on the SCM. 但是,您不会根据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. (如果在构建期间使用checkout scm,则会填充更改日志并初始化轮询。)此模式下,构建参数也不会替换为SCM配置。 Only selected SCM plugins support this mode. 只有选定的SCM插件才支持此模式。

At least that works for me 至少这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Jenkins 秘密文件传递给 Groovy 脚本? - How can I pass Jenkins Secret File to the Groovy Script? 在Jenkins Groovy脚本中,如何从批处理文件中获取返回值 - In Jenkins Groovy script, how can I grab a return value from a batch file 如何从groovy映射脚本调用参数到Jenkins Pipeline - how to call parameters from groovy map script to Jenkins Pipeline 如何从“执行系统Groovy脚本”中调用位于jenkins从属服务器中的批处理/常规脚本? - How to call batch/groovy script located in jenkins slave from “Execute system Groovy script”? Jenkins:我可以从现有Job生成Groovy脚本吗? - Jenkins: Can I Generate groovy script from existing Job? 如何在groovy脚本中调用Jenkins参数 - How to call Jenkins parameter inside a groovy script 如何从 Jenkins 中的配置文件(groovy 脚本)中读取值? - How to read value from config file (groovy script) in Jenkins? 我如何从 groovy 脚本调用另一个 jenkins 构建作业 - How can i invoke another jenkins build job from groovy script 如何从 Jenkins Groovy 脚本中执行 HTTP POST 请求? - How can I perform HTTP POST requests from within a Jenkins Groovy script? 使用 Groovy 脚本从 Jenkins 的工作区读取文件 - Reading file from Workspace in Jenkins with Groovy script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM