简体   繁体   English

我可以为脚本式管道到声明式方法中的方法创建共享库.groovy脚本吗

[英]Can i create a shared library .groovy script for a method in scripted pipeline into declarative

I am trying to convert all of the scripted pipelines at my workplace into declarative pipeline. 我正在尝试将工作场所中的所有脚本化管道转换为声明性管道。 I am new to this. 我是新来的。 I have a scripted pipeline that has 2 methods. 我有一个具有2个方法的脚本化管道。 I was able to finish the rest of the scripted to declarative but got stuck on methods. 我能够完成脚本化到声明式的其余部分,但陷入了方法的困境。 Since declarative doesn't really support methods and since i have to use this method multiple times in other declarative pipelines as well, i want to describe this method(s) as a groovy script in a shared library. 由于声明式并不真正支持方法,并且由于我也必须在其他声明式管道中多次使用此方法,因此我想将此方法描述为共享库中的常规脚本。

My question is, since this is a method from scripted pipeline, can i directly just copy paste my method into the groovy script or does it require exact syntax for groovy I checked the groovy syntax and don't really see much differences there ? 我的问题是,由于这是脚本化管道中的方法,我可以直接将我的方法复制粘贴到groovy脚本中吗,还是需要用于groovy的确切语法?

below is one of the the method: Can i just copy this into something like getversion.groovy and call it from my dec pipeline ? 下面是方法之一:我可以将其复制到类似getversion.groovy的文件中,然后从dec管道中调用它吗? or does it need syntax/code changes to put into the groovy script ? 还是需要将语法/代码更改放入groovy脚本中?

def getProjectVersion(directory) {
   dir(directory) {
      withEnv(["PATH+MAVEN=${env.M3}/bin"]) {
         sh 'rm -f version.txt'
         sh(
               """mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate \
               -Dexpression=project.version | grep -v '\\[\\|\\D'  > version.txt"""
           )      
           return readFile('version.txt').trim()
}

} }

There are some other complex methods as well in some of the other scripted pipelines that i am trying to convert to declarative, so this information would be very helpful. 我尝试将其他脚本化管道转换为声明性管道时,还有其他一些复杂的方法,因此此信息将非常有帮助。

Thanks 谢谢

If you have not done so already, check Shared Libraries for details. 如果尚未执行此操作,请查看“ 共享库”以获取详细信息。 You should be able to use your example by creating vars/getVersion.groovy : 您应该可以通过创建vars/getVersion.groovy使用示例:

def call(directory) {
   dir(directory) {
    ...  
   }
}

Set up your Shared Library as described in the link and you should be able to call your code in your pipeline: 如链接中所述设置共享库,您应该能够在管道中调用代码:

...
stage('Some stage') {
  steps {
    script {
      versionNumber = getVersion('/directory/of/project/')
    }
  }
}
...

In case your method does not have a return value the call might look like this: 如果您的方法没有返回值,则调用可能如下所示:

...
stage('Some stage') {
  steps {
    setVersion '/directory/of/project/'
  }
}
...

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

相关问题 我可以将声明性管道的整个阶段包装为groovy库函数吗? - Can I wrap a whole stage of a declarative pipeline as groovy library function? 我可以在共享库中定义和参数化整个声明性管道吗? - Can I have an entire declarative pipeline defined and parameterized in a shared library? 声明式管道共享库 - Declarative Pipeline shared library 我可以在声明/脚本 Jenkins 管道上使用 inheritance 吗? - Can I use inheritance on Declarative/Scripted Jenkins pipeline? 如何在Jenkins声明性管道中导入的groovy脚本中使用@Library? - How to use @Library in an imported groovy script in Jenkins declarative pipeline? 如何在继承自管道共享库的声明性 jenkins 管道中定义其他参数? - How can I define additional parameters in a declarative jenkins pipeline who inherit from a pipeline shared library? Jenkins 脚本化管道或声明性管道 - Jenkins scripted pipeline or declarative pipeline Jenkins 声明式管道添加 groovy postbuild 脚本 - Jenkins declarative pipeline add groovy postbuild script 在jenkins声明式管道文件中重用groovy脚本 - Reusing groovy script in jenkins Declarative pipeline file 使用 Spock 的 Groovy 共享库测试流水线步骤方法 - Groovy shared library testing pipeline step method with Spock
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM