Jenkins script execution in console output - bash

When I try to execute the shell script test.sh in Jenkins by adding Execute Shell in Jenkins Build configuration I don't see the shell script output in the jenkins console. I just receive that script has been executed seccessfully , but I don't see the output. My script for example is simple just `
#!/bin/bash +x
echo "hello"
and Jenkins console output is :
Started by user Andrej
Building in workspace C:\Program Files (x86)\Jenkins\workspace\test
[test] $ "C:\Program Files\Git\git-bash.exe" -xe C:\Users\ADMINI~1\AppData\Local\Temp\jenkins6727659055076114908.sh
C:\Program Files (x86)\Jenkins\workspace\test>exit 0
Finished: SUCCESS
How can I configure the jenkins that I can see all the script output and if script have receive error I get unseccessfull build message?

Make sure you set up your windows version of Jenkins to run shell scripts.
I noticed the path for your exe isn't for git or cygwin.
Check out this Stack Overflow answer for how to get windows to run shell commands in Jenkins.

Related

The shell script called inside 'Execute Shell' build step fails, but Jenkins build is marked as passed

I am calling a shell script with some parameters within 'Execute Shell' build step in Jenkins.
./shell_1.sh "$Param1" "$Param2" "$Param3"
This shell script downloads puppet modules, sets up the directories and then finally calls a puppet script.
puppet apply ${WORKSPACE}/scripts/puppet/sample.pp
The problem here is that Jenkins console shows the error from the puppet script and then finish with Success status.
Following are the last 3 lines from Jenkins console:
06:46:44 [1;31mError: /Stage[main]/Main/Deployit_environment[Environments/Build/Test/UAT/UAT-env]: Could not evaluate: cannot send request to deployit server 404/Not Found:Repository entity [Infrastructure/Build/Test/UAT/server_123/test-sample-service] not found[0m
06:46:44 [mNotice: Applied catalog in 1.82 seconds[0m
06:46:45 Finished: SUCCESS
I want the Jenkins job to fail if there is any error in puppet script.
I tried calling shell script with -xe option. But didn't work.
Thanks in advance.
The Jenkins Build step checks if the error code of the script is 0. If it has another value it will fail the build.
You can use special shell variable called $? to get the exit status of the previously executed command. To print $? variable use the echo command:
echo $?

Groovy Execute Bash Script in Jenkins

I am trying to run a bash script from Groovy in Jenkins but I am not able to find the lucky commands.
I can run this and it creates my "RJ" directory:
process = "mkdir /app/jenkins/workspace/TEST/RJ"
println process.execute()
But when I try to run my bash it is not creating my output file. I am able to run this bash script on the server directly and it is creating my expected output file.
process = "/app/jenkins/workspace/TEST/info_file.sh"
println process.execute()
why run it through groovy and not directly via a ssh command.
If you do want to do via groovy, you'll need to add a ssh libray, and do the whole connection, auth, execute. process.execute won't run a linux box.
first, you don't check the stderr and not waiting for process to end.
similar problem was here:
Curl request from command line and via Groovy script
with error text it's easier to solve the error.

Jenkins fails with Execute shell script

I have my bash script in ${JENKINS_HOME}/scripts/convertSubt.sh
My job has build step Execute shell:
However after I run job it fails:
The error message (i.e. the 0: part) suggests, that there is an error while executing the script.
You could run the script with
sh -x convertSubt.sh
For the safe side, you could also do a
ls -l convertSubt.sh
file convertSubt.sh
before you run it.
make sure that the script exist with ls
no need to sh , just ./convertSubs.sh ( make sure you have run permissions)

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.

Jenkins - Execute Shell Build Step Not Working

I'm trying to create a job in Jenkins that will execute a simple shell script. However, it seems that Jenkins isn't actually doing anything with my script. No matter what value I put into the Execute Shell Command section, Jenkins always says that it passes. Even if I put in a bogus filename like "RandomBogusFilename.sh" it'll say the job was a success.
Can anyone point out what I'm doing wrong and how I can get Jenkins to actually use my shell script?
The shell script, the job config, and the console output are all shown below. I'm currently trying to do this on a Windows Server 2008 R2 Standard machine.
Thanks.
My .sh file
File Name: surveyToolRequest.sh
File Location: /jobs/Jeff Shell Script Test/workspace
Description:
Hit a web address and retrieve the HTTP Response. Then print out the HTTP Response.
#!/bin/bash
response_code=$(curl -s -o /dev/null -w "%{http_code}" http://SOME-WEBSITE.COM)
echo "The response code is " $response_code
My Jenkins Job Config
Jenkins Console Output
I played with this and found that it worked if I specified the path to the script. If the script is in your job's workspace directory,
./surveyToolRequest.sh
should work as Jenkins looks for files relative to the root of the workspace.
It's more common to just put the contents of the script file directory into the job configuration; that way you can see what the job is doing and you'll avoid problems like this one.
You should use run "Execute windows batch command" and not "Execute Shell"

Resources