Azure DevOps Agents and Cleaning Up the Working Folder
Tags: azure
Published: 2023-07-05 14:57:27

In the context of Azure DevOps and Azure Pipelines, an Azure agent refers to a software component that is installed and configured on a machine or a virtual machine. These agents are responsible for executing tasks and jobs defined in your build and release pipelines.

Azure agents can be categorized into two types:

Self-hosted agents:

Self-hosted agents are installed and configured on your own infrastructure, such as physical machines, virtual machines, or containers.
With self-hosted agents, you have more control over the environment and can customize it to meet your specific requirements, including software dependencies, tools, and network configurations.
Self-hosted agents are particularly useful when you have specialized hardware or specific security restrictions that prevent you from using Microsoft-hosted agents.

Microsoft-hosted agents:

Microsoft-hosted agents are provided by Azure DevOps as a service. These agents are maintained and managed by Microsoft.
Microsoft-hosted agents offer a variety of preconfigured environments and tools, including Windows, Linux, and macOS platforms, with a range of software pre-installed.
You can select the appropriate Microsoft-hosted agent pool based on your requirements, and Azure Pipelines will automatically allocate and provision an agent to execute your pipeline jobs.
Microsoft-hosted agents are convenient to use when you don't want to manage the underlying infrastructure or when you need a specific environment readily available.
Both self-hosted agents and Microsoft-hosted agents connect to Azure DevOps and can execute tasks and jobs defined in your pipelines. They communicate with Azure DevOps to receive instructions, download source code, execute tasks, and report back the results.

Azure agents play a crucial role in enabling the automation and execution of build and release processes in Azure Pipelines, allowing you to build, test, and deploy your applications reliably and efficiently.

In the context of Azure Pipelines,some of the predefined variables that represent specific directories used during the build or deployment process are as follows;

$(Agent.HomeDirectory) represents the folder where the agent is installed. This folder contains the code and resources for the agent.
Example: D:\agent

$(Agent.ReleaseDirectory) represents the directory to which artifacts are downloaded during deployment of a release. The directory is cleared before every deployment if it requires artifacts to be downloaded to the agent.
Example: D:\agent\_work\r1\a

$(Agent.RootDirectory) represent the working directory for this agent, where subfolders are created for every build or release.
Example: D:\agent\_work

$(Agent.WorkFolder) represents the working folder for current agent, $(Agent.WorkFolder)\1 represents the working folder for current pipeline.(Normally the first pipeline will be put in $(Agent.WorkFolder)\1, and the second $(Agent.WorkFolder)\2...)

So for one pipeline run, it has four folders by default: a(artifact folder), b(binaries folder), s(source folder) and TestResults(Test results folder). The s folder is where the source code files are downloaded. 

In order to save space we should clean up working folder. Azure retention settings is a way to manage our artifacts and working files. Another way is to define a task and make it clean up unnecessary files periodically.

The task would be as follows;

```
steps:
- task: DeleteFiles@1
  displayName: 'Delete files from $(Agent.WorkFolder)'
  inputs:
    SourceFolder: '$(Agent.WorkFolder)'
```

And the schedule;


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nyv77e4hd0550hhpuc4s.png)