Clean Sources Directory – An Azure DevOps Pipeline Extension Task

Update: I have now produced a new extension that cleans up all build directories and not just source, you can find this here.

A while back I wrote about the difficulty managing self-hosted Azure DevOps agents when it comes to disk space. By default when a pipeline runs it leaves the entire cloned git repository behind when it finishes and when you have large git repositories this can start to become a problem. Even after setting maintenance jobs to run every night you can still run into issues and it starts to become an uphill battle.

After not being 100% happy with the maintenance job solution I decided to take a look at writing my own extension task for Azure DevOps Pipelines, the extension would simply delete the content of the sources directory (the directory the git repository is cloned to). This would be set to run as the last task in the pipeline and remove any files that had no use anymore. I’d never written an extension for Azure DevOps before but I have written a fairly popular one for VS Code and it was pretty easy, turns out Azure DevOps extensions are also pretty easy to write. What I came up with was Clean Sources Directory.

Once you’ve installed the extension for the Azure DevOps extension marketplace into your organisation all you need to do is add it to your pipelines. For classic pipelines simply add the ‘Clean Sources Directory’ task:

And for YAML pipelines simply add the following YAML:

- task: [email protected]0
  displayName: 'Cleanup Sources Directory'
  condition: always()

I’d recommend setting the always condition so even if your pipeline fails or you cancel it the sources directory will still be cleaned. That’s all there is to it!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.