Skip to content

Instantly share code, notes, and snippets.

@CJHarmath
Last active October 14, 2021 02:59
Show Gist options
  • Save CJHarmath/b2af0f50700ce9fbdd8c5c3e582fd41b to your computer and use it in GitHub Desktop.
Save CJHarmath/b2af0f50700ce9fbdd8c5c3e582fd41b to your computer and use it in GitHub Desktop.

Revisions

  1. CJHarmath revised this gist Feb 1, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions AspNetCore-IISDeploy.ps1
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Setup
    Import-Module WebAdministration
    # create 2 site root directories
    $a = 'C:\inetpub\AspNetCoreSampleA'
    $b = 'C:\inetpub\AspNetCoreSampleB'
  2. CJHarmath created this gist Feb 1, 2019.
    34 changes: 34 additions & 0 deletions AspNetCore-IISDeploy.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # Setup
    # create 2 site root directories
    $a = 'C:\inetpub\AspNetCoreSampleA'
    $b = 'C:\inetpub\AspNetCoreSampleB'
    $siteRoot = 'C:\inetpub\aspnetcoresample'
    $siteName = 'AspNetCoreSample'
    $poolName = "aspnetcore"
    New-Item -Type Directory $a
    New-Item -Type Directory $b
    # create a symlink to targeting one side
    New-Item -Type SymbolicLink -Path $siteRoot -Target $a
    # point the site root to the symlink
    Set-ItemProperty "IIS:\Sites\$siteName" -name physicalPath -value $siteRoot
    # make sure it get's picked up
    Restart-WebAppPool -Name $poolName

    # this tells you the active side
    Get-Item -Path $siteRoot | Select-Object -ExpandProperty target

    # Flip the symlink
    $current = (Get-Item -Path $siteRoot).Target
    $newTarget = if ($current -eq $a) {$b} else {$a}
    New-Item -Type SymbolicLink -Path $siteRoot -Target $newTarget -Force
    # at this point w3wp.exe still locks the current target folder until it's getting recycled
    # Deploy new version to the symlink which is now pointing to the other side which should have no locks
    robocopy \\myshare\myapp $siteRoot /mir
    # recycle app pool, so it picks up the new files
    Restart-WebAppPool -Name $poolName

    # bonus point: rollback is easy
    $current = (Get-Item -Path $siteRoot).Target
    $newTarget = if ($current -eq $a) {$b} else {$a}
    New-Item -Type SymbolicLink -Path $siteRoot -Target $newTarget -Force
    Restart-WebAppPool -Name $poolName