tfstfs-2017

TFS get build agent working folder programmatically


Is there any way to programmatically retrieve the working folder of a build agent (through TFS REST API)?

I'd like to clean this folder during the reboot of the build agent.


Solution

  • There isn't the REST API to retrieve the working folder of a build agent.

    The working folder is defined when your deploy the build agent. Commonly it should be like this D:\VSOAgent_work (the location which you deployed the agent to).

    To clean the folder during the reboot of the build agent, you just need to create a .cmd/.bat file to delete the _work directory, and copy it into the start up folder in Windows Explorer.

    Please note that you need to run the cmd/bat with administrator.

    Delete command sample: (Just change the dir accordingly, the command will delete all the files and sub-folders, but not delete the directory _work)

    @ECHO OFF
    
    Set dir=D:\VSOAgent\_work\
    
    Echo Deleting all files from %dir%
    del %dir%\* /F /Q
    
    Echo Deleting all folders from %dir%
    for /d %%p in (%dir%\*) Do rd /Q /S "%%p"
    @echo Folder deleted.
    
    
    exit
    

    To find the Startup folder:

    For your user account : Win + R run shell:startup
    For all users Win + R run shell:common startup

    For how to auto start Programs as Administrator, you can reference this article: http://www.thewindowsclub.com/autostart-programs-windows-10-make