diff --git a/scripts/deploy-ftp.ps1 b/scripts/deploy-ftp.ps1 index 51b2c05..c967050 100644 --- a/scripts/deploy-ftp.ps1 +++ b/scripts/deploy-ftp.ps1 @@ -1,38 +1,38 @@ -param( - [Parameter(Mandatory = $true)][string]$FtpHost, - [Parameter(Mandatory = $true)][string]$FtpUser, - [Parameter(Mandatory = $true)][string]$FtpPassword, - [string]$RemoteDir = "/httpdocs", - [string]$ProjectPath = "..\\GameList.csproj", - [string]$Configuration = "Release", - [string]$Runtime = "win-x64", - [string]$PublishDir = "..\\artifacts\\publish", - [switch]$SelfContained, - [string]$WinScpPath = "WinSCP.com" -) +# Hard-coded deploy settings. Fill these in before running. +$FtpHost = "ftp.example.com" +$FtpUser = "deploy-user" +$FtpPassword = "deploy-password" +$RemoteDir = "/httpdocs" +$ProjectPath = "..\\GameList.csproj" +$Configuration = "Release" +$Runtime = "win-x64" +$PublishDir = "..\\artifacts\\publish" +$SelfContained = $false +$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 Publish the app and mirror the output to an FTP-deployed IIS site. .DESCRIPTION - 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). - - Removes app_offline.htm when done. + - Optionally recycles the IIS app pool remotely via WinRM (no RDP needed). .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. - - 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 - pwsh ./scripts/deploy-ftp.ps1 ` - -FtpHost "ftp.example.com" ` - -FtpUser "deploy" ` - -FtpPassword (Get-Content ./secrets/ftp.pwd -Raw) ` - -RemoteDir "/httpdocs" ` - -Configuration Release + pwsh ./scripts/deploy-ftp.ps1 #> Set-StrictMode -Version Latest @@ -41,7 +41,7 @@ $ErrorActionPreference = "Stop" function Assert-Tool { param([string]$Name) 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 New-Item -ItemType Directory -Force -Path $PublishDir | Out-Null - $publishArgs = @("publish", $ProjectPath, "-c", $Configuration, "-r", $Runtime, "-o", $PublishDir) if (-not $SelfContained) { $publishArgs += "--self-contained=false" } dotnet @publishArgs -$offlineFile = Join-Path $PublishDir "app_offline.htm" -"
Back in a moment.
" | Set-Content -Encoding UTF8 $offlineFile - Write-Host "2) Syncing via WinSCP (FTP mirror with delete)..." -ForegroundColor Cyan $tempScript = New-TemporaryFile @" option batch continue option confirm off -open ftp://$($FtpUser):$($FtpPassword.Replace("`n","").Replace("`r",""))@$FtpHost +open ftp://$($FtpUser):$($FtpPassword.Replace('`n','').Replace('`r',''))@$FtpHost lcd $PublishDir cd $RemoteDir -put app_offline.htm synchronize remote . -delete -rm app_offline.htm exit "@ | Set-Content -Path $tempScript -Encoding UTF8 & $WinScpPath "/ini=nul" "/script=$tempScript" - Remove-Item $tempScript -ErrorAction SilentlyContinue -Write-Host "`nDone. If you want to explicitly bounce the app pool while on RDP:" -ForegroundColor Green -Write-Host " Import-Module WebAdministration" -Write-Host " Stop-WebAppPool 'YourAppPoolName'; Start-WebAppPool 'YourAppPoolName'" -ForegroundColor DarkGray +if ($RecycleAppPool) { + Write-Host "3) Recycling IIS app pool via WinRM..." -ForegroundColor Cyan + $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