Skip to content

Instantly share code, notes, and snippets.

@theodorejb
Created January 1, 2023 16:04
Show Gist options
  • Save theodorejb/284e57d457e31945d55681cd684a7e8c to your computer and use it in GitHub Desktop.
Save theodorejb/284e57d457e31945d55681cd684a7e8c to your computer and use it in GitHub Desktop.
A zero-downtime IIS site deploy script
param(
[string] $server
)
$ErrorActionPreference = "Stop"
$sitePathArr = Invoke-Command -ComputerName $server -ScriptBlock {
(Get-Website mySite).PhysicalPath.Split("\")
}
$currentPath = $sitePathArr[0..($sitePathArr.count - 2)] -join "\"
$path = $sitePathArr[0..($sitePathArr.count - 3)] -join "\"
$current = $currentPath.Split("\")[-1] # path segment above public folder
"Current site folder is $current"
# Always deploy to new directory without deleting any old ones to avoid losing in-memory ASP sessions.
# A separate task can be used to clean up old directories overnight.
$buildNum = [int]$current.Split("mySite")[1]
$buildNum++
$targetPath = "$path\mySite$buildNum"
"Deploying to $targetPath"
mkdir -p $targetPath
7z x ci\archive.zip -o"$targetPath"
if ($LastExitCode -gt 1) {
throw "7-Zip failed with exit code $LastExitCode"
}
"Copying config file"
Copy-Item "$currentPath\php\LocalConfig.php" -Destination "$targetPath\php"
"Updating physical path for IIS site"
Invoke-Command -ComputerName $server -ScriptBlock {
Import-Module WebAdministration
Set-ItemProperty IIS:\Sites\mySite -name physicalPath -value "$Using:targetPath\public"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment