不能在Jenkins管道作业中通过DSL(groovy)存储sh命令输出 [英] Can't store sh command output through DSL (groovy) in Jenkins pipeline job
问题描述
我想在管道Jenkins工作中获得最后的构建输出并附加到电子邮件中(使用emailext插件)。卷曲工作正常,并提供适当的构建输出,但我不能存储在变量附加在电子邮件中。我使用的是最新的jenkins版本。
我可以看到简单的sh命令有两个相关的帖子,但是它不适用于curl响应商店。
试过的代码:
<1>
def consoleOutput = sh(returnStdout:true,script:'curl http://'jenkinsUser +':'+ jenkinsUserToken +'@'+ jenkinsServer +':8080 / job /'+'myJob / lastBuild / consoleText')。trim()
echo consoleOutput
2。
sh'curl http://'jenkinsUser +':'+ jenkinsUserToken +'@'+ jenkinsServer +':8080 / job /' +$ {env.JOB_NAME}+'/ lastBuild / consoleText; echo $? >状态'
def consoleOutput = readFile('status')。trim()
3。
def consoleOutput = sh(script:'curl http://'jenkinsUser +':'+ jenkinsUserToken +'@' + jenkinsServer +':8080 / job /'+'/ myJob / lastBuild / consoleText',returnStatus:true).split(\r?\ n)
echo consoleOutput
$它看起来像你缺少内部数组和一些双引号并且转义了双引号用于运行脚本:
$ $ p $ $ $ $ c $ sh $ {$ {jenkinsUser} $ { jenkinsUserToken} @ $ {jenkinsServer}:8080 / job / myJob / lastBuild / consoleText \)。trim()
也有多种方法可以执行shell脚本,它取决于您使用的 jenkins管道类型。
In a 脚本{...} 块,并且这看起来像这:
pipeline {
代理{
...
}
参数{
...
}
环境{
...
}
阶段{
阶段('运行所需脚本') {
steps {
...
脚本{
NOTIFIER_BULD_NAME = sh([script:./getNotifier.sh,returnStdout:true])。trim()
EMAIL_TEXT = sh([script:./printEmailText.sh $ {CURRENT_BUILD} $ {PREVIOUS_BUILD},returnStdout:true])。trim()
BODY = sh([script:curl \ http:// $ {jenkinsUser}:$ {jenkinsUserToken} @ $ {jenkinsServer}:8080 / job / myJob / lastBuild / consoleText \).trim()
}
}
}
阶段('发送电子邮件'){
当{
表达式{
//只有在有文本时才发送。
$ {EMAIL_TEXT}!=;
步骤{
emailext(
to:'software@company.com',
subject:You have mail - $ {EMAIL_TEXT },
body:$ {NOTIFIER_BULD_NAME} - $ {EMAIL_TEXT}:
...
$ {BODY}
,
attachLog :false
)
}
}
}
在 Jenkins脚本管道中,您不需要脚本{}
块,你可以把它放在大部分地方。大多数情况下,我已经将它放在舞台块 stage('some stage'){...}
中,并且我这样做了:
V5_DIR = WORKSPACE +'/'+ sh([script:basename $ {V5_GIT_URL} .git,returnStdout:true])。trim()
尽管我也使用了curl命令(用于脚本管道),并且不需要内部数组。 (
script:curl -sL --user $ {JENKINS_API_USER}:$ {
lastSuccessfulCommit = sh JENKINS_API_PSW} \$ {lastSuccessfulCommitUrl} \| sed -e's /< [>] *> // g',
returnStdout:true
)
作为参考,echo vars看起来像这样
< pre $
sh([script:echo \Value:$ {someVariable} \])
希望这个文档也有点帮助,但我k现在最近詹金斯的文档可能非常多斑点,所以我还发现一个关于如何不为詹金斯做点什么的好主意声明式管道。祝你好运!
I would like to get last build output in pipeline Jenkins job and attach in email (using emailext plugin). Curl works fine and gives proper build output but i can't store in variable to attach in the email. I'm using latest jenkins version.
I can see there are couple of related posts for simple sh command but that doesn't work for curl response store.
Tried code:
1.
def consoleOutput = sh(returnStdout: true, script: 'curl http://' + jenkinsUser + ':' + jenkinsUserToken + '@' + jenkinsServer + ':8080/job/' + 'myJob/lastBuild/consoleText').trim()
echo consoleOutput
2.
sh 'curl http://' + jenkinsUser + ':' + jenkinsUserToken + '@' + jenkinsServer + ':8080/job/' + "${env.JOB_NAME}" + '/lastBuild/consoleText; echo $? > status'
def consoleOutput = readFile('status').trim()
3.
def consoleOutput = sh(script: 'curl http://' + jenkinsUser + ':' + jenkinsUserToken + '@' + jenkinsServer + ':8080/job/' + '/myJob/lastBuild/consoleText', returnStatus: true).split("\r?\n")
echo consoleOutput
It looks like you're missing the inner array and some double quotes and escaped double quotes for running the script:
sh([ script: "curl \"http://${jenkinsUser}:${jenkinsUserToken}@${jenkinsServer}:8080/job/myJob/lastBuild/consoleText\"").trim()
Also there are multiple ways to do shell scripts and it depends on the type of jenkins pipeline you are using.
In a jenkins declarative pipeline you need to include a script {...}
block for all script type code and setting variables, and that would look like this:
pipeline {
agent {
...
}
parameters {
...
}
environment {
...
}
stages {
stage('Run Required Scripts') {
steps {
...
script {
NOTIFIER_BULD_NAME = sh([script: "./getNotifier.sh", returnStdout: true]).trim()
EMAIL_TEXT = sh([script: "./printEmailText.sh ${CURRENT_BUILD} ${PREVIOUS_BUILD}", returnStdout: true]).trim()
BODY= sh([ script: "curl \"http://${jenkinsUser}:${jenkinsUserToken}@${jenkinsServer}:8080/job/myJob/lastBuild/consoleText\"").trim()
}
}
}
stage('Send Email') {
when {
expression {
// Only send when there is text.
"${EMAIL_TEXT}" != "";
}
}
steps{
emailext (
to: 'software@company.com',
subject: "You have mail - ${EMAIL_TEXT}",
body: """${NOTIFIER_BULD_NAME} - ${EMAIL_TEXT}:
...
${BODY}
""",
attachLog: false
)
}
}
}
In a Jenkins scripted pipeline, you don't need a script{}
block, you can actually put it most places. Mostly I've put it in stage blocks stage('some stage'){...}
and I've done it like this:
V5_DIR = WORKSPACE + '/' + sh([script: "basename ${V5_GIT_URL} .git", returnStdout: true]).trim()
Although I've also used curl commands (for scripted pipelines) and didn't need the inner array...
lastSuccessfulCommit = sh(
script: "curl -sL --user ${JENKINS_API_USER}:${JENKINS_API_PSW} \"${lastSuccessfulCommitUrl}\" | sed -e 's/<[^>]*>//g'",
returnStdout: true
)
And for reference, echoing vars looks like this in both
sh([script: "echo \"Value: ${someVariable}\""])
Hopefully this documentation helps a bit too, but I know recently Jenkins documentation can be pretty spotty, so I also found a great gist about how to not do things for Jenkins Declarative pipelines. Good luck!
这篇关于不能在Jenkins管道作业中通过DSL(groovy)存储sh命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!