Fra*_*sac 5 groovy jenkins email-ext
我想创建一个第一个共享库来分解jenkins管道中的代码.例如,我对所有管道使用两种通知方法,我希望将它们放在一个位置.所以我搜索了如何创建一个共享库,我已经这样做了:
在我的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']]
)
}
}
Run Code Online (Sandbox Code Playgroud)
我在管道代码中导入它:
@Library('jenkins-shared-lib')
import fr.enterprise.Notify
Run Code Online (Sandbox Code Playgroud)
当我的管道想要使用我的方法之一时,我有这个错误:
groovy.lang.MissingMethodException: No signature of method: java.lang.Class.emailext() is applicable for argument types: (java.util.LinkedHashMap)
Run Code Online (Sandbox Code Playgroud)
我忘记了什么?
这里我的代码调用我的方法:
success {
script {
Notify.notifySuccessful(params.TARGET_ENV)
}
}
failure {
script {
Notify.notifyFailed(params.TARGET_ENV, env.JOB_NAME, env.BUILD_URL, env.BUILD_NUMBER)
}
}
Run Code Online (Sandbox Code Playgroud)
小智 0
来自 Jenkins 文档:库类不能直接调用 sh 或 git (或您正在使用的)等步骤。为了做到这一点,你应该做这样的事情
通知.groovy
#! /usr/bin groovy
def notifySuccessful(String targetEnv) {
your code
}
return this
Run Code Online (Sandbox Code Playgroud)
请注意return this的用法。
然后从管道声明中您可以使用它
@Library('jenkins-shared-lib') _
def notify = new fr.enterprise.Notify()
notify.notifySuccessful("var")
Run Code Online (Sandbox Code Playgroud)