我可以从Jenkinsfile的相对目录中导入一个groovy脚本吗? [英] Can I import a groovy script from a relative directory from a Jenkinsfile?

查看:888
本文介绍了我可以从Jenkinsfile的相对目录中导入一个groovy脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的项目:

  / 
/ Jenkinsfile
/ build_tools /
/ pipeline.groovy#定义流水线的函数
/ reporting.groovy#其他misc构建报告的东西
/ dostuff.sh#流水线使用的shell脚本
/ domorestuff.sh#另一个支持shell脚本的管道

是否可以将groovy文件导入/ build_tools所以我可以在我的Jenkinsfile中使用这两个文件中的函数?



理想情况下,我想要一个Jenkins文件,看起来像这样(伪代码):

  from build_tools.pipeline import build_pipeline 
build_pipeline(project_name =my project,reporting_id = 12345)

我坚持的一点是如何在我的第1行上编写假装导入语句伪代码。



PS。为什么我这样做:build_tools文件夹实际上是一个由许多项目共享的git子模块。我试图让每个项目都有一套通用的构建工具来阻止每个项目维护人员重新发明这个轮子。

解决方案

加载共享groovy代码的最佳方式是通过

然后使用它,例如,像这样:

  pipeline {
agent {label'docker'}
stages {
stage(' build'){
steps {
script {
@Library('simplest-jenkins-shared-library')
def bar = new org.foo.Bar()
bar.awesomePrintingFunction()
}
}
}
}
}

从这个版本的控制台日志中输出co包括:

  hello world 

还有很多其他的方式可以编写共享库(比如使用类)并使用它们(比如定义变量,以便可以用超级流畅的方式在Jenkins文件中使用它们)。您甚至可以将非常规文件作为资源加载。查看这些扩展用例的共享库文档


I've got a project structured like this:

/
/ Jenkinsfile 
/ build_tools /
              / pipeline.groovy # Functions which define the pipeline
              / reporting.groovy # Other misc build reporting stuff
              / dostuff.sh # A shell script used by the pipeline
              / domorestuff.sh # Another pipeline supporting shell-script

Is it possible to import the groovy files in /build_tools so that I can use functions inside those 2 files in my Jenkinsfile?

Ideally, I'd like to have a Jenkins file that looks something like this (pseudocode):

from build_tools.pipeline import build_pipeline
build_pipeline(project_name="my project", reporting_id=12345)

The bit I'm stuck on is how you write a working equivalent of that pretend import statement on line #1 of my pseudocode.

PS. Why I'm doing this: The build_tools folder is actually a git submodule shared by many projects. I'm trying to give each project access to a common set of build tooling to stop each project maintainer from reinventing this wheel.

解决方案

The best-supported way to load shared groovy code is through shared libraries.

If you have a shared library like this:

simplest-jenkins-shared-library master % cat src/org/foo/Bar.groovy
package org.foo;

def awesomePrintingFunction() {
  println "hello world"
}

Shove it into source control, configure it in your jenkins job or even globally (this is one of the only things you do through the Jenkins UI when using pipeline), like in this screenshot:

and then use it, for example, like this:

pipeline {
  agent { label 'docker' }
  stages {
    stage('build') {
      steps {
        script {
          @Library('simplest-jenkins-shared-library')
          def bar = new org.foo.Bar()
          bar.awesomePrintingFunction()
        }
      }
    }
  }
}

Output from the console log for this build would of course include:

hello world

There are lots of other ways to write shared libraries (like using classes) and to use them (like defining vars so you can use them in Jenkinsfiles in super-slick ways). You can even load non-groovy files as resources. Check out the shared library docs for these extended use-cases.

这篇关于我可以从Jenkinsfile的相对目录中导入一个groovy脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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