From c97fc47fd5742b1008920b8adc7a0bf64fb984ef Mon Sep 17 00:00:00 2001 From: Tristan Barcelon Date: Wed, 2 Aug 2023 22:28:47 -0400 Subject: [PATCH 1/2] Add support for specifying which installing pipelines-agent binaries, but default to installing vsts-agent binaries as it did prior --- VSTSAgent/VSTSAgent.psm1 | 44 ++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/VSTSAgent/VSTSAgent.psm1 b/VSTSAgent/VSTSAgent.psm1 index ea387bd..da8a90d 100644 --- a/VSTSAgent/VSTSAgent.psm1 +++ b/VSTSAgent/VSTSAgent.psm1 @@ -91,6 +91,8 @@ function Get-Platform { Find the latest available agent version. .PARAMETER Platform The platform required for the agent. +.PARAMETER AgentType + Install Node6-based vsts-agent or Node10-based pipelines-agent asset #> function Find-VSTSAgent { [CmdletBinding( DefaultParameterSetName = "NoVersion")] @@ -111,22 +113,24 @@ function Find-VSTSAgent { [switch]$Latest, [parameter(Mandatory = $false)] - [string]$Platform + [string]$Platform = 'win', + + [parameter(Mandatory = $false)] + [ValidateSet('vsts-agent','pipelines-agent')] + [string]$AgentType = 'vsts-agent' ) - if ( $Latest ) { + $rootUri = [uri]"https://github.com" - $findArgs = @{ } - if ( $Platform ) { $findArgs['Platform'] = $Platform } - $sortedAgents = Find-VSTSAgent @findArgs | Sort-Object -Descending -Property Version - $sortedAgents | Where-Object { $_.Version -eq $sortedAgents[0].Version } - return + if ( $Latest ) { + $releasesRelativeUri = [uri]"/Microsoft/vsts-agent/releases/latest" + } + else + { + $releasesRelativeUri = [uri]"/Microsoft/vsts-agent/releases" } Set-SecurityProtocol - - $rootUri = [uri]"https://github.com" - $releasesRelativeUri = [uri]"/Microsoft/vsts-agent/releases" $page = [uri]::new( $rootUri, $releasesRelativeUri ) $queriedPages = @() @@ -134,11 +138,11 @@ function Find-VSTSAgent { do { $result = Invoke-WebRequest $page -UseBasicParsing - $result.Links.href | Where-Object { $_ -match "vsts-agent-(\w+)-x64-(\d+\.\d+\.\d+)\..+$" } | ForEach-Object { + $result.Links.href | Where-Object { $_ -match "$($AgentType)-$($Platform)-x64-(\d+\.\d+\.\d+)\..+$" } | ForEach-Object { $instance = [PSCustomObject] @{ - 'Platform' = $Matches[1] - 'Version' = [VSTSAgentVersion]$Matches[2] + 'Platform' = $Platform + 'Version' = [VSTSAgentVersion]$Matches[1] 'Uri' = [uri]::new($_, [System.UriKind]::RelativeOrAbsolute) } @@ -148,8 +152,7 @@ function Find-VSTSAgent { if ( $RequiredVersion -and $instance.Version -ne $RequiredVersion) { return } if ( $MinimumVersion -and $instance.Version -lt $MinimumVersion) { return } if ( $MaximumVersion -and $instance.Version -gt $MaximumVersion) { return } - if ( $Platform -and $instance.Platform -ne $Platform) { return } - + Write-Verbose "Found agent at $($instance.Uri)" Write-Output $instance } @@ -194,6 +197,8 @@ function Find-VSTSAgent { What user credentials should be used by the agent service? .PARAMETER Cache Where should agent downloads be cached? +.PARAMETER AgentType + Install Node6-based vsts-agent or Node10-based pipelines-agent asset #> function Install-VSTSAgent { [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = "NoVersion")] @@ -253,7 +258,11 @@ function Install-VSTSAgent { [pscredential]$LogonCredential, [parameter(Mandatory = $false)] - [string]$Cache = [io.Path]::Combine($env:USERPROFILE, ".vstsagents") + [string]$Cache = [io.Path]::Combine($env:USERPROFILE, ".vstsagents"), + + [parameter(Mandatory = $false)] + [ValidateSet('vsts-agent','pipelines-agent')] + [string]$AgentType = 'vsts-agent' ) if ($PSVersionTable.Platform -and $PSVersionTable.Platform -ne 'Win32NT') { @@ -274,6 +283,7 @@ function Install-VSTSAgent { if ( $MinimumVersion ) { $findArgs['MinimumVersion'] = $MinimumVersion } if ( $MaximumVersion ) { $findArgs['MaximumVersion'] = $MaximumVersion } if ( $RequiredVersion ) { $findArgs['RequiredVersion'] = $RequiredVersion } + if ( $AgentType ) { $findArgs['AgentType'] = $AgentType } $agent = Find-VSTSAgent @findArgs | Sort-Object -Descending -Property Version | Select-Object -First 1 if ( -not $agent ) { throw "Could not find agent matching requirements." } @@ -303,7 +313,7 @@ function Install-VSTSAgent { Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop } - [System.IO.Compression.ZipFile]::ExtractToDirectory($destPath, $agentFolder) + [System.IO.Compression.ZipFile]::ExtractToDirectory($destPath, $agentFolder, $true) $configPath = [io.path]::combine($agentFolder, 'config.cmd') $configPath = Get-ChildItem $configPath -ErrorAction SilentlyContinue From de484131d7b0bca647298f561a82b720a2cb2852 Mon Sep 17 00:00:00 2001 From: Tristan Barcelon Date: Sat, 5 Aug 2023 07:36:40 -0400 Subject: [PATCH 2/2] Detect presence of agentfolder and support overwrite confirmation --- VSTSAgent/VSTSAgent.psm1 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/VSTSAgent/VSTSAgent.psm1 b/VSTSAgent/VSTSAgent.psm1 index da8a90d..b9bf9fa 100644 --- a/VSTSAgent/VSTSAgent.psm1 +++ b/VSTSAgent/VSTSAgent.psm1 @@ -313,7 +313,17 @@ function Install-VSTSAgent { Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop } - [System.IO.Compression.ZipFile]::ExtractToDirectory($destPath, $agentFolder, $true) + if (Test-Path -Path $agentFolder -PathType Container) { + if ($PSCmdlet.ShouldProcess($agentFolder, "Overwriting with content of $($destPath)")) { + [System.IO.Compression.ZipFile]::ExtractToDirectory($destPath, $agentFolder, $true) + } + else { + throw "$($agentFolder) already exists." + } + } + else { + [System.IO.Compression.ZipFile]::ExtractToDirectory($destPath, $agentFolder) + } $configPath = [io.path]::combine($agentFolder, 'config.cmd') $configPath = Get-ChildItem $configPath -ErrorAction SilentlyContinue