Skip to content

Commit 6002b6c

Browse files
authored
Merge pull request #78 from git-for-windows/install-pwsh-to-runners
self-hosted runners: install pwsh
2 parents 3c005ee + 48ac9cb commit 6002b6c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

azure-self-hosted-runners/post-deployment-script.ps1

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,45 @@ catch {
111111
exit 1
112112
}
113113

114+
# =================================
115+
# Obtain the latest pwsh binary and other pwsh information
116+
# =================================
117+
#
118+
# This will install pwsh on the machine, because it's not installed by default.
119+
# It contains a bunch of new features compared to "powershell" and is sometimes more stable as well.
120+
#
121+
# url for Github API to get the latest release of pwsh
122+
#
123+
# TODO update this to /releases/latest once 7.5.0 is out, as it adds support for arm64 MSIs
124+
[string]$PwshUrl = "https://api.github.com/repos/PowerShell/PowerShell/releases/tags/v7.5.0-preview.2"
125+
126+
# Name of the MSI file that should be verified and downloaded
127+
[string]$PwshMsiName = "PowerShell-.*-win-arm64.msi"
128+
129+
try {
130+
[System.Object]$PwshRestData = Invoke-RestMethod -Uri $PwshUrl -Method Get -Headers $GithubHeaders -TimeoutSec 10 | Select-Object -Property assets, body
131+
[System.Object]$PwshAsset = $PwshRestData.assets | Where-Object { $_.name -match $PwshMsiName }
132+
if ($PwshRestData.body -match "\b$([Regex]::Escape($PwshAsset.name))\r\n.*?([a-zA-Z0-9]{64})" -eq $True) {
133+
[System.Object]$GitHubPwsh = [PSCustomObject]@{
134+
DownloadUrl = [string]$PwshAsset.browser_download_url
135+
Hash = [string]$Matches[1].ToUpper()
136+
OutFile = "./pwsh-installer.msi"
137+
}
138+
}
139+
else {
140+
Write-Error "Could not find hash for $PwshMsiName"
141+
exit 1
142+
}
143+
}
144+
catch {
145+
Write-Error @"
146+
"Message: "$($_.Exception.Message)`n
147+
"Error Line: "$($_.InvocationInfo.Line)`n
148+
"Line Number: "$($_.InvocationInfo.ScriptLineNumber)`n
149+
"@
150+
exit 1
151+
}
152+
114153
# ======================
115154
# WINDOWS DEVELOPER MODE
116155
# ======================
@@ -173,6 +212,38 @@ Start-Process -Wait $GitHubGit.OutFile '/VERYSILENT /NORESTART /NOCANCEL /SP- /C
173212

174213
Write-Output "Finished installing Git for Windows."
175214

215+
# ======================
216+
# PWSH (PowerShell)
217+
# ======================
218+
219+
Write-Output "Downloading pwsh..."
220+
221+
$ProgressPreference = 'SilentlyContinue'
222+
Invoke-WebRequest -UseBasicParsing -Uri $GitHubPwsh.DownloadUrl -OutFile $GitHubPwsh.OutFile
223+
$ProgressPreference = 'Continue'
224+
225+
if ((Get-FileHash -Path $GitHubPwsh.OutFile -Algorithm SHA256).Hash.ToUpper() -ne $GitHubPwsh.Hash) {
226+
Write-Error "Computed checksum for $($GitHubPwsh.OutFile) did not match $($GitHubPwsh.Hash)"
227+
exit 1
228+
}
229+
230+
Write-Output "Installing pwsh..."
231+
232+
# Get the full path to the MSI in the current working directory
233+
$MsiPath = Resolve-Path $GitHubPwsh.OutFile
234+
235+
# Define arguments for silent installation
236+
$MsiArguments = "/qn /i `"$MsiPath`" ADD_PATH=1"
237+
238+
# Install pwsh using msiexec
239+
Start-Process msiexec.exe -Wait -ArgumentList $MsiArguments
240+
241+
# TODO remove once 7.5.0 is out
242+
Write-Output "Copying pwsh-preview.cmd to pwsh.cmd as a temporary measure until 7.5.0 is out..."
243+
Copy-Item "C:\Program Files\PowerShell\7-preview\preview\pwsh-preview.cmd" "C:\Program Files\PowerShell\7-preview\preview\pwsh.cmd"
244+
245+
Write-Output "Finished installing pwsh."
246+
176247
# ======================
177248
# GITHUB ACTIONS RUNNER
178249
# ======================

0 commit comments

Comments
 (0)