获取作业DSL处理的脚本目录的绝对路径

获取作业DSL处理的脚本目录的绝对路径

本文介绍了获取作业DSL处理的脚本目录的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义管道作业的DSL groovy脚本。我需要从工作区加载Jenkinsfile。 Jenkinsfile与groovy脚本位于同一文件夹中。我正在尝试以编程方式获取groovy脚本的路径,以便可以使用它来找出Jenkinsfile的路径并使用 readFileFromWorkspace 加载它。经过。但是我收到以下错误:

I have a DSL groovy script defining a pipeline job. I need to load Jenkinsfile from the workspace. The Jenkinsfile resides in the same folder as that of the groovy script. I am trying to get the path of the groovy script programmatically so that I can use that to figure out the path of the Jenkinsfile and load it using readFileFromWorkspace. I tried using __FILE__ directive after going through the job-dsl-wiki. But I am getting the following error:

Processing provided DSL script
ERROR: (test_job.groovy, line 3) No such property: absolutePath for class: java.lang.String
Finished: FAILURE

此处是我的DSL脚本

job_name = "my-pipeline-job"
job_path = "${new File(__FILE__).parent.absolutePath}"
jenkinsfile = job_path + "/Jenkinsfile"
pipelineJob(job_name){
 description("Jenkins pipeline job")
 parameters{
  stringParam("MyTestParam", "", "a sample parameter")
 }
 definition {
   cps {
      sandbox()
      script(readFileFromWorkspace(jenkinsfile))
   }
 }
}

我有什么东西吗?这里做错了吗?非常感谢您提供任何帮助。

Is there anything that I am doing wrong here? Really appreciate any help on this.

推荐答案

Wiki中的文档有点误导。这是有效的解决方案。

The documentation in the wiki is a little misleading. Here is the working solution.

job_name = "my-pipeline-job"
println "Script: ${ __FILE__}"
println("script directory: ${new File(__FILE__).parent}")
job_path = "${new File(__FILE__).parent}"
jenkinsfile = job_path + "/Jenkinsfile"
pipelineJob(job_name){
 description("Jenkins pipeline job")
 parameters{
   stringParam("MyTestParam", "", "a sample parameter")
 }
 definition {
   cps {
      sandbox()
      script(readFileFromWorkspace(jenkinsfile))
   }
 }
}

这篇关于获取作业DSL处理的脚本目录的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 13:54