Search code examples
windowsteamcitysmb

How to delete all content from remote folder before uploading files through SMB with TeamCity


I'm running a TeamCity server and agent on a Windows machine. My last step in the build process is to upload the bin/release filed over to a shared Windows folder on another server through SMB.

I need to delete all filed on the remote server before uploading the new build but can't figure out a way to do it.

I don't see any such option in SMB upload runner.


Solution

  • Yes, you are correct that should be added as a step under build steps, I would prefer a powershell command something like this

    robocopy \\%WebServer1%\%SourceFolder% \\%WebServer1%\%DestinationFolder% /E /PURGE /IS /COPY:DT /R:1 /W:2
    RMDir /S "%WebServer1%\%SourceFolder%
    
    Where, 
        /E - Copies sub directories
        /PURGE - Deletes destination files and directories that no longer exist in the source
        /COPY:DT - Specifies the file properties to be copied, in this case it copies Data and Timestamps
        /R:1 - Specifies the number of retries on failed copies, in this case it is 1
        /W:2 - Specifies the wait time between retries, in seconds, in this case it is 2 seconds
        /s - Includes subdirectories
    

    RmDir will remove the source directory once the robocopy is successful.

    If you need to directly remove the files instead of copying and then removing, you could use Move

    Reference for Move - https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/move

    I would personally prefer copy and delete