If it is the last stage in your Jenkinsfile, (like my case), you can use BUILD_ID=dontKillMe
to solve that problem, like this:
stage("Run"){
steps{
withEnv(['BUILD_ID=dontKillMe']) {
script{
sh '<YOUR COMMAND HERE>'
}
}
}
}
In my case, I have Jenkins in Ubuntu, and I want to run the last stage of my jenkinsfile forever, so I used :
stage("Run app service forever"){
steps{
withEnv(['BUILD_ID=dontKillMe']) {
script{
kubeconfig(credentialsId: 'my_minikube_config_file', serverUrl: 'https://###.###.###.###:8443') {
sh 'kubectl port-forward --address 0.0.0.0 services/tecapp 5000:5000'
}
}
}
}
}
NB: DON'T add & at the end of your command, like this :
stage("Run"){
steps{
withEnv(['BUILD_ID=dontKillMe']) {
script{
sh '<YOUR COMMAND HERE> &'
}
}
}
}
when I used &, the stage didn't run forever.