How to detect an agent is running a build?
I am looking for a clean and consistent way to detect if an agent is running a build.
We have another system which has been setup to perform upgrades of software and the operating system. We would prefer to update the agent when its between builds.
In addition we are looking for the best practice why to disable the agent then re-enable the agent after the updates have been performed.
Please sign in to leave a comment.
Hi Jason,
you can use a GET call to the REST "agents" endpoint to try to collect this information. For example:
<url>/app/rest/agents/id:<the_id_of_the_agent>?fields=build will return you whether the agent with the ID you passed as a parameter has currently any running build. It also contains some information about completion that might be useful if you want to wait for it to complete before proceeding.
It should be noted that there might be a few race conditions in here where you check this, see no build running, then stop the agent but in the meantime the agent had picked up a new build.
In terms of best practices, you would probably be looking at programming in advance when the updates are going to happen, disable the build agents, wait for the current build to finish, then stop the agent, run the updates, and restart-enable again. Another good practice is to simply use Containers or VM images which you can control on demand. Then you simply update the images as needed and replace the image so that the new one can be used in future builds. That should allow to finish the current build and start new ones without any disruption.