Re-run scripted pipeline when agent loses connection with master

13 views
Skip to first unread message

Nahue

unread,
Jun 8, 2020, 1:29:57 PM6/8/20
to Jenkins Users
I'm trying to configure something that its harder than I thought.

There is a possibility that I can lose an agent in the middle of the pipeline (this is not a problem, is expected due my k8s configuration, its not ofen but can happen) so I would like to re-run the pipeline when this happens.

Due is a pipeline Naginator and Periodic Reincarnation plugin will not work, they work with freestyle projects, not with pipelines.

So, what thought was add a post step that will re-run the pipeline if notices that the problem was agent's disconnetion.

I've noticed that, when I kill the agent, the pipeline aborts so this works:

#!/usr/bin/env groovy

pipeline {
  agent {
    label 'agent1'
  }
  stages {
    stage('Verify template') {
      steps {
        container('container1') {
          script{
            sh 'uname -a'
            sh 'sleep 600'
            sh 'date'
          }
        }
      }
    }
  }
  post {
    aborted { 
      build job: "$env.JOB_NAME", wait: false
    }
  }
}


Looks ok, works as expected but also is re-running the pipeline if I abort the pipeline manually (which is expected too).

Other think I noticed is that always is showing Agent was removed in the pipeline's output log, so if I can analyze the output, I would be able to just re-run when I see that message and here is the problem, I don't find a way to read the log from post.

Do you have some clue how to do it? There is a better way?

Thanks!
Reply all
Reply to author
Forward
0 new messages