Files
Actions/deploy-iis/action.yml
alberto 2b860fbc27 Add: clean-destination input to optionally clear destination folder during IIS deployment
- Introduces a new input to allow cleanup of destination folder while retaining specific files and folders (`appsettings.json`, `web.config`, and `store`).
- Updates error handling for IIS site and app pool stop commands to continue execution on failure.
2026-06-07 15:06:05 +02:00

145 lines
4.4 KiB
YAML

name: Deploy IIS
description: Ferma sito e application pool IIS, sincronizza i file pubblicati e riavvia i servizi.
inputs:
source-path:
description: Cartella sorgente da distribuire su IIS.
required: true
destination-path:
description: Cartella di destinazione sul server IIS.
required: true
site-name:
description: Nome del sito IIS da fermare e riavviare.
required: true
app-pool-name:
description: Nome dell'application pool IIS da fermare e riavviare.
required: true
exclude-dirs:
description: Elenco di directory da escludere dal mirroring (separate da virgola, punto e virgola o newline).
required: false
default: store
exclude-files:
description: Elenco di file da escludere dal mirroring (separati da virgola, punto e virgola o newline).
required: false
default: appsettings.json
clean-destination:
description: Se 'true', elimina tutto il contenuto della cartella di destinazione (dopo aver fermato IIS) tranne appsettings.json, web.config e la cartella store.
required: false
default: 'false'
runs:
using: composite
steps:
- name: Deploy su IIS
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$appcmd = Join-Path $env:SystemRoot 'System32\inetsrv\appcmd.exe'
$src = '${{ inputs.source-path }}'
$dst = '${{ inputs.destination-path }}'
$site = '${{ inputs.site-name }}'
$appPool = '${{ inputs.app-pool-name }}'
function Split-InputList {
param([string]$Value)
if ([string]::IsNullOrWhiteSpace($Value)) {
return @()
}
return @(
$Value -split '[,;\r\n]+'
| ForEach-Object { $_.Trim() }
| Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
)
}
if (-not (Test-Path -LiteralPath $appcmd)) {
throw "appcmd.exe non trovato: $appcmd"
}
foreach ($entry in @{
'source-path' = $src
'destination-path' = $dst
'site-name' = $site
'app-pool-name' = $appPool
}.GetEnumerator()) {
if ([string]::IsNullOrWhiteSpace($entry.Value)) {
throw "Input '$($entry.Key)' mancante."
}
}
if (-not (Test-Path -LiteralPath $src)) {
throw "Cartella sorgente non trovata: $src"
}
$excludeDirs = Split-InputList '${{ inputs.exclude-dirs }}'
$excludeFiles = Split-InputList '${{ inputs.exclude-files }}'
$robocopyArgs = @(
$src
$dst
'/MIR'
'/R:2'
'/W:1'
'/NFL'
'/NDL'
'/NP'
)
if ($excludeDirs.Count -gt 0) {
$robocopyArgs += '/XD'
$robocopyArgs += $excludeDirs
}
if ($excludeFiles.Count -gt 0) {
$robocopyArgs += '/XF'
$robocopyArgs += $excludeFiles
}
$siteStopped = $false
$poolStopped = $false
try {
& $appcmd stop site "/site.name:$site"
if ($LASTEXITCODE -ne 0) {
Write-Warning "stop site fallito ($LASTEXITCODE) - si prosegue comunque"
} else {
$siteStopped = $true
}
& $appcmd stop apppool "/apppool.name:$appPool"
if ($LASTEXITCODE -ne 0) {
Write-Warning "stop apppool fallito ($LASTEXITCODE) - si prosegue comunque"
} else {
$poolStopped = $true
}
New-Item -ItemType Directory -Force -Path $dst | Out-Null
if ('${{ inputs.clean-destination }}' -eq 'true') {
$keepItems = @('appsettings.json', 'web.config', 'store')
Get-ChildItem -Path $dst | Where-Object { $_.Name -notin $keepItems } | Remove-Item -Recurse -Force
Write-Host "Pulizia destinazione completata (keep: $($keepItems -join ', '))"
}
& robocopy @robocopyArgs
$robocopyExitCode = $LASTEXITCODE
if ($robocopyExitCode -ge 8) {
throw "robocopy fallito ($robocopyExitCode)"
}
}
finally {
if ($poolStopped) {
& $appcmd start apppool "/apppool.name:$appPool"
if ($LASTEXITCODE -ne 0) { throw "start apppool fallito ($LASTEXITCODE)" }
}
if ($siteStopped) {
& $appcmd start site "/site.name:$site"
if ($LASTEXITCODE -ne 0) { throw "start site fallito ($LASTEXITCODE)" }
}
}