Jenkins pipeline timeout - jenkins

I have a stage in Jenkins pipeline(declarative) on my windows machine.
There is a script that runs forever. However i need logs and have to ensure it doesnt break in between so i cannot run it in background.
Bascially i am looking for if there is not logs populating in jenkins for sometime it should proceed to next stage with "SUCCESS" status of that stage.
I have used time option, however it is marking the job as failure and successive stage wont run and job gets aborted.
timeout(time: 150, unit: 'SECONDS', activity: true)
Any way if i can mark the status of stage to success after so and so duration.
Thanks,

You can try to play with try\catch block and currentBuild global variable, for example (tested):
try {
stage("UNSTABLE"){
timeout(time: 20, unit: 'MINUTES') {
//do something
echo "UNSTABLE stage"
currentBuild.result = "UNSTABLE"
}
}
stage("SUCCESS"){
timeout(time: 20, unit: 'MINUTES') {
//do something
echo "SUCCESS stage"
currentBuild.result = "SUCCESS"
}
}
stage("FAILURE"){
timeout(time: 20, unit: 'MINUTES') {
//do something
echo "FAILURE stage"
//currentBuild.result = "FAILURE" //this will fail all the pipeline
}
}
} catch (err) {
echo err
currentBuild.result = "SUCCESS"
//do job
} finally {
currentBuild.result = "SUCCESS"
//do job
}
From pipeline-syntax docs (link to access this on your local jenkins - http://localhost:8080/pipeline-syntax/globals#currentBuild):
The currentBuild variable may be used to refer to the currently
running build. It has the following readable properties:
result -
typically SUCCESS, UNSTABLE, or FAILURE (may be null for an ongoing
build)
currentResult - typically SUCCESS, UNSTABLE, or FAILURE. Will
never be null.
Also see: How to manipulate the build result of a Jenkins pipeline job?

I was trying to run npm start that runs forever
However i got to know there was alternate command
npm build
that fixed my issue. So i dont need to include try catch block and time out.

Related

Force complete Jenkins job

I am running Jenkins pipeline script.
From this pipeline script we are running one script which keeps on producing output,so this process does not end.
So because of this my Jenkins job is not getting completed but as all the steps completed here i somehow want to mark this job complete after say 10 mins.
Is there any way to complete jenkins job from pipeline scipt after say 10 mins.
Below is my pipeline script and runspbt.sh is that never ending script.
pipeline {
agent {label 'Executionmachine3089'}
stages {
stage('Run Script') {
steps {
bat "ssh rxx11pp#G0XXXX209 /home/rxx11pp/runspbt.sh"
}
}
}}
you can wait and check for some success output (or maybe a marker file in the workspace) .
steps{
sh "sleep 600s"
script{
if(testSuccessMethod){
currentBuild.result = "SUCCESS"
return
}else{currentBuild.result = "FAILURE"
return
}
}
You may want to use linux's timeout command:
pipeline {
agent { label 'Executionmachine3089' }
stages {
stage('Run Script') {
steps {
bat "ssh rxx11pp#G0XXXX209 timeout 90 /home/rxx11pp/runspbt.sh"
}
}
}
}
This will start the script and signal it to exit after 90 seconds.
Note this is not related to Jenkins.

How to make build timeout in jenkins pipeline groovy script?

I saw declarative timeout example, e.g.
options{
timeout(100)
}
but it cannot work in my groovy file for jenkins pipeline. Also, I cannot find more documentation about this API on jenkins site.
You can put the timeout inside the stage.
stage ("deploy") {
steps {
script {
try {
timeout(time: 180, unit: 'SECONDS') {
sh("RUN COMMAND")
}
}
catch (err) {
echo "Timeout."
}
}
}
}
If you don't use try/catch, a timeout would cause a failure of this build, then your build will be interrupted on this stage.

How to kill a stage of Jenkins Pipeline?

I've a Jenkins pipeline with multiple stages but because of some issue, in one of the stages, it is likely to run longer unnecessarily.
Instead of aborting entire pipeline build and skip next stages, I want to kill that specific stage on which other stages are not dependent.
Is there a way to kill specific stage of Jenkins pipeline?
There are ways to skip a stage. But I'm not sure if there are options to kill a long running stage. I'd simply add a conditional expression to run the stage or not OR maybe you could put a timeout condition wrapped in a try..catch block for the long running unnecessary stage to skip and proceed to other stages you want like as below.
pipeline {
agent any
stages {
stage('stage1') {
steps {
script {
try {
timeout(time: 2, unit: 'NANOSECONDS')
echo "do your stuff"
} catch (Exception e) {
echo "Ended the never ending stage and proceeding to the next stage"
}
}
}
}
stage('stage2') {
steps {
script {
echo "Hi Stage2"
}
}
}
}
}
OR Check this page for conditional step/stage.
You can try using "try/catch" block in the scripted pipeline. Even if there is error in a particular stage, Jenkins will continue to execute the next stage.
node {
stage('Example') {
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}
You can refer documentation here: https://jenkins.io/doc/book/pipeline/syntax/

setting status of a build in jenkins

I have a jenkins job.
It is pretty simple: pull from git, and run the build.
The build is just one step:
Execute window command batch
In my use case, I will need to run some python scripts.
Some will fail, some others will not.
python a.py
python b.py
What does determine the final status of the build?
It seems I can edit that by:
echo #STABLE > build.proprieties
but how are the STABLE/UNSTABLE status assigned if not specified by the user?
What happens if b.py raise an error and fails?
Jenkins interprets a pipeline as failed if a command returns an exit code unequal zero.
Internally the build status is set with currentBuild.currentResult which can have three values: SUCCESS, UNSTABLE, or FAILURE.
If you want to control the failure / success of your pipeline yourself you can catch exceptions / exit codes and manually set the value for currentBuild.currentResult. Plugins also use this attribute to change the result of the pipeline.
For example:
stage {
steps {
script {
try {
sh "exit 1" // will fail the pipeline
sh "exit 0" // would be marked as passed
currentBuild.currentResult = 'SUCCESS'
} catch (Exception e) {
currentBuild.currentResult = 'FAILURE'
// or currentBuild.currentResult = 'UNSTABLE'
}
}
}}

Scripted Jenkins pipeline: continue on fail

Is there a way to continue execution of the scripted pipeline even if the previous stage failed? I need to run specific commands (cleanup) when the build fails before the whole job fails.
The accepted answer wouldn't fail the stage or even mark it as unstable. It is now possible to fail a stage, continue the execution of the pipeline and choose the result of the build:
pipeline {
agent any
stages {
stage('1') {
steps {
sh 'exit 0'
}
}
stage('2') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "exit 1"
}
}
}
stage('3') {
steps {
sh 'exit 0'
}
}
}
}
In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:
As you might have guessed, you can freely choose the buildResult and stageResult, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.
Just make sure your Jenkins is up to date, since this is a fairly new feature.
The usual approach is to wrap your steps within a try block.
try {
sh "..."
} catch (err) {
echo "something failed"
}
// cleanup
sh "rm -rf *"
To ease the pain and make the pipeline code more readable, I've encapsulated this in another method here in my global library code.
Another approach, esp. created because of this very issue, are the declarative pipelines (blog, presentation).
post {
always {
cleanWs()
}
}
}
Will always cleanup the job even if the rest fails

Resources