gpt4 book ai didi

jenkins - Jenkins 管道中的变量

转载 作者:行者123 更新时间:2023-12-04 18:31:33 30 4
gpt4 key购买 nike

为了使我的 jenkins 管道定义文件更具可定制性,我尝试使用最多的变量。

当我尝试在 中使用变量时邮箱 步骤 指令 Jenkins 抛出这个错误:

java.lang.NoSuchMethodError: No such DSL method '$' found among [archive, bat, build, catchError, checkout, deleteDir, dir, echo, emailext, emailextrecipients, error, fileExists, git, input, isUnix, load, mail, node, parallel, properties, pwd, readFile, readTrusted, retry, sh, sleep, stage, stash, step, svn, timeout, timestamps, tool, unarchive, unstash, waitUntil, withCredentials, withEnv, wrap, writeFile, ws]

这是我的 jenkins pipleline 定义文件:
#!groovy
node {

//Define job context constants
def projectName = "JenkinsPipelineTest"
def notificationEmailRecipients = "aaa@domain.com"
def notificationEmailSender = "bbb@domain.com"
currentBuild.result = "SUCCESS"

//Handle error that can occur in every satge
try {

//Some others stage...

stage 'Finalization'
step([$class: 'ArtifactArchiver', artifacts: '*.zip, *.tar, *.exe, *.html', excludes: null])
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: ${notificationEmailRecipients}, sendToIndividuals: false])
}
catch (err) {
//Set built state to error
currentBuild.result = "FAILURE"

//Send error notification mail
mail body: ${err},
charset: 'UTF-8',
from: ${notificationEmailSender},
mimeType: 'text/plain',
replyTo: ${notificationEmailSender},
subject: '"${projectName}" meet an error',
to: ${notificationEmailRecipients}

throw err
}
}

这是正常的还是我的定义文件中有错误?

最佳答案

这是我的错!

我混淆了字符串中的变量和变量和 Groovy 代码:

代码:

step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: ${notificationEmailRecipients}, sendToIndividuals: false])

必须是:
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: notificationEmailRecipients, sendToIndividuals: false])

这里我一定不能用 ${} 因为我在 Groovy 代码中而不是在字符串中。

第二个错误,邮件正文必须是:
mail body: "Error: ${err}"

并不是:
mail body: ${err}

因为 错误 这是一个 IOException 类实例而不是字符串。

所以最终的代码是:
#!groovy
node {

//Define job context constants
def projectName = "JenkinsPipelineTest"
def notificationEmailRecipients = "aaa@domain.com"
def notificationEmailSender = "bbb@domain.com"
currentBuild.result = "SUCCESS"

//Handle error that can occur in every satge
try {

//Some others stage...

stage 'Finalization'
step([$class: 'ArtifactArchiver', artifacts: '*.zip, *.tar, *.exe, *.html', excludes: null])
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: notificationEmailRecipients, sendToIndividuals: false])
}
catch (err) {
//Set built state to error
currentBuild.result = "FAILURE"

//Send error notification mail
mail body: "Error: ${err}",
charset: 'UTF-8',
from: notificationEmailSender,
mimeType: 'text/plain',
replyTo: notificationEmailSender,
subject: '${projectName} meet an error',
to: notificationEmailRecipients

throw err
}
}

希望这个答案会有所帮助。

关于jenkins - Jenkins 管道中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38803970/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com