Search code examples
azure-devopsazure-pipelinesazure-pipelines-yaml

Azure DevOps - Clean build directory


I am using a self-hosted agent for running my build and release pipelines.

A problem happens after I run a build and it fails due to an issue in the pipeline. The cloned branch from remote sits in the working directory. The second run also uses the same working directory, not a new one. I have verified on the agent that no new directory was created. I can also see in the Azure Pipeline logs that it's using the same old directory.

What I did temporarily to solve this was I located the old working directory (e.g. /home/user/_work/13) and manually deleted it. Then Azure creates a new folder (e.g. "14") under _work, and I could see the latest cloned code from the remote in that new directory.

How can I automatically delete the working folder (the numbered directory under _work) when a failure occurs?

Note: I already have put clean-up steps in place at the end of my pipeline which run when the pipeline completes with success. Also, I am writing YAML pipelines, not Classic.

Please let me know if any information is required for a better understanding.


Solution

  • Azure DevOps - Clean build directory

    There is a Clean option on the Get Sources tab, which can perform different kinds of cleaning of the working directory of your private agent before the build is run:

    enter image description here

    We could set the value to true to clean the working directory of your private agent. Even if the build is failed.

    You could check the document Clean the local repo on the agent for some more details.

    Update:

    But this is meant for the classic pipeline, do not we have any tag which we define in yml pipeline only

    jobs:
    - job: string  # name of the job (A-Z, a-z, 0-9, and underscore)
      ...
      workspace:
        clean: outputs | resources | all # what to clean up before the job runs
    

    Check this document YAML schema reference for some details.