Files
blog.stevanfreeborn.com/scripts/deploy.ps1
T

258 lines
5.8 KiB
PowerShell
Raw Normal View History

2025-10-09 12:29:37 -05:00
$NGINX_CONFIG_PATHS = @(
"/etc/nginx/sites-available/blog.stevanfreeborn.com",
"/etc/nginx/sites-available/blog.steva.nz"
)
$BLUE_PORT = 5001
$GREEN_PORT = 5002
2024-04-13 02:03:44 -05:00
2024-04-14 16:48:24 -05:00
function StartContainer
{
2024-04-13 02:03:44 -05:00
param (
[string]$containerColor,
2025-10-09 12:29:37 -05:00
[string]$dockerTag,
[int]$hostPort
2024-04-13 02:03:44 -05:00
)
$containerName = "blog.stevanfreeborn.com.$containerColor"
$postDirEnv = "FilePostServiceOptions__PostsDirectory=wwwroot/posts"
2025-10-09 12:29:37 -05:00
docker run -d --restart always -p "${hostPort}:8080" --name $containerName --env $postDirEnv $dockerTag | Out-Null
2024-04-13 02:03:44 -05:00
if ($LASTEXITCODE -ne 0)
{
Write-Host "Failed to run $containerName container."
exit 1;
}
$running = $false
$attemptLimit = 12
$attempts = 0
$waitTimeInSeconds = 10
while ($running -eq $false -and $attempts -lt $attemptLimit)
{
2024-04-13 03:14:58 -05:00
try
2024-04-13 02:03:44 -05:00
{
2025-10-09 12:29:37 -05:00
$response = Invoke-WebRequest -Uri "http://localhost:$hostPort/rss" -UseBasicParsing
2024-04-13 03:14:58 -05:00
if ($response.StatusCode -eq 200)
{
$running = $true
}
else
{
Write-Host "Waiting for $containerName container to start..."
Start-Sleep -Seconds $waitTimeInSeconds
$attempts++
}
2024-04-13 02:03:44 -05:00
}
2024-04-13 03:14:58 -05:00
catch
2024-04-13 02:03:44 -05:00
{
Write-Host "Waiting for $containerName container to start..."
Start-Sleep -Seconds $waitTimeInSeconds
2024-04-13 03:14:58 -05:00
$attempts++
2024-04-13 02:03:44 -05:00
}
}
if ($running -eq $false)
{
Write-Host "Failed to start $containerName container."
exit 1
}
}
2024-04-14 16:48:24 -05:00
function UpdateNginxConfig
{
2024-04-13 02:03:44 -05:00
param (
[string]$filePath,
[string]$portNumber
)
$nginxConfig = Get-Content $filePath
$pattern = "proxy_pass http://localhost:\d+;"
$replacement = "proxy_pass http://localhost:$portNumber;"
$modifiedContent = @()
2025-10-09 12:29:37 -05:00
foreach ($line in $nginxConfig)
{
if ($line -match $pattern)
{
$line = $line -replace $pattern, $replacement
2024-04-13 02:03:44 -05:00
}
$modifiedContent += $line
}
Set-Content -Path $filePath -Value $modifiedContent
}
# Get the version number from the command line
$version = $args[0];
if ($null -eq $version)
{
Write-Host "Please provide a version number."
exit 1
}
# check if docker is installed
$dockerVersion = docker --version
if ($null -eq $dockerVersion)
{
Write-Host "Docker is not installed. Please install Docker and try again."
exit 1
}
2025-10-09 12:29:37 -05:00
foreach ($configPath in $NGINX_CONFIG_PATHS)
2024-04-13 02:03:44 -05:00
{
2025-10-09 12:29:37 -05:00
if (-not (Test-Path $configPath))
{
Write-Host "Nginx configuration file not found: $configPath"
exit 1
}
2024-04-13 02:03:44 -05:00
}
# attempt to pull docker image
$dockerTag = "stevanfreeborn/blog.stevanfreeborn.com:$version"
docker pull $dockerTag
if ($LASTEXITCODE -ne 0)
{
Write-Host "Failed to pull docker image: $dockerTag"
exit 1
}
2025-10-09 12:29:37 -05:00
# Check if blue container is running
$blueContainerId = docker ps --filter "name=blog.stevanfreeborn.com.blue" --format "{{.ID}}"
2024-04-13 02:03:44 -05:00
if ($null -eq $blueContainerId)
2024-04-13 03:08:36 -05:00
{
Write-Host "Blue container is not running. Starting blue container."
2024-04-13 03:08:36 -05:00
2025-10-09 12:29:37 -05:00
StartContainer -containerColor "blue" -dockerTag $dockerTag -hostPort $BLUE_PORT
2024-04-13 03:08:36 -05:00
Write-Host "Blue container is running."
2024-04-13 03:08:36 -05:00
2025-10-09 12:29:37 -05:00
# Backup original configs before making changes
$configBackups = @{}
2024-04-13 03:08:36 -05:00
2025-10-09 12:29:37 -05:00
foreach ($configPath in $NGINX_CONFIG_PATHS)
{
$configBackups[$configPath] = Get-Content $configPath
}
foreach ($configPath in $NGINX_CONFIG_PATHS)
{
UpdateNginxConfig -filePath $configPath -portNumber $BLUE_PORT
Write-Host "Nginx configuration updated: $configPath to point to blue container on port $BLUE_PORT."
}
Write-Host "All Nginx configurations updated to point to blue container on port $BLUE_PORT."
2024-04-13 03:08:36 -05:00
nginx -t
if ($LASTEXITCODE -ne 0)
{
Write-Host "Nginx configuration test failed. Reverting changes."
2025-10-09 12:29:37 -05:00
foreach ($configPath in $NGINX_CONFIG_PATHS)
{
Set-Content -Path $configPath -Value $configBackups[$configPath]
Write-Host "Reverted: $configPath"
}
2024-04-13 03:08:36 -05:00
exit 1
}
nginx -s reload
if ($LASTEXITCODE -ne 0)
{
Write-Host "Failed to reload Nginx."
exit 1
}
Write-Host "Nginx reloaded. Successfully deployed version $version."
exit 0
}
else
2024-04-13 02:03:44 -05:00
{
Write-Host "Blue container is running. Starting green container."
2024-04-13 02:03:44 -05:00
2025-10-09 12:29:37 -05:00
StartContainer -containerColor "green" -dockerTag $dockerTag -hostPort $GREEN_PORT
2024-04-13 02:03:44 -05:00
Write-Host "Green container is running."
2024-04-13 02:03:44 -05:00
2025-10-09 12:29:37 -05:00
# Backup original configs before making changes
$configBackups = @{}
2024-04-13 02:03:44 -05:00
2025-10-09 12:29:37 -05:00
foreach ($configPath in $NGINX_CONFIG_PATHS)
{
$configBackups[$configPath] = Get-Content $configPath
}
foreach ($configPath in $NGINX_CONFIG_PATHS)
{
UpdateNginxConfig -filePath $configPath -portNumber $GREEN_PORT
Write-Host "Nginx configuration updated: $configPath to point to green container on port $GREEN_PORT."
}
Write-Host "All Nginx configurations updated to point to green container on port $GREEN_PORT."
2024-04-13 02:03:44 -05:00
2024-04-13 02:19:20 -05:00
nginx -t
2024-04-13 02:03:44 -05:00
if ($LASTEXITCODE -ne 0)
{
Write-Host "Nginx configuration test failed. Reverting changes."
2025-10-09 12:29:37 -05:00
foreach ($configPath in $NGINX_CONFIG_PATHS)
{
Set-Content -Path $configPath -Value $configBackups[$configPath]
Write-Host "Reverted: $configPath"
}
2024-04-13 02:03:44 -05:00
exit 1
}
2024-04-13 02:19:20 -05:00
nginx -s reload
2024-04-13 02:03:44 -05:00
if ($LASTEXITCODE -ne 0)
{
Write-Host "Failed to reload Nginx."
exit 1
}
Write-Host "Nginx reloaded. Successfully deployed version $version."
Write-Host "Stopping and removing blue container."
2024-04-13 02:03:44 -05:00
docker stop $blueContainerId
2024-04-13 02:03:44 -05:00
if ($LASTEXITCODE -ne 0)
{
Write-Host "Failed to stop blue container."
2024-04-13 02:03:44 -05:00
exit 1
}
docker rm $blueContainerId
2024-04-13 02:03:44 -05:00
if ($LASTEXITCODE -ne 0)
{
Write-Host "Failed to remove blue container."
2024-04-13 02:03:44 -05:00
exit 1
}
Write-Host "Blue container stopped and removed."
2024-04-13 02:03:44 -05:00
Write-Host "Switching green container to blue container."
2024-04-13 02:03:44 -05:00
docker rename "blog.stevanfreeborn.com.green" "blog.stevanfreeborn.com.blue"
2024-04-13 02:03:44 -05:00
if ($LASTEXITCODE -ne 0)
{
Write-Host "Failed to rename green container to blue container."
2024-04-13 02:03:44 -05:00
exit 1
}
Write-Host "Successfully deployed version $version."
exit 0
}