Skip to content

Instantly share code, notes, and snippets.

@ducas
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save ducas/217ce79cef7b9d61e87b to your computer and use it in GitHub Desktop.

Select an option

Save ducas/217ce79cef7b9d61e87b to your computer and use it in GitHub Desktop.
Validate that a release and its packages can be deployed to an environment based on whether they have pre-release version numbers
$environment = $OctopusParameters['Octopus.Environment.Name']
$releaseNumber = $OctopusParameters['Octopus.Release.Number']
$packages = @(
@{
"Id" = $OctopusParameters['Octopus.Action[Update Database].Package.NuGetPackageId'];
"Version" = $OctopusParameters['Octopus.Action[Update Database].Package.NuGetPackageVersion']
},
@{
"Id" = $OctopusParameters['Octopus.Action[Deploy Website].Package.NuGetPackageId'];
"Version" = $OctopusParameters['Octopus.Action[Deploy Website].Package.NuGetPackageVersion']
}
)
$environmentReleaseMap = @{
"Test" = "*";
"UAT" = @("rc");
"Pre-Production" = @();
"Production" = @();
}
$releases = $environmentReleaseMap[$environment]
Write-Host "$environment allows [ $( $releases -join ", " ) ] pre-release package types within releases."
if ($releases -eq $null -or $releases -eq "*") {
exit
}
function Get-PreReleaseType ($version) {
$regex = [regex]"^(\d+.){2}\d+(-(?<prerelease>[A-z]+)\d+){1}$"
$matches = $regex.Matches($version)
foreach ($match in $matches) {
$group = $match.Groups["prerelease"]
if ($group -ne $null) {
return $group.Value
}
}
}
function Validate-Release ($number) {
Write-Host "Validating release $number can be deployed to $environment."
$type = Get-PreReleaseType $number
if ($type -eq $null) { Write-Host "Release does not have a pre-release version number."; }
if ($type -ne $null) { Write-Host "Release has a pre-release ($type) version number."; }
if ($type -ne $null -and $releases -notcontains $type) {
throw "This release can not be deployed to $environment because it has a pre-release ($type) version number."
}
}
function Validate-Package ($packageName, $packageVersion) {
Write-Host "Validating $packageName $packageVersion can be deployed to $environment."
$type = Get-PreReleaseType $packageVersion
if ($type -eq $null) { Write-Host "$packageName is not a pre-release package."; }
if ($type -ne $null) { Write-Host "$packageName is a pre-release ($type) package."; }
if ($type -ne $null -and $releases -notcontains $type) {
throw "This release can not be deployed to $environment because it contains a pre-release ($type) version of the $packageName package."
}
}
Validate-Release $releaseNumber
$packages | %{ Validate-Package $_.Id $_.Version }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment