VSTS find working directory inside a source alias

I wanted to run a custom exe (in my case it was called Warmsole.exe) file as part of my Release Definition using the "Command Line" VSTS task that came out of the box. This task required me to locate the path to the exe.

Turns out it isn't a straight forward task as the artefacts are downloaded to a directory prefixed by a Artifact source alias (in my case was called my-build-definition). I have an artefact called PostDeploy so the full directory would be.

C:\vsts-agents\DeploysPool\agent1\_work\r6\a\my-build-definition\PostDeploy

But how do get the full path above dynamically? VSTS doesn't provide a way for me get a path of artefact by name so I created a handy powershell script which sets a variable containing the value of the directory to used in my later steps in the release definition.

$defaultWorkingDir = $env:SYSTEM_DEFAULTWORKINGDIRECTORY
$postDeployArtefactNm = $env:POSTDEPLOYARTEFACTNAME
$directories = Get-ChildItem .\ -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match $postDeployArtefactNm}
$stats = ($directories | measure)
if($stats.Count -eq 0)
{
    Write-Error ("dir not found")
    exit 1
}
$folder = $directories | Select-object -First 1
$workingDir = $folder.FullName
Write-Host "##vso[task.setvariable variable=PostDeployWorkingDir]$workingDir"

What this script does is dynamically write a value to a VSTS variable (PostDeployWorkingDir in my case) which can then be be later used in later tasks such as my Command Line task.

p37

p38-1

The built in Command Line template in VSTS allows us to specify the working directory under Advanced section in the template. Here simply inject the PostDeployWorkingDir as the value and specify Warmsole.exe as the Tool and we are good to go.

comments powered by Disqus