How do I make jenkins to capture the output of external source like windows command line [duplicate] - 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.

Related

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.

How can I add a Jenkins post action to execute two different scripts once the build successed or failed exclusively?

I want to execute a shell scripts if the Jenkins job build successful or another scripts if the Jenkins job build failed.
I added the post build task plugin, but it seems only can execute a shell in all status or just successful status, cannot specify another shell script to be run once build failed, the two scripts should be run exclusively.
are there anyone can help me on this?
use Post Build Task plugin for conditional steps
I use Conditional BuildStep Plugin and Text Finder Run Condition Plugin to execute steps when the build fails.
But there's a limitation with this method: You have to find a text that is displayed ONLY when your build fails.
See the pictures below:
I add exit 0 at the end of the script to force the build to be successful so that the program can execute the next step, which is Conditional steps(multiple).
You will see Execute script after a failed build. being displayed on console output when the job fails.
This is the execution result.
You will also need to find a text that is displayed ONLY when the first step succeeds so that you can trigger another script.

In jenkins, execute shell command based on test result

I want to execute a shell command using
jenkins
based on test result. that means,
if the test result is successfull, i will execute the shell script and if not, i quit.
which plugin i have to use???
please save my searching time by giving ur suggestion.

Jenkins: How to terminate Expect script (properly)

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?

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