How to get full path to build pipeline - azure-devops

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.

Related

How do I use an Azure DevOps Services Build Pipeline to retrieve TFVC source files based upon a Label value and then zip those files?

This is a TFVC repo in Azure, not Git. It is running in Azure DevOps Services, not local in Azure DevOps Server (2019). This is a classic pipeline, not YAML.
I got as far as adding a variable that contains the Label value I am looking to package into the zip file.
I can't figure out how to get the sources by Label value. In the Pipeline Get Sources step, I've narrowed the path down, but then I need to recursively get source files that have the Label in the variable I defined.
The next step is to zip those source files up, I've added an Archive task to which I will change the root folder from "build binaries" to the sources folder.
This is necessary for this particular project because we must pass the source files to the vendor as a zip for them to compile and install for us. The developers create/update the source files, build and test them locally, then apply a Label to the sources for a given push to the vendor.
When configuring 'Get sources' step, there is no any option or method that can only map the source files with the specified label.
As a workaround, in the pipeline job, you can try to add the steps to filter out the source files with the specified label, and use the Copy Files task to copy these files to a folder, then use the Archive Files task in this folder.
[UPDATE]
Normally, a pipeline run will automatically check out the file version (changeset) that triggers the run. If manually trigger the the pipeline, by default the run will check out the latest changeset if you do not specify one.
The labels are used to mark a version of a files or folders, so you also can get the specific version of files or folders via the labels.
In your case, you can try using the 'tf get' command to download the files with the specified labels.

Azure devops build pipeline

I have created a sample Website project with a single page having HellowWord.aspx and HellowWord.aspx.cs. I am trying to create an Azure DevOps build pipeline for this project. The following are the tasks I have added to my build package.
But the publish artifacts always contains the aspx and aspx.cs file. Not sure which tasks I am supposed to add to make the proper publish package. Which will create proper dlls for .aspx file instead of aspx.cs.
Publish build artifacts task has an argument Path to publish, which defines the folder or file path to publish. This can be a fully-qualified path or a path relative to the root of the repository. Wildcards are not supported. Variables are supported. Example: $(Build.ArtifactStagingDirectory). By default, this argument uses variable $(Build.ArtifactStagingDirectory).
So you need to check your Copy files task, to see what you have copied to $(Build.ArtifactStagingDirectory), and copy the correct files in this task.
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/copy-files?view=azure-devops&tabs=yaml

azure devops: copy file to artifact folder to access in release pipeline

I am creating a build for ios using azure and I want to copy a certificate file to artifact folder created to use in the release pipeline.
I am using a copy task and after the task is done its job, I am not able to find a copied file at the desired artifact folder. there are answers for copy task but not working for my scenario.
or is there any way to access files from the source folder of build pipeline to from release pipeline?
You were using download secure files task to download the cert files. As below described: see here for more information.
Once downloaded, use the name value that is set on the task (or "Reference name" in the classic editor) to reference the path to the secure file on the agent machine. For example, if the task is given the name mySecureFile, its path can be referenced in the pipeline as $(mySecureFile.secureFilePath). Alternatively, downloaded secure files can be found in the directory given by $(Agent.TempDirectory)
The secure files are downloaded to directory $(Agent.TempDirectory) and its path can be referenced by $(mySecureFile.secureFilePath) if you set the reference name to mySecureFile
So that the copy files task of yours need to be configured as below: Set the Source Folder to $(Agent.TempDirectory) and Contents to the name of the secure files. See below example:
You can also set the Contents to $(certFile.secureFilePath) $(provisionFile.secureFilePath) if you set the Reference name of above download secure file tasks to certFile and provisionFile
More conveniently you can use download secure files task directly in release pipeline to download the cert files.
Then the secure files will be available in release pipeline, and can be referenced just like in the build pipeline.

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.

Unique Need - Perform Team Services releases using artifacts created in an external build

I have a unique need where I need to perform releases from Team Services using a Release Pipeline and artifacts that have been created in a previous external build. I have the artifacts that were created, dacpacs and websites ect.
I would like to deploy these items using the features in release Pipelines but artifact sources only come from a build or some other version control.
My approach (hack) was to use a build to copy the external files and publish them into the artifact container for the build. I could then use the release pipelines to do my releases. But .. Build copy tasks only seem to work with paths into a repo.
My fall back will be to use the release pipeline and powershell to do the releases with these externally created artifacts. I would sure like to avoid this since there is nice capability in the release pipeline tasks.
This is a compliance requirement my firm has which results in the rather crazy post.
Any help would really be appreciated.
You can use Copy Files task and Publish Build Artifacts task for your build definition.
Copy Files task
Source Folder: you can specify the folder which has your external build artifacts. Such as C:\project\a.
Contents: you can use wildcards to specify which files to copy. Such as **\*.dll, this will copy all *.dll files in C:\project\a and it’s subfilder.
Target Folder: where you want to copy these files. Usually it’s $(build.artifactstagingdirectory).
Publish Build Artifacts task
Path to Publish: set as the same with Target folder in Copy files task. Such as $(build.artifactstagingdirectory).
Note: Copy files task will find the source folder in the machine where the private agent is located.