我想创建第一个共享库以在jenkins管道中分解我的代码。例如,我对我所有的管道使用两种notify方法,并且我希望将它们放在一个位置。因此,我搜索了如何创建共享库,并且做到了:
在我的Notify类中,我的方法:
#!/usr/bin/env groovy
package fr.enterprise
class Notify {
static def notifySuccessful(String targetEnv) {
emailext (
subject: "SUCCESSFUL: New version deployed on $targetEnv",
body: """<html>
<body>
Go try it now! It's better when it's hot.
<br>
<br>With love,
<br>Your Dear Jenkins
</body>
</html>""",
recipientProviders: [[$class: 'RequesterRecipientProvider']]
)
}
static def notifyFailed(String targetEnv, String jobName, String buildUrl, String buildNumber) {
emailext (
subject: "FAILURE: Couldn't deploy new version on $targetEnv",
body: """<html>
<body>
I'm really sorry, but something went wrong when deploying Fides.
<br>
Please have a look at the logs here:
<br><a href="$buildUrl/console">$jobName [$buildNumber]</a>
<br>
<br>With love,
<br>Your Dear Jenkins
</body>
</html>""",
recipientProviders: [[$class: 'RequesterRecipientProvider']]
)
}
}
我将其导入我的管道代码中:
@Library('jenkins-shared-lib')
import fr.enterprise.Notify
当我的管道要使用我的方法之一时,出现此错误:
groovy.lang.MissingMethodException: No signature of method: java.lang.Class.emailext() is applicable for argument types: (java.util.LinkedHashMap)
我忘记了什么?
这里是我的代码来调用我的方法:
success {
script {
Notify.notifySuccessful(params.TARGET_ENV)
}
}
failure {
script {
Notify.notifyFailed(params.TARGET_ENV, env.JOB_NAME, env.BUILD_URL, env.BUILD_NUMBER)
}
}
答案 0 :(得分:0)
从Jenkins文档:库类不能直接调用诸如sh或git (或您正在使用的步骤)之类的步骤。 为了做到这一点,你应该做这样的事情
Notify.groovy
#! /usr/bin groovy
def notifySuccessful(String targetEnv) {
your code
}
return this
请注意返回此内容的用法。
然后从管道中使用声明即可
@Library('jenkins-shared-lib') _
def notify = new fr.enterprise.Notify()
notify.notifySuccessful("var")