SharpLab/#build/Publish-All.ps1

116 строки
4.3 KiB
PowerShell
Исходник Обычный вид История

2016-01-16 03:05:52 +03:00
param (
2016-01-25 12:36:14 +03:00
[switch] [boolean] $azure
2016-01-16 03:05:52 +03:00
)
Set-StrictMode -Version 2.0
$ErrorActionPreference = 'Stop'
$ProgressPreference = "SilentlyContinue" # https://www.amido.com/powershell-win32-error-handle-invalid-0x6/
# Write-Host, Write-Error and Write-Warning do not function properly in Azure
# So this mostly uses Write-Output for now
$PublishToIIS = Resolve-Path "$PSScriptRoot\Publish-ToIIS.ps1"
$PublishToAzure = Resolve-Path "$PSScriptRoot\Publish-ToAzure.ps1"
2016-01-25 12:36:14 +03:00
function Login-ToAzure($azureConfig) {
$passwordKey = $env:TR_AZURE_PASSWORD_KEY
if (!$passwordKey) {
throw "Azure credentials require TR_AZURE_PASSWORD_KEY to be set."
}
$passwordKey = [Convert]::FromBase64String($passwordKey)
$password = $azureConfig.Password | ConvertTo-SecureString -Key $passwordKey
$credential = New-Object Management.Automation.PSCredential($azureConfig.UserName, $password)
"Logging to Azure as $($azureConfig.UserName)..." | Out-Default
Login-AzureRmAccount -Credential $credential | Out-Null
}
2016-01-16 03:05:52 +03:00
# Code ------
try {
$Host.UI.RawUI.WindowTitle = "Deploy TryRoslyn" # prevents title > 1024 char errors
Write-Output "Environment:"
Write-Output " Current Path: $(Get-Location)"
Write-Output " Script Root: $PSScriptRoot"
$sourceRoot = Resolve-Path "$PSScriptRoot\..\Source"
Write-Output " Source Root: $sourceRoot"
$sitesBuildRoot = Resolve-Path "$PSScriptRoot\..\!sites"
Write-Output " Sites Build Root: $sitesBuildRoot"
if ($azure) {
$azureConfigPath = ".\!Azure.config.json"
2016-01-25 12:36:14 +03:00
if (!(Test-Path $azureConfigPath)) {
throw "Path '$azureConfigPath' was not found."
}
$azureConfig = ConvertFrom-Json (Get-Content $azureConfigPath -Raw)
Login-ToAzure $azureConfig
2016-01-16 03:05:52 +03:00
}
$branchesJson = @()
Get-ChildItem $sitesBuildRoot | ? { $_ -is [IO.DirectoryInfo] } | % {
$branchFsName = $_.Name
$siteMainRoot = "$($_.FullName)\!site"
if (!(Test-Path $siteMainRoot) -or !(Get-ChildItem $siteMainRoot -Recurse | ? { $_ -is [IO.FileInfo] })) {
return
}
Write-Output ''
Write-Output "*** $_"
$siteMainRoot = Resolve-Path $siteMainRoot
$siteRoslynRoot = Resolve-Path "$($_.FullName)\!roslyn"
$branchInfo = ConvertFrom-Json ([IO.File]::ReadAllText("$siteRoslynRoot\!BranchInfo.json"))
$webAppName = "tr-b-dotnet-$($branchFsName.ToLowerInvariant())"
$iisSiteName = "$webAppName.tryroslyn.local"
$url = "http://$iisSiteName"
&$PublishToIIS -SiteName $iisSiteName -SourcePath $siteMainRoot
if ($azure) {
&$PublishToAzure `
2016-01-25 12:36:14 +03:00
-ResourceGroupName $($azureConfig.ResourceGroupName) `
-AppServicePlanName $($azureConfig.AppServicePlanName) `
2016-01-16 03:05:52 +03:00
-WebAppName $webAppName `
-CanCreateWebApp `
-CanStopWebApp `
2016-01-16 03:05:52 +03:00
-SourcePath $siteMainRoot `
-TargetPath "."
$url = "http://$($webAppName).azurewebsites.net"
}
# Success!
2016-04-19 12:23:47 +03:00
$branchesJson += [ordered]@{
id = $branchFsName
2016-01-16 03:05:52 +03:00
name = $branchInfo.name
group = $branchInfo.repository
2016-01-16 03:05:52 +03:00
url = $url
commits = $branchInfo.commits
}
}
2016-04-19 12:23:47 +03:00
$branchesFileName = "!branches.json"
Write-Output "Updating $branchesFileName..."
Set-Content "$sitesBuildRoot\$branchesFileName" $(ConvertTo-Json $branchesJson -Depth 100)
2016-01-29 11:24:23 +03:00
$brachesJsLocalRoot = "$sourceRoot\Web\wwwroot"
if (!(Test-Path $brachesJsLocalRoot)) {
New-Item -ItemType Directory -Path $brachesJsLocalRoot | Out-Null
}
Copy-Item "$sitesBuildRoot\$branchesFileName" "$brachesJsLocalRoot\$branchesFileName" -Force
2016-01-29 11:24:23 +03:00
2016-01-16 03:05:52 +03:00
if ($azure) {
&$PublishToAzure `
2016-01-25 12:36:14 +03:00
-ResourceGroupName $($azureConfig.ResourceGroupName) `
-AppServicePlanName $($azureConfig.AppServicePlanName) `
2016-01-16 03:05:52 +03:00
-WebAppName "tryroslyn" `
2016-04-19 12:23:47 +03:00
-SourcePath "$sitesBuildRoot\$branchesFileName" `
-TargetPath "wwwroot\$branchesFileName"
2016-01-16 03:05:52 +03:00
}
}
catch {
Write-Output "[ERROR] $_"
Write-Output 'Returning exit code 1'
exit 1
}