-1

我正在使用 TeamCity 构建自动化流程。我的计划是在部署到 prod 服务器之前创建一个备份文件夹:

  1. 停止 IIS 并复制或备份 IIS 网站物理路径文件夹 VersionNo 为 X(使用 Nant 或 Powershell)
  2. 将IIS网站物理路径文件夹重命名为VersionNo为X+1(使用Nant或Powershell) 3.部署到IIS网站物理路径文件夹X+1
  3. 重新启动 IIS

如何在 Nant 中使用 Nant 或 PowerShell 脚本来完成此任务?任何帮助将是一个很大的帮助。谢谢

4

1 回答 1

0

就 Powershell 脚本而言,该脚本将有助于 IIS 步骤和备份。

Import-Module WebAdministration

function Get-SiteName
{    
    [cmdletBinding()]
    param(
        [Parameter(Mandatory)]
        $site)
    if(Test-Path IIS:\Sites\$site)
    {
        Write-Host "The provided website name is $site and it is a valid website`r`n" -ForegroundColor Cyan
    }
    else
    {
        Write-Host "There is not a website present in the name provided`r`n" -ForegroundColor Red
        Exit
    }
}

function Stop-AppPool
{
    # Get the app pool of the website
    $appPool = Get-Item IIS:\Sites\$site | Select-Object applicationPool

    # Get the app pool name
    $appPoolName = $appPool.applicationPool

    # Stop the app pool. If already stopped then a message will be shown
    if ((Get-WebAppPoolState $appPoolName).Value -ne "Stopped")
    {
        Stop-WebAppPool $appPoolName
        Write-Host "App pool $appPoolName is $((Get-WebAppPoolState $appPoolName).Value)"
    }
    else
    {
        Write-Host "App pool $appPoolName is already stopped`r`nNo need to stop it again`r`n" -ForegroundColor DarkGreen
    }
}

function Test-IsStoppedOrNot
{
    # Check the status of the app pool before taking backup
    sleep -Seconds 30
    if ((Get-WebAppPoolState $appPoolName).Value -eq "Stopped")
    {
        Write-Host "App pool $appPoolName is in stopped state`r`nBackup and Deploy can start`r`n" -ForegroundColor Cyan
    }
    else
    {
        Write-Host "App pool is not yet stopped`r`nTry after sometime`r`n" -ForegroundColor Red
        Exit
    }
}

function Make-BackupFolder
{
    # Get the website physical folder of the website
    $SiteFolder = Get-Item IIS:\Sites\$site
    $SiteFolderName = $SiteFolder.physicalPath
    Write-Host "The physical folder of the website is $SiteFolderName" -ForegroundColor DarkCyan

    # Forming backup folder name

    $backupFolder = $site + "_" + (Get-Date).ToString('MM.dd.yyyy.hh.mm.ss')

    # Creating backup folder path

    $DriveName = Split-Path -Path $SiteFolderName -Qualifier

    $backupFolderPath = Join-Path "$DriveName" -ChildPath "Backup" | Join-Path -ChildPath "$backupFolder"

    # Creating backup folder

    mkdir $backupFolderPath
}

function Test-IsBackupFolderCreatedOrNot
{
    # Confirm if the backup folder is created

    if (Test-Path $backupFolderPath)
    {
        Write-Host "Backup folder $backupFolderPath is created`r`n" -ForegroundColor DarkGreen
        Get-ChildItem $SiteFolderName | Copy-Item -Destination $backupFolderPath
        Write-Host "Backup is complete`r`n" -ForegroundColor Blue
        Write-Host "Perform the deployment`r`n" -ForegroundColor Cyan
    }
    else
    {
        Write-Host "Backup folder is not created`r`nTake a look please" -ForegroundColor Red
        Exit
    }
}

# Start the app pool. If already started then a message will be shown
function Start-AppPool
{
    if ((Get-WebAppPoolState $appPoolName).Value -ne "Started")
    {
        Start-WebAppPool $appPoolName
        Write-Host "App pool $appPoolName is $((Get-WebAppPoolState $appPoolName).Value)"
    }
    else
    {
        Write-Host "App pool $appPoolName is already started`r`nNo need to start it again`r`n" -ForegroundColor DarkGreen
        Write-Host "THE DEPLOYMENT IS COMPLETE`r`n" -ForegroundColor DarkBlue        
    }
}

function Test-IsStartedOrNot
{
    # Check the status of the app pool once again
    sleep -Seconds 30
    if ((Get-WebAppPoolState $appPoolName).Value -eq "Started")
    {
        Write-Host "App pool $appPoolName is not in started state yet`r`nTAKE A LOOK" -ForegroundColor Cyan
    }
    else
    {
        Write-Host "App pool is in started state`r`n" -ForegroundColor Red
        Write-Host "THE DEPLOYMENT IS COMPLETE`r`n" -ForegroundColor DarkBlue
    }
}

function Confirm-Deployment
{
    [cmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateSet('true','false','yes','no','1','0')]
        $CanProceed)

    if (($CanProceed -eq 'true') -or ($CanProceed -eq 'yes') -or ($CanProceed -eq '0'))
    {
        Start-AppPool
    }
}

Get-SiteName

Stop-AppPool

Test-IsStoppedOrNot

Make-BackupFolder

Confirm-Deployment

Test-IsStartedOrNot

该脚本执行以下操作:

  1. 从用户那里获取站点名称
  2. 如果站点名称不是有效的网站名称,脚本将终止
  3. 根据提供的站点名称,将确定站点的应用程序池和物理文件夹。
  4. 应用程序池已停止
  5. 等待 30 秒并检查应用程序池是否已停止
  6. 将使用物理文件夹路径导出备份文件夹路径。例如,如果网站路径是 D:\Sites\MySite,那么备份文件夹将位于 D:\Backup。备份文件夹名称为 SiteName+_+Date。日期的格式为month.day.year.hour.min.seconds。
  7. 执行备份
  8. 备份后,您需要按照自己的方式进行部署。脚本不进行部署。
  9. 该脚本会提示您确认您已执行部署。
  10. 如果您通过输入 true 或 yes 或 0 确认部署已完成,则脚本将启动应用程序池。
  11. 等待 30 秒,然后检查应用程序池是否已启动。如果不是,它会让你知道同样的事情。

请再次注意,脚本不负责您的部署。您需要进行部署。在您执行部署之前,您可以保持 POWERSHELL 会话打开。

此外,该脚本不会删除网站文件夹的内容。请根据需要包括该步骤。

希望这可以帮助。

于 2017-02-14T11:31:49.117 回答