How do I configure jenkins to restart the agent for each build?

I tried to use "launch method: launch agent via execution of command on the master" in combintation with "availability: take this agent online when in demand, and offline when idle" but it has two drawbacks:

  1. (not critical) build starts with a delay of about one minute.
  2. (critical) the node goes offline only a minute after the end of the build, and if another build starts at that time, the same still running agent will be used (even if there are several similarly configured free nodes).

There is even a plugin (scripted cloud plugin) that, according to the description, should be suitable, but it does not work, probably because its code is outdated.

  • Curious ... why do you need the agent to be restarted for each build ? At first glance that sounds suspicious to me where you might be trying to solve the wrong problem. But without context it's hard to evaluate. – Newtopian 2 days ago
  • @Newtopian, each build requires a clean image, and each start of agent creates clean image. if the agent does not restart, a dirty image is used. that's the problem. – alexander barakin 2 days ago
  • hmmm... I see... my first reflex here would be to inverse this dependency where the agent would start the image and run the build on it. I'll move the the rest in an answer. – Newtopian 2 days ago

Slave restarts the agent itself after each build? You can use Conditional BuildStep plugin to restart it on a defined condition.

I still dont see how starting an agent creates a clean image. Expanding on your setup would help us provide better answer.

That said, from what I can gather here the following assumption comes to mind :

  • You have agents pre-populated on a VM image. Starting this image will in turn start the agent in which you wish to start the build.

So you now have a chicken vs egg problem where you need the agent running to start the job but you need to start the agent as the job starts.

Why not have another agent who's job is to start the image. This new agent is no longer sensitive to the image cleanliness and can run indefinitely.

My approach here would take this a step further and not have the agent as part of the image altogether, but rather have the agent first start the image, provision it with whatever is needed for the build in some way, and then run the build.

A simple shared folder on the image with the host could do the work. Tools like Vagrant could also be of use. But without more information it's really difficult to go further.

This answer on stack overflow could also contain some help in this regard. Basically spins-up a new docker image on demand.

Another possible way with your current setup, make sure the build always ends with a shutdown of the image. This way the next job will be sure to use a fresh image as it should never survive the build. You could even test fort his situation and fail the build outright if you detect the image was re-cycled between builds rather than re-launched.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.