Query : Folders within Self hosted agent in Azure Devops - azure-devops

I have a build pipeline created using a self hosted agent with source as GIT repo.
Within the build pipeline, I am using Copy files task with the source folder being 1 folder path within the GIT repo and target folder being : $(build.artifactstagingdirectory)/DF/
When I execute the pipeline and I login into the Self hosted agent , I observe the below set of folders:
where folder 2 and 3 are unique for each different pipeline (that is my guess )
And within the folder:
So van someone signify what does each folder which is autogenerated signify?

The number '1' '2'...'6' of work folder c:\work\1, c:\work\2... in your build agent which stands for a particular pipeline.
Agent.BuildDirectory
The local path on the agent where all folders for a given build
pipeline are created. This variable has the same value as
Pipeline.Workspace. For example: /home/vsts/work/1
a folder:
The local path on the agent where any artifacts are copied to before
being pushed to their destination. For example: c:\agent_work\1\a
b folder:
The local path on the agent you can use as an output folder for
compiled binaries.
By default, new build pipelines are not set up to clean this
directory. You can define your build to clean it up on the Repository
tab.
For example: c:\agent_work\1\b
s folder:
The local path on the agent where your source code files are
downloaded. For example: c:\agent_work\1\s
TestResult folder:
The local path on the agent where the test results are created. For
example: c:\agent_work\1\TestResults

Related

Azure build pipeline: build output generates dll json pdb outside the bin folder in artifacts

We are deploying asp.net core application through build pipeline and artifacts it creates have many dll json pdb files outside the bin folder. We want it move it to bin folder and remove unused files. We have tried copy task but its not working.
We are using .Net Core command to publish the project by passing arguments: —configuration $(BuildConfiguration) —output $(build.artifactstagingdirectory)
How do I define copy task to move all the unused and binary files to bin folder.
Thanks.
Azure build pipeline: build output generates dll json pdb outside the bin folder in artifacts
To use the copy task, we should use the predefined variables System.DefaultWorkingDirectory as Source Folder instead of selecting it directly by the Browse Source Folder:
That because the generates files on the agent where we build the pipeline not in the repo.

Azure DevOps - During the build pipeline run, what is the path where the Agent downloads the files locally?

We are using a Microsoft Hosted Agent to run a build pipeline for an automated test scenario for our application. What we would like to achieve is, having our automation procedure, to download a file (from a headless chrome browser), then navigate to the path where it is downloaded and open it.
How could I find the path where the files are being downloaded inside the Agent?
Quick compilation of list of pre-defined variables related to paths for the build on linux and windows self hosted agents from official doc link.
The one you are looking is Agent.BuildDirectory or Pipeline.Workspace.
List of predefined variables:
Variable type
Variable
Description
Example
Agent
Agent.BuildDirectory
The local path on the agent where all folders for a given build pipeline are created
D:\..\agent\_work\1
Agent
Agent.HomeDirectory
The directory the agent is installed into
C:\agent
Agent
Agent.TempDirectory
A temporary folder that is cleaned after each pipeline job
D:\..\agent\_work\_temp
Agent
Agent.ToolsDirectory
The directory used by tasks such as Node Tool Installer and Use Python Version to switch between multiple versions of a tool
D:\..\agent\_work\_tool
Agent
Agent.WorkFolder
The working directory for agent
c:\agent_work
Build
Build.SourcesDirectory
The local path on the agent where your source code files are downloaded.
c:\agent_work\1\s
Build
Build.ArtifactStagingDirectory
The local path on the agent where any artifacts are copied to before being pushed to their destination. A typical way to use this folder is to publish your build artifacts with the Copy files and Publish build artifacts tasks
c:\agent_work\1\a
Build
Build.StagingDirectory
The local path on the agent where any artifacts are copied to before being pushed to their destination.
c:\agent_work\1\a
Build
Build.BinariesDirectory
The local path on the agent you can use as an output folder for compiled binaries
c:\agent_work\1\b
Build
Build.Repository.LocalPath
The local path on the agent where your source code files are downloaded.
c:\agent_work\1\s
Build
Common.TestResultsDirectory
The local path on the agent where the test results are created.
c:\agent_work\1\TestResults
Pipeline
Pipeline.Workspace
The Workspace directory for a particular pipeline
/home/vsts/work/1
System
System.DefaultWorkingDirectory
The local path on the agent where your source code files are downloaded.
c:\agent_work\1\s
When you install the agent, you specify the work directory. In pipeline tasks, you can find out exactly where within that directory files are staged with variables like $(Agent.BuildDirectory). This might not be the exact location you need, but I think it is in the right direction.
For a complete list of predefined variables, see here: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
You can check this document:
So for Linux its default location /home/<username>/Downloads.
I do the test in Microsoft hosted agent-window2019 with test C# code(Hint from Daniel!) like:
static void Main(string[] args)
{
string FILEURI = "https://www.nuget.org/api/v2/package/Cauldron.Newton/2.0.0";
System.Diagnostics.Process prozess = new System.Diagnostics.Process();
prozess.StartInfo.FileName = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
prozess.StartInfo.Arguments = "--download " + FILEURI;
prozess.Start();
Console.WriteLine("Test starts.");
}
And then I use command like dir c:\users\VssAdministrator\cauldron.newton.2.0.0.nupkg /s /b to find the location of downloaded file: cauldron.newton.2.0.0.nupkg.
Then i confirmed the default download location of Chrome is still C:/Users/{user}/Downloads, same as using self-agent or downloading locally. (VssAdministrator is user when run windows hosted agent)
So I think Linux hosted agent should have similar behavior. You can try to find your file from /home/<username>/Downloads folder. Hope it helps.

Dynamic Bin folders hard to copy bin folder to artifact location Azure DevOps

I have a problem with my CI in Azure DevOps.
I need to have a generic PowerShell script run in each CI that copy files from the bin folder and put them to artifact(a) folder with each project folder name.
But bin path are dynamic e.g.
$projectBinDirectory\bin\release\net471\win-x64
$projectBinDirectory\bin\release\net471
$projectBinDirectory\bin\Release\netcoreapp2.1\win-x64
$projectBinDirectory\bin\Release\netcoreapp2.2\win-x64
$projectBinDirectory\bin\Release\netcoreapp2.1
in the future, if someone creates netcore 2.2 or something else how to configure publish path dynamically as I want to copy main project files
e.g.
copy from
bin\release\netcoreapp2.1\win-x64 all project *.dlls,*configs etc files
to
artifact\Projectname\ all project files
the issue
cannot figure out after bin\release\ path to reach to project files
I need to figure out what is the path dynamically after bin\release...\projectFiles
this argument fixed my issue
-c Release -o ./bin/$(BuildConfiguration)/publishNew:

How to get full path to build pipeline

In a custom vsts build task I want to know the path to the current build pipeline so that I can reuse the path to automatically calculate the path to the build drop folder.
In the vsts build portal where I create build pipelines they can be organized into folders. It's this folder path that I need to use in the build task. I can get hold of the name of the build pipeline from variables here: https://learn.microsoft.com/en-us/vsts/pipelines/build/variables?view=vsts&tabs=batch
But, there's no variable for the folder name. How can I get the folder name?
The folder that the build definition lives under is purely a build definition thing -- it has no bearing on a running build, so it's not populated as a build-time variable.
You can retrieve the build definition by making a REST API call:
GET https://{accountName}.visualstudio.com/{project}/_apis/build/definitions/{definitionId}?api-version=4.1. The account URI and build definition ID are available in a running build, and you can easily allow your script access to an OAuth token (populated in the SYSTEM_ACCESSTOKEN environment variable) for authentication purposes.
This will return a JSON object, which contains a path property. That's the path to your build definition.
You must be using an agent for your build pipeline. The agent may be hosted on VSTS or it may be on your premises.
Now, all the tasks in build definitions are executed on agent. Agent is simply a machine. Normally it uses below folders during build process.
Build.Repository.LocalPath or Build.SourcesDirectory or
System.DefaultWorkingDirectory = c:\agent_work\1\s : The local path
on the agent where your source code files are downloaded
Build.ArtifactStagingDirectory or Build.StagingDirectory =
c:\agent_work\1\a: The local path on the agent where any artifacts
are copied to before being pushed to their destination.
So answer your question:
c:\agent_work\1\s here source code is downloaded.
c:\agent_work\1\a here you can copy anything. Build definition uses this path to generate build artifacts at the end of build.

Do I need to import code to VSTS?

I am having my repository at bitbucket, now for CI I am creating build definition on VSTS, please note that due to team constraints, I need to continue bitbucket, so I had configured VSTS to trigger build when changes are made to master branch.
Now for placing files such as .Nuspec files, Powershell Scripts (those I need for build process), do I need to Import code from repository to VSTS? because when I go to Code > Files, it shows Project is empty. Add some code!
It's unnecessary to import files/code into VSTS.
If you need to specify files (such as .nuspec or .ps1 etc) in your build definition, you can specify the files from your bitbucket repo or from the build agent machine.
If the files already managed in your bitbucket repo, you can select the files directly.
If the files are not managed in your bitbucket repo, there are two options you can follow:
Option 1: copy the files into build agent machine
If you do not use Hosted agent for your CI build, you can copy the files into the build agent machine to the directory where the agent machine can access. Such as copy test.ps1 into C:\test\test.ps1 of the agent machine, then you can specify the powershell file with the path C:\test\test.ps1 in your build definition.
Option 2: add the files into your bitbucket repo
You can also commit the files into your bitbucket repo. Then specify the files with relative path. Such as add the file test.ps1 into your bitbucket repo's mytest directory (rootRepo\mytest\test.ps1), then you can specify the powershell with the path mytest\test.ps1 in your build definition.