在'src/**'下用groovy文件动态加载Jenkins共享库

h43kikqp  于 2022-11-02  发布在  Jenkins
关注(0)|答案(1)|浏览(253)

我正在构建一个Jenkins管道来自动验证共享库(来自Gerrit),方法是重用我们现在拥有的任何管道。
最初的想法应该很简单,即:

@Library( 'shared-libs@master' ) _
//                       |
//                       |
//         variable instead of hard code
//                       |
//                       v
@Library( "shared-libs@${refs_params}" ) _

库结构:

(root)
+- src                                # Groovy source files
|   +- org
|       +- jenkins
|           +- FooDefinition.groovy   # for org.jenkins.FooDefinition
|           +- BarDefinition.groovy   # for org.jenkins.BarDefinition
|           +- ...                    # for org.jenkins....
|       +- virtualization
|           +- PodDefinition.groovy   # for org.virtualization.PodDefinition
|           +- ...                    # for org.virtualization....
+- vars
|   +- foo.groovy                     # for global 'foo' variable
|   +- logger.groovy                  # for global 'logger' variable

对于常规流水线,我使用groovy共享库(在Manage Jenkins-〉Configure System-〉Global Pipeline Libraries中预设置),方法是:

@Library( 'shared-libs@master' ) _
import org.virtualization.PodDefinition

// generate pod
PodDefinition.runWith( ... ) {
  // pipeline content
}

确保refs/changes/x指定参考规范+refs/changes/*:refs/remotes/@{remote}/changes/*

// works by using hard code `refs/changes/x/x/123`
@Library( 'shared-libs@refs/changes/x/x/123' ) _
import org.virtualization.PodDefinition

然而,当我使用GERRIT_REFSPEC时,由于WorkflowScript: @Library value ‘shared-libs@$GERRIT_REFSPEC’ was not a constant的原因,它不起作用,如下所示:

  • @Library( 'shared-libs@${GERRIT_REFSPEC}' ) _
  • @Library( 'shared-libs@${env.GERRIT_REFSPEC}' ) _
  • @Library( 'shared-libs@${params.GERRIT_REFSPEC}' ) _

因此,我已经根据官方文档Extending with Shared Libraries修改了库的用法,将其修改为library( "shared-libs@${GERRIT_REFSPEC}" ),libs克隆工作正常,但import org.virtualization.PodDefinition失败:

library( "shared-libs@${GERRIT_REFSPEC}" )
// or
this._ = library( "shared-libs@${GERRIT_REFSPEC}" )
// or
this = library( "shared-libs@${GERRIT_REFSPEC}" )
// or
library( "shared-libs@${GERRIT_REFSPEC}" ) _

import org.virtualization.PodDefinition 

// WorkflowScript: 5: unable to resolve class org.virtualization.PodDefinition
//  @ line 3, column 1.
//   import org.virtualization.PodDefinition
//   ^
//
// 1 error

所以,我试过:

  • load path/to/x.grovy
library( "shared-libs@${GERRIT_REFSPEC}" )
node( 'built-in' ) {
  load "../${env.JOB_BASE_NAME}@libs/<reivsion>/src/org/jenkins/FooDefinition.groovy"
  load "../${env.JOB_BASE_NAME}@libs/<reivsion>/src/org/jenkins/BarDefinition.groovy"
  load "../${env.JOB_BASE_NAME}@libs/<reivsion>/src/org/virtualization/PodDefinition.groovy"
}

// --- shows thrid party libs import failure ---
// org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
// Script7.groovy: 3: unable to resolve class com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition
//  @ line 3, column 1.
//    import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition
//    ^
//
// 1 error

个字符串

def a = library( "shared-libs@${GERRIT_REFSPEC}" ) org.jenkins.FooDefinition.groovy
def b = library( "shared-libs@${GERRIT_REFSPEC}" ) org.jenkins.BarDefinition.groovy
def c = library( "shared-libs@${GERRIT_REFSPEC}" ) org.jenkins.xxx.groovy
def d = library( "shared-libs@${GERRIT_REFSPEC}" ) org.virtualization.PodDefinition.groovy
def e = library( "shared-libs@${GERRIT_REFSPEC}" ) org.virtualization.xxx.groovy

带来了困难

  • 第一个

因此,我不知道下一步应该做什么,是将"shared-libs@${GERRIT_REFSPEC}"“更改”为常量以使用@Library(...) _,还是使用library(...)导入src/**下的所有库
顺便说一句,这里我已经尝试过将变量“转换”为常量的方法:

Object.metaclass.SHARED_LIBS = "shared-libs@${GERRIT_REFSPEC}"
@Library( SHARED_LIBS ) _

// or
class SHARED_LIBS {
  final String REFSPEC = "shared-libs@${GERRIT_REFSPEC}"
}
@Library( SHARED_LIBS.REFSPEC ) _

// or
import groovy.transform.Field
@Field final String SHARED_LIBS = "shared-libs@${GERRIT_REFSPEC}"
@Library( SHARED_LIBS ) _

// or
def bindings = new Binding()
bindings.setVariable("SHARED_LIBS", "shared-libs@${GERRIT_REFSPEC}")
@Library( SHARED_LIBS ) _

// or
withEnv([ 'SHARED_LIBS=shared-libs@${GERRIT_REFSPEC}' ]) {
  @Library( SHARED_LIBS ) _
}
h22fl7wq

h22fl7wq1#

您可以在groovy文件的任意位置动态加载库,并使用完整的类名来使用函数。

def myLib = dsl.library 'my-shared-lib@<SHA>'
myLib.net.vy.a.v1.Sonarqube.call(dsl, parameters)

这里的net.vy.a.v1.Sonarqube是实际的包。dslScript的对象

相关问题