Simplify deploy script (hardcoded settings, no offline file, optional WinRM recycle)

This commit is contained in:
2026-02-02 16:36:33 +01:00
parent cc5c4edd98
commit 7bc6bb2a3d

View File

@@ -1,38 +1,38 @@
param( # Hard-coded deploy settings. Fill these in before running.
[Parameter(Mandatory = $true)][string]$FtpHost, $FtpHost = "ftp.example.com"
[Parameter(Mandatory = $true)][string]$FtpUser, $FtpUser = "deploy-user"
[Parameter(Mandatory = $true)][string]$FtpPassword, $FtpPassword = "deploy-password"
[string]$RemoteDir = "/httpdocs", $RemoteDir = "/httpdocs"
[string]$ProjectPath = "..\\GameList.csproj", $ProjectPath = "..\\GameList.csproj"
[string]$Configuration = "Release", $Configuration = "Release"
[string]$Runtime = "win-x64", $Runtime = "win-x64"
[string]$PublishDir = "..\\artifacts\\publish", $PublishDir = "..\\artifacts\\publish"
[switch]$SelfContained, $SelfContained = $false
[string]$WinScpPath = "WinSCP.com" $WinScpPath = "WinSCP.com"
)
<# # Optional: recycle IIS app pool via WinRM instead of RDP. Set $RecycleAppPool = $false to skip.
$RecycleAppPool = $true
$AppPoolName = "DefaultAppPool"
$WinRmComputer = "your-server-hostname"
$WinRmCredentialUser = "DOMAIN\\deploy-user"
$WinRmCredentialPass = "P@ssw0rd!"
<#!
.SYNOPSIS .SYNOPSIS
Publish the app and mirror the output to an FTP-deployed IIS site. Publish the app and mirror the output to an FTP-deployed IIS site.
.DESCRIPTION .DESCRIPTION
- Builds with dotnet publish. - Builds with dotnet publish.
- Drops app_offline.htm to pause the IIS site while files upload.
- Uses WinSCP (ftp) to mirror publish output into $RemoteDir (deletes extraneous remote files). - Uses WinSCP (ftp) to mirror publish output into $RemoteDir (deletes extraneous remote files).
- Removes app_offline.htm when done. - Optionally recycles the IIS app pool remotely via WinRM (no RDP needed).
.PREREQS .PREREQS
- WinSCP.com available in PATH or pass -WinScpPath "C:\Path\To\WinSCP.com". - WinSCP.com available in PATH or set $WinScpPath.
- FTP user must have write/delete rights to $RemoteDir. - FTP user must have write/delete rights to $RemoteDir.
- If you have RDP, you can still run this locally; the app_offline flag safely stops the app without touching the pool. - WinRM must be enabled for remote app pool recycle (set $RecycleAppPool = $false otherwise).
.EXAMPLE .EXAMPLE
pwsh ./scripts/deploy-ftp.ps1 ` pwsh ./scripts/deploy-ftp.ps1
-FtpHost "ftp.example.com" `
-FtpUser "deploy" `
-FtpPassword (Get-Content ./secrets/ftp.pwd -Raw) `
-RemoteDir "/httpdocs" `
-Configuration Release
#> #>
Set-StrictMode -Version Latest Set-StrictMode -Version Latest
@@ -41,7 +41,7 @@ $ErrorActionPreference = "Stop"
function Assert-Tool { function Assert-Tool {
param([string]$Name) param([string]$Name)
if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) { if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) {
throw "Required tool '$Name' not found. Install it or point -WinScpPath to the executable." throw "Required tool '$Name' not found. Install it or update paths."
} }
} }
@@ -50,32 +50,34 @@ Assert-Tool $WinScpPath
Write-Host "1) Publishing..." -ForegroundColor Cyan Write-Host "1) Publishing..." -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path $PublishDir | Out-Null New-Item -ItemType Directory -Force -Path $PublishDir | Out-Null
$publishArgs = @("publish", $ProjectPath, "-c", $Configuration, "-r", $Runtime, "-o", $PublishDir) $publishArgs = @("publish", $ProjectPath, "-c", $Configuration, "-r", $Runtime, "-o", $PublishDir)
if (-not $SelfContained) { $publishArgs += "--self-contained=false" } if (-not $SelfContained) { $publishArgs += "--self-contained=false" }
dotnet @publishArgs dotnet @publishArgs
$offlineFile = Join-Path $PublishDir "app_offline.htm"
"<html><body style='font-family:sans-serif;padding:32px;background:#f6e9d6;'><h2>Updating…</h2><p>Back in a moment.</p></body></html>" | Set-Content -Encoding UTF8 $offlineFile
Write-Host "2) Syncing via WinSCP (FTP mirror with delete)..." -ForegroundColor Cyan Write-Host "2) Syncing via WinSCP (FTP mirror with delete)..." -ForegroundColor Cyan
$tempScript = New-TemporaryFile $tempScript = New-TemporaryFile
@" @"
option batch continue option batch continue
option confirm off option confirm off
open ftp://$($FtpUser):$($FtpPassword.Replace("`n","").Replace("`r",""))@$FtpHost open ftp://$($FtpUser):$($FtpPassword.Replace('`n','').Replace('`r',''))@$FtpHost
lcd $PublishDir lcd $PublishDir
cd $RemoteDir cd $RemoteDir
put app_offline.htm
synchronize remote . -delete synchronize remote . -delete
rm app_offline.htm
exit exit
"@ | Set-Content -Path $tempScript -Encoding UTF8 "@ | Set-Content -Path $tempScript -Encoding UTF8
& $WinScpPath "/ini=nul" "/script=$tempScript" & $WinScpPath "/ini=nul" "/script=$tempScript"
Remove-Item $tempScript -ErrorAction SilentlyContinue Remove-Item $tempScript -ErrorAction SilentlyContinue
Write-Host "`nDone. If you want to explicitly bounce the app pool while on RDP:" -ForegroundColor Green if ($RecycleAppPool) {
Write-Host " Import-Module WebAdministration" Write-Host "3) Recycling IIS app pool via WinRM..." -ForegroundColor Cyan
Write-Host " Stop-WebAppPool 'YourAppPoolName'; Start-WebAppPool 'YourAppPoolName'" -ForegroundColor DarkGray $sec = ConvertTo-SecureString $WinRmCredentialPass -AsPlainText -Force
$cred = New-Object pscredential($WinRmCredentialUser, $sec)
Invoke-Command -ComputerName $WinRmComputer -Credential $cred -ScriptBlock {
Import-Module WebAdministration
Stop-WebAppPool -Name $using:AppPoolName -ErrorAction SilentlyContinue
Start-WebAppPool -Name $using:AppPoolName
}
}
Write-Host "Done." -ForegroundColor Green