Jenkins: How to terminate Expect script (properly) - jenkins

Wrote an expect script to kickoff a build and it works perfectly. At least, until I started calling it from Jenkins.
I originally wrote the script to use the interaction command, to give control back to the user when the script inside the expect script is finished. Jenkins finishes the script too early, however. (interaction command does not work with Jenkins).
Then I changed the script to use expect eof then wait, however, now Jenkins never terminates the script. It just runs forever.
My script looks like this.
#!/usr/bin/expect
#This script never finishes
send "KICKOFF_OFF_BUILD.ksh LOCATION\r"
expect eof
wait
Is there anyway to change this so Jenkins recognizes the job is finished when the korn shell script inside the expect script above finishes?

Related

Jenkins execute command only if previous one failed

I have the following problem. I have a command that unfortunately only works from time to time in powershell. Sometimes an error comes sometimes it runs through.
Now I am looking for an option in the declarative Jenkins pipeline to execute a step, if this step fails it should execute another command.
However, if the first command runs through, skip the second command, because it is then no longer necessary.
Unfortunately I don't know at all how to implement this.
I have thought about catch error.
I have thought about if else .
Maybe what you need it's already resolved here. I've been working with try/catch because I had same issues of unstable executions and works like a charm.
Try-catch block in Jenkins pipeline script

Gracefully stop a jenkins job

I use jenkins for my countinuous integration testing + possibility of starting manual checks.
My utilization is a job that :
Poll mercurial repo every 10 mn
Once a commit is done, start a generation (clone + make)
Launch a python suite test script
Gather result
Sometimes I want to be able to gracefully stop a job without using the "stop" button that simply aborts the test.
I managed to do it using a trick that check the presence of a file in the log directory used by the Python test suite
But I'm looking for a way to do it inside jenkins job itself.
Is there a way to have a customizable button for that purpose ?
I tried "batch task" plugin that would have been perfect BUT it waits for the python script to complete before execution ... So useless in my case (but the code works)
Thanks in advance for your help

How do I make jenkins to capture the output of external source like windows command line [duplicate]

Stage("execution") {
Steps {
bat 'start cmd.exe /c c:\\users\\doc\\sample.bat'
}
}
The above script is just opening the cmd prompt and executing it. It's not taking the output of the execution. Even if the execution fails, the stage is shown as successful and it moves to the next stage for deployment. I want to develop it so that the output in the cmd prompt should be taken as input by Jenkins and stage should go on. If the stage fails during execution, the stage should show failure and if the execution succeeds, the stage should show success in Jenkins. Can anyone help?
Another question: If the batch file is located in Git, how to give the path in the above script?
Using the start command, you are creating a new detached console process which Jenkins cannot keep track of.
Do it like this instead:
bat 'call "c:\\users\\doc\\sample.bat"'
This runs the batch file in the same environment as the current script and waits for the batch file to end. Jenkins will be able to capture the standard output and detect errors through the exit code of the batch file.
You may write #call to hide the command-line from the output.

How to Direct the Cmd line output to jenkins

Stage("execution") {
Steps {
bat 'start cmd.exe /c c:\\users\\doc\\sample.bat'
}
}
The above script is just opening the cmd prompt and executing it. It's not taking the output of the execution. Even if the execution fails, the stage is shown as successful and it moves to the next stage for deployment. I want to develop it so that the output in the cmd prompt should be taken as input by Jenkins and stage should go on. If the stage fails during execution, the stage should show failure and if the execution succeeds, the stage should show success in Jenkins. Can anyone help?
Another question: If the batch file is located in Git, how to give the path in the above script?
Using the start command, you are creating a new detached console process which Jenkins cannot keep track of.
Do it like this instead:
bat 'call "c:\\users\\doc\\sample.bat"'
This runs the batch file in the same environment as the current script and waits for the batch file to end. Jenkins will be able to capture the standard output and detect errors through the exit code of the batch file.
You may write #call to hide the command-line from the output.

Jenkins logs for a perl build file

Today I started working with jenkins and I successfully added my projects to jenkins and it says all works fine . one of the build takes more than 5 hours but didn't finish either so aborted it(while manual build takes less than 1 hour).. and while i tried to check with the log the log was not detailed. so i tried to get the logs of the perl script by running it as a shell command
usr/local/bin/perl perlscript.pl>logfile.txt
there was no log written and there was no evidence of the build triggered either cases.i'm not aware of what the problem is as both the perlscript(works fine while manually triggered) and jenkins are working properly except this project. I would like to have your help.thanks in advance
A few things you should understand about Jenkins:
Jenkins shows STDOUT as the log of the Job,
so if you redirect it to a file - nothing will be shown in the log.
Depending on how you have set it up, Jenkins may run as its own user,
which may change the behavior of your scripts.
You can confirm this by echo-ing the username at the beginning of your Execute Shell block,
for example:
echo $USER
Each Jenkins-Job is run from its own workspace -
you can confirm that location by simply printing the current working directory
at the beginning of your Execute Shell block, for example:
echo my current directory is
pwd

Resources