Jenkins - Execute Shell Build Step Not Working - bash

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"

Related

Jenkins script execution in console output

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.

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.

'Nant' as not recognized command on Jenkins Mac Slave

I have a Jenkins's master-slave structure setup created having Master on windows server, plus few windows slaves and one Mac-slave.
The flow is like this,
Jenkins shell script triggers a shell command (sh sample.command) [this is used on both windows(using win-bash) and mac node.....]
The first step where it triggers the shell script is working fine on both windows and mac slave.
#!/bin/bash
echo “This is a shell script acting as a middleware to trigger the NAnt....”
echo "Calling NAnt...."
nant ${1} ${2} ${3} ${4}
2.Now, the sample.command has a code to trigger a nant command, which is not working on mac slave and giving me an error :
nant: command not found
3.The NAnt is installed on the Mac-slave through brew and when I trigger this shell script sample.command from the Mac machine, it works fine and executes the nant command, but doesn't work through jenkins.
Any help would be appreciated, thanks in advance.
I was able to solve this by setting up the $PATH variable at the beginning of the shell script. Just added the below line in the shell script,
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet/bin
The Paths mentioned here might be different on other machines, what I did is, I checked the $PATH while calling the shell from Mac machine and copy-pasted and that worked.

Commands in sourced file are not found when executing bash script

My script first sources an API file, the executes a few commands from the file. When the script is executed from the command line, I get "command not found" errors for each of the commands. Here's the script:
#!/bin/bash
## include API file
source /cygdrive/c/path/to/unifi_sh_api
## login
unifi_login
## authorize a client for 30 minutes, limit down/up speed to 2048/1024kbps, quota is 500MB
unifi_authorize_guest "x2:ff:ff:ff:ff:ff" 30 up=1024 down=2048 bytes=500
unifi_logout
This returns "command not found" for the 3 unifi_* commands. There are no other errors.
This is on a Windows server and you can see I'm using Cygwin, so maybe that's part of the problem?
Here's what my sourced file looks like, in case the issue is there: https://dl.ubnt.com/unifi/4.7.6/unifi_sh_api
Any suggestions?
UPDATE:
Definitely using bash.
I did try . instead of source before posting my question here.
Here's the command I'm running in a cmd.exe window:
> C:\cygwin\bin\bash.exe -c '/cygdrive/c/path/to/myscript.sh'

Getting a shell error code from curl in Jenkins while still displaying output in console

I am using a shell script in Jenkins that, at a certain point, uploads a file to a server using curl. I would like to see whatever output curl produces but also check whether it is the output I expect. If it isn't, then I want to set the shell error code to > 0 so that Jenkins knows the script failed.
I first tried using curl -f, but this causes the pipe to be cut as soon as the upload fails and the error output never gets to the client. Then I tried something like this:
curl ...params... | tee /dev/tty | \
xargs -I{} test "Expected output string" = '{}'
This works from a normal SSH shell but in the Jenkins console output I see:
tee: /dev/tty: No such device or address
I'm not sure why this is since I thought Jenkins was communicating with the slave using a normal SSH shell. In any case, the whole xargs + test thing strikes me as a bit of a hack.
Is there a way to accomplish this in Jenkins so that I can see the output and also test whether it matches a specific string?
When Jenkins communicates with slave via SSH, there is no terminal allocated, and so there is no /dev/tty device for that process.
Maybe you can send it to /dev/stderr instead? It will be a terminal in an interactive session and some log file in non-interactive session.
Have you thought about using the Publish over SSH Plugin instead of using curl? Might save you some headache.
If you just copy the file from master to slave there is also a plugin for that, copy to slave Plugin.
Cannot write any comments yet, so I had to post it as an answer.

Resources