Understanding the directory structure created by Azure DevOps tasks.
Tags: azure, devops
Published: 2020-09-29 23:35:52

As a beginner in all things Azure DevOps, understanding when folders are created and populated by the pipeline tasks is the first step to learning how to manipulate it to suit your needs. 

#### The YAML
Using Visual Studio I created an ASP.NET based project (named TestApplication). The YAML file shown below was the template created by Azure DevOps, following [this](https://docs.microsoft.com/en-us/azure/devops/pipelines/create-first-pipeline?view=azure-devops&tabs=net%2Cyaml%2Cbrowser%2Ctfs-2018-2) tutorial. However, the *VSBuild@1* task was modified using [this] (https://stackoverflow.com/questions/50495038/msbuild-arguments-to-generate-files), as it resulted in a more suitable folder structure. I also added a *PublishBuildArtifacts@1*.

```
trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/TestApplication.sln'
  buildPlatform: 'ANY CPU' # x86
  buildConfiguration: 'Release'
  buildversion: 1.0.0

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(build.artifactStagingDirectory)'
    ArtifactName: '$(buildversion)'
    publishLocation: 'Container'
```
I ran this YAML file multiple times using PowerShell tasks to display the environment variables at different stages of the pipeline.

#### Overall look at the directory structure
This image shows a big-picture look at the directory structure after one complete run of the pipeline.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lgl3rixyjoa8atfxc505.jpg)

#### Summary of when each directory is used
Most of this information, though publicly available on the Microsoft [docs](https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml), is not very intuitive to a beginner. I hope that this table summarizes the usage of the most relevant directories.

| Directory | Uses | References |
|-----------|------|---|
| /a        | This is the working director for agent _a_.  | Agent.WorkFolder   |
| /a/1/a    | Artifact staging directory. This is where the VS Build task results are stored in. The publish build artifacts task creates an artifact of whatever is in this folder. Note - it gets purged before each new build.     |  Build.StagingDirectory, System.ArtifactsDirectory, Build.ArtifactStagingDirectory  |
| /a/1/b    | The output folder for compiled binaries. | Build.BinariesDirectory  |
| /a/1/s    | Source directory. This is the working directory and where your source code is stored.     | Build.SourcesDirectory, System.DefaultWorkingDirectory  |

#### Conclusion
This information was very helpful to me when it came down to customizing a YAML for my project. I wasn't able to find any existing documentation or articles like this, so please feel free to add links in the comments to similar articles if they exist.
:)