Sometimes you might want a cleanly started machine (not cleanly installed, mind you) for your pipeline job. For instance, if you are running UI tests. In some situations UI tests are very brittle and might be affected by a canceled or failed previous run. Is these circumstances, restarting the agent automatically after each job can be beneficial. This is now possible with the introduction of the
--once parameter of the agent (more info here).
Start off by installing your agent as usual, and be sure to make it an interactive agent. Don’t forget to configure it to autologon, since automatically rebooting without that feature would stop our agent in its tracks rather fast. After you have done this you can add a custom cmd (you could name it customrun.cmd for instance) file to the root directory of the agent with the following contents:
call "C:\agent\run.cmd" --startuptype autostartup --once shutdown /r /t 0 /f
If you run this file, the agent will start and the
--once parameter will force it to close after the first job is finished. The shutdown command will then immediately restart the machine.
To have this file run during autologon instead of the default generated run command. You need to edit the registry as well. Start your registry editor and search for the following key:
Change the contents to something like the following (be sure to put in the full path to your own custom cmd file obviously):
C:\windows\system32\cmd.exe /D /S /C start "Agent with AutoLogon" "C:\agent\customrun.cmd"