Azure DevOps – Tips and Tricks – 15 – Working with the Magic folders a, b and s in the Azure DevOps Agents

In this article, we are going to learn about the folders inside the agent that we can use for various scenarios. It’s essential to learn about this folder for building better pipelines.

Introduction

When authoring Azure DevOps Pipelines, you need to understand the default folder structure that the agent maintains and configures.

Azure DevOps Agent supports the below Operating Systems.

  1. Windows
  2. Linux / Ubuntu
  3. macOS
Advertisements

In this article, we will focus on Windows and Ubuntu based agents.

Let’s try to understand the folder structure by creating a very simple YAML based Azure DevOps Pipeline and add the Tasks based on the below instructions.

Windows based Agent

We are going to list all the folders that are created for a given pipeline. It’s called as Workspace and so the local folder path can be referred using a Pre-defined variable called $(Pipeline.Workspace)

Let’s add the below PowerShell Task that prints the folder structure inside the $(Pipeline.WorkSpace)

Advertisements
- task: PowerShell@2
  displayName: List all Folders in $(Pipeline.Workspace) 
  inputs:
    targetType: 'inline'
    pwsh: true
    script: |
      Get-ChildItem -path $(Pipeline.Workspace)

Once you run the pipeline, you will be able to view the folders named a,b,s and Test Results available in the workspace as shown below.

Azure DevOps – Tips and Tricks – 15 – Working with the Magic folders a,b and s in the Azure DevOps Agents – View Folders

Let’s now understand the significance of these folders.

Folder Name: a
Referred using: $(Build.ArtifactStagingDirectory)
Description:

Artifact Staging Directory is a pre-defined variable which is usually used in Build Pipelines for storing the output (referred as artifacts) that is produced as part of the Build process for any technology (.net, java, python etc) or it could be as simple as copy files.

Advertisements

Folder Name: b
Referred using: $(Build.BinariesDirectory)
Description:

Binaries directory is a pre-defined variable used for storing the binaries that are created as part of compilation process.

Note: In my personal experience, most of the time, I have seen people using $(Build.ArtifactStagingDirectory) for storing the binaries too instead of $(Build.BinariesDirectory).

Folder Name: s
Referred using: $(System.DefaultWorkingDirectory)
Description:

Default working directory is a pre-defined variable that is mostly used to store the source code of the application. $(System.DefaultWorkingDirectory) is used automatically by the checkout step which download the code automatically as shown below.

Azure DevOps – Tips and Tricks – 15 – Working with the Magic folders a,b and s in the Azure DevOps Agents – Checkout

Let’s now modify the code to view the files and folders inside the $(System.DefaultWorkingDirectory) recursively. Please note the flag -recurse added to the Get-ChildItem command in the below code.

Advertisements
- task: PowerShell@2
  displayName: List all Folders in $(System.DefaultWorkingDirectory)
  inputs:
    targetType: 'inline'
    pwsh: true
    script: |
      Get-ChildItem -path $(System.DefaultWorkingDirectory) -recurse

Once you run the pipeline, the recursively displays the contents of all the folders as shown below.

Azure DevOps – Tips and Tricks – 15 – Working with the Magic folders a,b and s in the Azure DevOps Agents – s contents
Advertisements

Folder Name: TestResults
Referred using: $(Common.TestResultsDirectory)             
Description:

Test results Directory contains the local directory of the agent which could be used for storing the Test Results.

Below is the complete code for Windows agent

pool:
  vmImage: windows-latest
steps:
- task: PowerShell@2
  displayName: List all Folders in $(System.DefaultWorkingDirectory)
  inputs:
    targetType: 'inline'
    pwsh: true
    script: |
      Get-ChildItem -path $(System.DefaultWorkingDirectory) -recurse 

Advertisements

Linux Based Agent

Let’s now change our pipeline code to refer ubuntu based agents as shown below. Please also note that the folder structure remains same even in the Linux based agent. And, it’s also important to understand that all the pre-defined variables are applicable in Linux agents too.

pool:
  vmImage: windows-latest

The PowerShell task would still work but it may not provide you the appropriate details (especially the names). So, Let’s now write a Bash script and use the tree command as shown below to view all the contents of the $(System.DefaultWorkingDirectory)

- task: Bash@3
  displayName: List all Folders in $(System.DefaultWorkingDirectory)
  inputs:
    targetType: 'inline'
    script: |
      tree $(System.DefaultWorkingDirectory)

And, here is the output

Azure DevOps – Tips and Tricks – 15 – Working with the Magic folders a,b and s in the Azure DevOps Agents – s contents
Advertisements

Summary

In this article, we have learnt about the folders inside both the Window and Linux based agents, learnt about the Pre-defined variable which could be used to refer those folders that we can use for various scenarios to build better pipelines.

Do you like this article? If you want to get more updates about these kind of articles, you can join my Learning Groups

WhatsApp

Telegram

Advertisements
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s