Copy files (and run tests) on a remote machine back to the VSTS build and release agent

This week I was working on a script, running in my VSTS release pipeline that executed Pester tests on a remote machine. Running Pester tests on the VSTS Build and Release Agent work fine with one the awesome tasks on the Visual Studio Marketplace, but this was about running the tests remotely AND getting the output back in to the pipeline top publish the results.

I did some investigation for different scenarios. When running on a VM in Azure, you can create a networkshare to the files by using the FQDN of the azure machine. something like

\\machinename.westeurope.cloudapp.azure.com. But this only works when you have your SMB port (445) open on the network.

An alternative to this, is to use the WINRM protocol to copy files back to the agent. When you use Powershell 5 you can use the Copy-Item cmdlet and use the -ToSession or -FromSession to copy files

IN my case I created a small script that takes a file on the remote machine and the targetpath on the agent machine as parameter and executes the script.

param
(
[string] $remotemachineFQDN,
[string] $username,
[string] $password,
[string] $sourcePath,
[string] $targetPath
)

$pso = New-PSSessionOption -SkipCACheck
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential
($username, $secpasswd)

$session = New-PSSession -ComputerName $remotemachineFQDN 
                         -UseSSL -SessionOption $pso 
                         -Credential $credentials

Copy-Item $sourcePath -Destination $targetPath -FromSession $session

When you put this in a Powershell task in your VSTS pipeline it works great !

Running Pester tests on remote machine

The copying part is what I wanted to talk about but I can imagine that you are also interested in the running pester tests part. For that I use two VSTS build tasks

AzureFileCopy and Powershell in target machines

tasks

IN the AzureFileCopy I copy the files to a location on the remote machine (c:\tmp)

In the Powershell on Target machine I run a custom Powershell script, to install the pester module and invoke the test. Because the Powershell on Target machine task runs from a different location than what you specify in the deployment param (it does some magic), you need to keep in mind that the working folder is different and some files that you reference are not relative to the script anymore. I created a small invoker.ps1 script that takes some parameters as input.

param
(
 [string] $outputfile,
 [string] $sourcelocation

)
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
install-module pester -Repository PSGallery -Force
invoke-pester -outputfile $outputfile 
              -outputformat NUnitXml 
              -script $sourcelocation

When you run this, and copy the outputfile to the agent, you can run the Publish Test Results task and you are all set !

 

Trackbacks/Pingbacks

  1. TFS/VSTS/ALM/DevOps Ramblings #1 – Mickey Gousset - October 3, 2016

    […] Rene van Osnabrugge discusses how to copy files (and run tests) on a remote machine back to the VSTS build and release agent: […]

%d bloggers like this: