Cannot save sh command output through DSL (groovy) in Jenkins work

I want to get the latest build output in a Jenkins job in the pipeline and connect via email (using the emailext plugin). Curl works fine and gives the correct build result, but I cannot store the variable for attachment in the letter. I am using the latest version of jenkins.

I see that there are several related messages for the simple sh command, but this does not work for the vortex store.

Trial Code:

one.

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 
+3
bash curl jenkins groovy
Dec 03 '16 at 10:23
source share
1 answer

It looks like you are missing an internal array and some double quotes and escaped double quotes to run the script:

 sh([ script: "curl \"http://${jenkinsUser}:${jenkinsUserToken}@${jenkinsServer}:8080/job/myJob/lastBuild/consoleText\"").trim() 

There are also several ways to execute shell scripts, and this depends on the type of jenkins type you are using.

In the jenkins declarative pipeline, you need to include a script {...} block script {...} for all script types and installation 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 script, you don’t need a script{} block, you can actually place it in most places. I basically put it in the frames of the stage stage('some stage'){...} , and I did it like this:

 V5_DIR = WORKSPACE + '/' + sh([script: "basename ${V5_GIT_URL} .git", returnStdout: true]).trim() 

Although I also used curl commands (for script pipelines) and did not need an internal array ...

 lastSuccessfulCommit = sh( script: "curl -sL --user ${JENKINS_API_USER}:${JENKINS_API_PSW} \"${lastSuccessfulCommitUrl}\" | sed -e 's/<[^>]*>//g'", returnStdout: true ) 

And for reference, echo-wars look like in

  sh([script: "echo \"Value: ${someVariable}\""]) 

Hope this documentation helps too, but I know that recently Jenkins documentation can be quite spotty, so I also found a great idea on how not to do something for Jenkins declarative pipelines . Good luck

+4
03 Oct '17 at 20:45
source share



All Articles