A while back, I wrote a post titled Monitor and Download Latest .NET Core Installer. The post was about a script which could be used to monitor the aspnet/aspnetcore releases page, and if a new releases came out the installer for the .NET Hosting Bundle would be downloaded. A piece of code I didn’t include in that post, was a script that then took the downloaded .exe installer and created an installation package from it. This installation package was targeted at servers that host ASP.NET Core web applications. This post is not about that secondary script.
Instead, this is a small tweak to the monitor/download script. Instead of downloading the .NET Hosting Bundling, it would download the SDK. And the installation package that it eventually creates (not included in this post) is targeted at build servers.
Import-Module PowerShellForGitHub | |
# retrieve most recent version info from GitHub | |
$releases = Get-GitHubRelease -OwnerName dotnet -RepositoryName core | |
$latestPreview = $releases |? { $_.prerelease } | sort created_at -Descending | select -First 1 | |
$latestFull = $releases |? { -not $_.prerelease } | sort created_at -Descending | select -First 1 | |
<########################################### | |
# So, we use Octopus Deploy (https://octopus.com/) and these are some custom functions we wrote to | |
# help work with it. You will need to replace these functions with a way to retrieve what is the | |
# latest version of .NET Core installed on your servers. | |
###########################################> | |
Import-Module OctoUcsb | |
OctoUcsb\Set-OctoGlobalVarsByEnv -Environment Prod | |
# retrieve most recent version info from Octo | |
$octopackage = Get-LatestReleaseOfProject -ProjectName "WebFarm.NetCoreSDK" -IncludePrerelease | |
<###########################################> | |
$currentLatestFullVersion = $latestFull.tag_name.Substring(1) | |
$currentOctoVersion = $octopackage.Version.Split("-")[0] | |
# download the new releases x64 runtime installer | |
$downloadPageUrlFormat = "https://github.com/dotnet/core/blob/master/release-notes/{0}/{1}/{1}.md" | |
$version = $currentLatestFullVersion | |
$versionSplit = $version.Split(".") | |
$majorMinorVersion = $versionSplit[0] + "." + $versionSplit[1] | |
$downloadPageUrl = $downloadPageUrlFormat -f $majorMinorVersion, $version | |
# check if the page can be loaded | |
$dpResponse = Invoke-WebRequest -Uri $downloadPageUrl -UseBasicParsing | |
if($dpResponse.StatusCode -eq 200) { | |
Write-Host "Success" | |
} else { | |
Write-Error "Error" | |
Write-Error ($dpResponse | Out-String) | |
Write-Error "Contents" | |
Write-Error "$($dpResponse.Content | Out-String)" | |
return | |
} | |
# parse the page for an SDK version number | |
$versionSplit = $version.Split(".", [System.StringSplitOptions]::RemoveEmptyEntries) | |
$sdkTextRegex = "\.NET Core SDK ({0}\.{1}\.[0-9]+)" -f $versionSplit[0], $versionSplit[1] | |
if($dpResponse.Content -match $sdkTextRegex) { | |
$sdkVersion = $Matches[1] | |
} else { | |
Write-Error "Unable to parse SDK Version from $downloadPageUrl" | |
Write-Error "Page Contents" | |
Write-Error ($dpResponse.Content | Out-String) | |
return | |
} | |
# check if a new release has been made which we don't have a package for | |
if($sdkVersion -eq $currentOctoVersion) { | |
Write-Host "Current released version ($sdkVersion) and current Octo package version ($currentOctoVersion) are the same. No more processing needed." | |
break; | |
} | |
# parse the page for data | |
Import-Module SeleniumUcsb | |
Write-Host "Retrieving download page, $downloadPageUrl ..." | |
$driver = Start-SeChrome -Arguments "headless", "incognito" | |
$downloadUrl = $null | |
try { | |
Enter-SeUrl -Driver $driver -Url $downloadPageUrl | |
Wait-UntilElementLoaded -Driver $driver -Id "readme" | |
# -XPath "//div[contains(@id,'service-small-info-')]/div[@class='highlights']/ul" | |
$sdkText = "dotnet-sdk-{0}-win-x64.exe" -f $sdkversion | |
$a = Find-SeElement -Driver $driver ` | |
-XPath ("//table[thead//text()[contains(., 'SDK Installer')]]" + | |
"/tbody/tr[td//text()[contains(., 'Windows')]]" + | |
"/td/a[contains(@href, '$sdkText')]") | |
$downloadUrl = $a.GetAttribute('href') | |
Write-Host "Found download url: $downloadUrl" | |
} finally { | |
Stop-SeDriver -Driver $driver | |
} | |
if($null -eq $downloadUrl) { | |
Write-Error "Could not find the download link on the page $downloadPageUrl" | |
Write-Error "Page Contents" | |
Write-Error ($dpResponse.Content | Out-String) | |
return | |
} | |
# download the executable | |
$folderName = Join-Path -Path $env:TEMP -ChildPath $sdkText | |
New-Item -Path $folderName -ItemType Directory -Verbose | |
try { | |
$tempFilename = Join-Path -Path $folderName -ChildPath $sdkText | |
Invoke-WebRequest -Uri $downloadUrl -OutFile $tempFilename | |
} finally { | |
# clean up the executable download | |
Write-Host "Deleting $folderName ..." | |
Remove-Item -Path $folderName -Force -Recurse -ErrorAction SilentlyContinue | |
} | |
# send an email notifying the new deployment being ready | |
<########################################### | |
# The email body describes information that was cut from this script for brevity. | |
# There are other stesp which involve updating source control, creating a build, | |
# and creating a release/deployment for the new installer. | |
# | |
# This should give you some ideas of what pieces you can build out next to make | |
# your life easier. It might also turn into another blog post later (... maybe). | |
###########################################> | |
$priority = [System.Net.Mail.MailPriority]::Normal; | |
$messageParams = @{ | |
Subject = "WebFarm.NetCoreSDK - $version - Octo Package Available"; | |
Priority = $priority; | |
BodyAsHtml = $true; | |
Body = | |
"Octo: <a href=`"https://octopus.xxxxxxx/app#/Spaces-1/projects/webfarm-netcoresdk/overview`">WebFarm.NetCore</a><br />" + ` | |
"Build: <a href=`"$($build.Url)`">$($build.Id)</a>"; | |
From = "donotreply@yourcompany.com"; | |
To = @("Your Name <your.name@yourcompany.com>") | |
SmtpServer = "smtp.yourcompany.com"; | |
}; | |
Send-MailMessage @messageParams; |
0 comments:
Post a Comment