Jenkins build says successful but no output is seen - shell

My Jenkins build job indicates success but none of the scripts I am executing seem to do anything. I put a simple "pwd" command to echo current working directory but nothing happens. All Jenkins reports is "Running shell script" and then nothing is seen.
Snippet of Jenkins log:
First time build. Skipping changelog.
[Pipeline] sh
[an_example_test3-26UQSYQ445FSBP4QRKEWLPQCJH545MLQVD6B552CIGPOXLZO4G5A] Running shell script
[Pipeline] sh
[an_example_test3-26UQSYQ445FSBP4QRKEWLPQCJH545MLQVD6B552CIGPOXLZO4G5A] Running shell script
[Pipeline] sh
[an_example_test3-26UQSYQ445FSBP4QRKEWLPQCJH545MLQVD6B552CIGPOXLZO4G5A] Running shell script
[Pipeline] sh
[an_example_test3-26UQSYQ445FSBP4QRKEWLPQCJH545MLQVD6B552CIGPOXLZO4G5A] Running shell script
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Jenkinsfile that is used to define the builds:
node {
stage 'build'
sh "echo ${env.BRANCH_NAME}"
git url: 'git#hd1:testing', branch: "${env.BRANCH_NAME}"
sh "ls -rtl"
sh "pwd"
sh "csh ${workspace}/simple.csh"
sh "csh ${workspace}/source.csh"
}

Related

Cannot call ruby rake with parameter containing spaces in jenkins pipeline

I have Jenkins Pipeline that will deploy a pod with a container using my ruby's application image, and there is a stage that will run a rake command from a $COMMAND parameter like this:
rake db_command:count_data[histories,status IN ("active","pending")] inside the container.
this is the step I wrote on my stage to run the rake command from a $COMMAND parameter:
stage("Rake Task") {
try {
container("ruby-rake-task") {
sh '''
cd /var/www/mos
${COMMAND}
'''
}
} catch(e) {
echo "rake failed"
sh "exit 1"
}
}
but I get this error from console output jenkins:
+ cd /var/www/mos
+ rake 'db_command:count_data[histories,status' IN '("active","pending")]'
rake aborted!
Don't know how to build task 'db_command:count_data[histories,status' (See the list of available tasks with `rake --tasks`)
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)
it seems the jenkins or groovy won't execute the rake command as a whole command, and instead it split the command to three parts. Jenkins is somehow always give a quote for all parameters after 'rake' command this really hard when the parameter is having spaces like above.
I already try adjust my stage step like this:
stage("Rake Task") {
try {
container("ruby-rake-task") {
sh "cd /var/www/mos"
def rakeTask = $/eval ${COMMAND}/$
resultRake = sh(script: "${rakeTask}", returnStdout: true).toString()
echo "${resultRake}"
}
} catch(e) {
echo "rake failed"
sh "exit 1"
}
}
yet the result is produce another error like this:
[Pipeline] stage
[Pipeline] { (Rake Task)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ cd /var/www/mos
[Pipeline] sh
/home/jenkins/agent/workspace/RUBY-APP-RAKE-TASK#tmp/durable-c0e8d0d4/script.sh: line 1: syntax error: unexpected "("
[Pipeline] }
[Pipeline] // container
what do I need to do to be able run this rake task as a whole command included the spaces parameter? can someone tell me what am I missing here?
Thank you!
I'm using a node container with entrypoint "bash" and resolved errors like your.
I adapt it for you:
"-c \"cd /var/www/mos && ${command}\""
I hope it helps.

Jenkinsfile not executing entire shell script

I have the following in a Jenkinsfile as a discrete stage:
stage("Check if Postman scripts are already cloned, delete") {
steps {
sh "[ -d postman-scripts ] && rm -rf postman-scripts && echo 'Directory cleared.'"
}
}
However when I look at my Jenkins pipeline it looks like it fails and doesn't run the entire script:
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Check if Postman scripts are already cloned, delete)
[Pipeline] sh
+ '[' -d postman-scripts ']'
[Pipeline] }
Is that because it's not finding the postman-scripts directory, so it's not executing the rest of the script? And is that being counted as a build failure? It skips all following stages, so logically I think it's doing that.
Also any help with the bash scripting would be nice, I'm not entirely sure how I can string this all together on one line to do what I want (IF directory exists, remove directory. Otherwise do nothing).
Your shell test condition [ -d postman-scripts ] will FAIL if the directory does not exist. That is a failure like any other command, so it exits. #jordanm is correct, using -f will return success even if nothing exists to remove, so your next step will continue. Exception if unable to remove files because of a system lock or similar, which is probably a good thing,

Shell command does not work in Jenkins pipeline

My shell command works when run in the terminal, but not in a Jenkins pipeline; when the pipeline runs test.sh, the shell is run, but the bug service is not. Why is that, and how can I fix this?
run.sh:
nohup /usr/local/java/1.8/bin/java -Dspring.cloud.config.profile=test -Dspring.cloud.config.uri=http://localhost:8888/ -cp /data/jcpt/service/service-account/jar/.:/data/jcpt/service/service-account/jar/lib/* com.caifubao.jcpt.account.app.AccountApplication >./logs/service-account.log &
Jenkins pipeline:
stage('UPLOAD') {
agent{node { label "TEST" }}
steps {
sh "./test.sh"
}
}

How to load variables from file into windows environment variables in Jenkins pipeline?

I have the following Jenkinsfile
node {
stage 'checkout'
deleteDir()
checkout scm
load 'LoadTheseVariables.txt'
echo "MYKEY: ${MYKEY}"
echo "REPO: ${REPO}"
bat 'echo MYKEY is %MYKEY%'
bat 'echo REPO is %REPO%'
}
The file LoadTheseVariables.txt contains:
MYKEY="ThisIsTheKey"
REPO="ThisIsTheRepo"
The output of the Jenkins build is:
<..snip..>
[Pipeline] load
[Pipeline] { (LoadTheseVariables.txt)
[Pipeline] }
[Pipeline] // load
[Pipeline] echo
MYKEY: ThisIsTheKey
[Pipeline] echo
REPO: ThisIsTheRepo
[Pipeline] bat
[test] Running batch script
D:\Jenkins\workspace\test>echo MYKEY is
MYKEY is
[Pipeline] bat
[test] Running batch script
D:\Jenkins\workspace\test>echo REPO is
REPO is
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
The Jenkins pipeline can see the variables but why can't my windows cmd environment see the variables? I even tried this step before the bat commands at the end: bat 'refreshenv' but that didn't reload the environment with those variables.
How can I load variables from a file in a Windows job and have them loaded into the environment at run time?
The solution is to use double quotes instead of single around the bat command. Seems to work fine.

Jenkins Pipeline cannot execute SH command file in a Windows slave

I'm executing this code:
node('my_windows_slave') {
sh 'ls'
}
In my Windows slave I can properly execute sh command:
But the pipeline script can't run the .sh file:
[Pipeline] sh
[D:\workspace\sandbox_pipeline] Running shell script
sh: D:\workspace\sandbox_pipeline#tmp\durable-2d7dd2f8\script.sh: command not found
What I could notice is that this .sh file is not even created, once I tried with bat and worked fined.
Any clue what could be the problem?
[UPDATE]
Jenkins somehow can't create the SH temporary file. Already checked the log, permissions, everything that came to my mind.
I will leave my workaround as an answer for while before approve it once I'm still not 100% sure about the root cause and might someone else show up with a elegant solution...
def shell(command) {
return bat(returnStdout: true, script: "sh -x -c \"${command}\"").trim()
}
Attention
You still executing SH commands in a CMD, it means some %d for example can break your SH command.
Use the bat step instead of sh.
From Jenkins docs:
Windows-based systems should use the bat step for executing batch commands.

Resources