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.
Related
I have a Jenkinsfile:
pipeline {
agent any
stages {
stage('Install dependencies') {
steps {
sh 'yarn'
}
}
}
}
How can I execute the Java garbage collector in the pipeline script?
This related question and answer hints at the monitoring plugin but you still have to manually click with it.
Run the garbage collector in a pipeline script:
pipeline {
agent any
stages {
stage('Run garbage collector') {
steps {
script {
System.gc();
}
}
}
stage('Install dependencies') {
steps {
sh 'yarn'
}
}
}
}
On first execution, this job will fail. In the console output you will find an error:
Scripts not permitted to use staticMethod java.lang.System gc. Administrators can decide whether to approve or reject this signature.
You can approve the script signature here: https://jenkins.YOUR_URL.com/scriptApproval/
allow java.lang.System gc
Solved when I found this issue.
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.
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/
I want to run Jenkins scripted pipeline job at the specified slave at the moment when no other job is running on it.
After my job will be started, no other jobs should be performed on this slave, they will have to wait for my job ending running
All the tutorials that I found allowed me to run job on the node when it is free but did not protect me from launching other jobs on this node
Could you tell me how can I do this?
Because Pipeline has two Syntax, there are two ways to achieve that. For scripted pipeline please check the second one.
Declarative
pipeline {
agent none
stages {
stage('Build') {
agent { label 'slave-node' }
steps {
echo 'Building..'
sh '''
'''
}
}
}
post {
success {
echo 'This will run only if successful'
}
}
}
Scripted
node('your-node') {
try {
stage 'Build'
node('build-run-on-this-node') {
sh ""
}
} catch(Exception e) {
throw e
}
}
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.