Azure Pipelines predefined variables and directory structure
Published: 2022-01-03 14:20:41

# Summary
In this article, I would like to share my learnings about the directory structure of [Azure pipeline Microsoft-hosted agents](https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml). [This code sample](https://github.com/koheikawata/network-sample-app) includes the ready-to-use Azure pipelines with `windows-2019` and `windows-2022`.

# TOC
- [Predefined variables](#predefined-variables)
- [.NET Restore, Build, Publish](#dotnet)

# Predefined variables <a name="predefined-variables"></a>
Azure Pipelines have [predefined variables](https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml) which you can use on your pipelines. There are different directory variables, some of which overlap, some of them are under a different partition. I am trying to describe those predefined variables below.

**windows-2019 (Microsoft-hosted agent)**

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

- `Agent.ToolsDirectory` or `Agent.TempDirectory` are used for different versions of tools. `Agent.ToolsDirectory` is under `C:` and then tools are cached and not cleaned up across the pipeline runs. `Agent.TempDirectory` is under `D:` and then cleaned up after a pipeline run. [The code example below](https://github.com/koheikawata/network-sample-app/blob/main/azure-devops/dotnet/dotnet-pipeline-travelapisql.yml) lets the pipeline use the cached versions of .NET 6 tools. 

```
- task: UseDotNet@2
  displayName: Use .NET 6 sdk
  inputs:
    packageType: sdk
    version: 6.0.x
    installationPath: $(Agent.ToolsDirectory)/dotnet
```

- `Agent.BuildDirectory` and `Pipeline.Workspace` are at `D:\a\1`which is a workspace directory for a particular pipeline.
- `Build.StagingDirectory` and `Build.ArtifactStagingDirectory` are at `D:\a\1\a`, which is used for where build artifacts are copied before being pushed.
- `Build.BinariesDirectory` is at `D:\a\1\b`, which is used for compiled binaries.
- `Build.SourcesDirectory` and `System.DefaultWorkingDirectory` are at `D:\a\1\a`, which are used for your source code files.

# .NET Restore, Build, Publish <a name="dotnet"></a>
## dotnet restore

`dotnet restore` creates `obj` folder and includes some of files regarding Nuget packages.

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

## dotnet build
`dotnet build` outputs dll files and exe files under `bin` and `obj`.

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

## dotnet publish
`dotnet publish` outputs dll files and exe files under a pointed folder. For example, [the code example](https://github.com/koheikawata/network-sample-app/blob/main/azure-devops/dotnet/dotnet-pipeline-travelapisql.yml) outputs files under `publish` folder and then those files are zipped. In this case, the project name is `TravelApi` and the zip file name is `TravelApi.zip`, which is used for Azure App Service deployment. The difference from `dotnet build` is `TravelApi.zip` includes `web.config` in addition to the build files.

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