Jenkins样品Groovy Code与Plain Groovy(关闭错误)

发布于 2025-01-30 13:27:19 字数 1578 浏览 1 评论 0原文

在詹金斯共享库中,我可以做这样的事情:

jenkinsfile

@Library(value="my-shared-lib", changelog=false) _
jobGenerator {
        notifier = [notifyEveryUnstableBuild: true]
}

sharedlibary/vars/jobgenerator.groovy

def call(body) {
    println 'hi!'
}

可以更好地了解我本地创建了两个GROOVY文件的流程(根本没有提及Jenkins):

samples/pluncher.groovy

jobGenerator {
 s = 's'
}

samples/jobgenerator.groovy

def call(body) {
 println 'inside jobGenerator '
}

但是当我使用它运行时:

groovy "/home/user/samples/launcher.groovy"

我得到:

Caught: groovy.lang.MissingMethodException: No signature of method: launcher.jobGenerator() is applicable for argument types: (launcher$_run_closure1) values: [launcher$_run_closure1@61019f59]
groovy.lang.MissingMethodException: No signature of method: launcher.jobGenerator() is applicable for argument types: (launcher$_run_closure1) values: [launcher$_run_closure1@61019f59]
    at launcher.run(launcher.groovy:2)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

上面有多少代码詹金斯/共享库是特定的吗?甚至有可能在 plain groovy中写下类似上述内容吗?

或以另一种方式放置。如何将上述Jenkins代码转换为普通的Groovy?

In a jenkins shared library I can do something like this:

Jenkinsfile

@Library(value="my-shared-lib", changelog=false) _
jobGenerator {
        notifier = [notifyEveryUnstableBuild: true]
}

sharedLibary/vars/jobGenerator.groovy

def call(body) {
    println 'hi!'
}

To better understand the flow of whats goes on I have created two groovy files locally (with no reference to jenkins at all):

samples/launcher.groovy

jobGenerator {
 s = 's'
}

samples/jobGenerator.groovy

def call(body) {
 println 'inside jobGenerator '
}

But when I run that with:

groovy "/home/user/samples/launcher.groovy"

I get:

Caught: groovy.lang.MissingMethodException: No signature of method: launcher.jobGenerator() is applicable for argument types: (launcher$_run_closure1) values: [launcher$_run_closure1@61019f59]
groovy.lang.MissingMethodException: No signature of method: launcher.jobGenerator() is applicable for argument types: (launcher$_run_closure1) values: [launcher$_run_closure1@61019f59]
    at launcher.run(launcher.groovy:2)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

So how much of the above code is jenkins/shared library specific? And is it even possible to write something like the above in plain groovy?

Or put in another way. How do I convert the above jenkins code to plain groovy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘴硬脾气大 2025-02-06 13:27:19

关注接近詹金斯在做

启动器。

// load library scripts/functions
def binding = this.getBinding()
def gshell = new GroovyShell(this.getClass().getClassLoader(),binding)
new File("./my-lib").traverse(nameFilter: ~/.*\.groovy$/){f-> binding[f.name[0..-8]] = gshell.parse(f) }

// main
bar{
    foo(name:"world")
}

以恕我直言的

def call (Map m){
    return "hello $m.name"
}

>

def call (Closure c){
    println ( "BAR: "+c() )
}
#> groovy launcher.groovy

BAR: hello world

IMHO following is close to what jenkins is doing

launcher.groovy

// load library scripts/functions
def binding = this.getBinding()
def gshell = new GroovyShell(this.getClass().getClassLoader(),binding)
new File("./my-lib").traverse(nameFilter: ~/.*\.groovy$/){f-> binding[f.name[0..-8]] = gshell.parse(f) }

// main
bar{
    foo(name:"world")
}

./my-lib/foo.groovy

def call (Map m){
    return "hello $m.name"
}

./my-lib/bar.groovy

def call (Closure c){
    println ( "BAR: "+c() )
}
#> groovy launcher.groovy

BAR: hello world
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文