Развертывание Azure с использованием powershell. Ошибка: Get-AzureDeployment: при отправке запроса произошла ошибка.
Я использую приведенный ниже скрипт для развертывания моего.cspkg с помощью powershell. Я создал соответствующий сертификат и загрузил его на портал Azure, который был установлен на сервере сборки.
param
(
[string]$subscriptionID = "",
[string]$subscriptionName = "XXXXXXX",
[string]$thumbprint = "",
[string]$serviceName = "",
[string]$slot = "Production",
[string]$storageAccountName ="",
[string]$packageLocation = "",
[string]$serviceConfiguration = "",
[string]$certificateStore = "cert:\localmachine\root",
[string]$timeStampFormat = "g",
[string]$upgradeMode = "Auto",
[string]$action="deploy",
[int]$alwaysDeleteExistingDeployments = 1,
[int]$enableDeploymentUpgrade = 1,
[string]$tag = ""
)
$certThumbprint = $thumbprint.ToUpper()
$certPath = $certificateStore + "\\" + $certThumbprint
$cert = get-item $certPath
$buildLabel = ""
$packageName = ""
$a = Get-Date
if ($tag -eq "")
{
$buildLabel = "Daily Build" + "-" + $a.ToShortDateString() + "-" + $a.ToShortTimeString()
$packageName = $serviceName + $a.ToString("yyyyMMdd") + ".cspkg"
}
else
{
$buildLabel = "BuildTag-" + $tag + "-" + $a.ToShortDateString() + "-" + $a.ToShortTimeString()
$packageName = $serviceName + "-" + $tag + "-" + $a.ToString("yyyyMMdd") + ".cspkg"
}
Get-ChildItem "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\*.psd1" | ForEach-Object {Import-Module $_}
function Publish()
{
$deployment = Get-AzureDeployment -ServiceName $serviceName -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue
if ($a[0] -ne $null)
{
Write-Output "$(Get-Date -f $timeStampFormat) - No deployment is detected. Creating a new deployment. "
}
#check for existing deployment and then either upgrade, delete + deploy, or cancel according to $alwaysDeleteExistingDeployments and $enableDeploymentUpgrade boolean variables
if ($deployment.Name -ne $null)
{
switch ($alwaysDeleteExistingDeployments)
{
1
{
switch ($enableDeploymentUpgrade)
{
1 #Update deployment inplace (usually faster, cheaper, won't destroy VIP)
{
Write-Output "$(Get-Date -f $timeStampFormat) - Deployment exists in $servicename. Upgrading deployment."
UpgradeDeployment
}
0 #Delete then create new deployment
{
Write-Output "$(Get-Date -f $timeStampFormat) - Deployment exists in $servicename. Deleting deployment."
DeleteDeployment
CreateNewDeployment
}
} # switch ($enableDeploymentUpgrade)
}
0
{
Write-Output "$(Get-Date -f $timeStampFormat) - ERROR: Deployment exists in $servicename. Script execution cancelled."
#exit
}
} #switch ($alwaysDeleteExistingDeployments)
} else {
CreateNewDeployment
}
}
function CreateNewDeployment()
{
write-progress -id 3 -activity "Creating New Deployment" -Status "In progress"
Write-Output "$(Get-Date -f $timeStampFormat) - Creating New Deployment: In progress"
$opstat = New-AzureDeployment -Slot $slot -Package $packageLocation -Configuration $serviceConfiguration -label $buildLabel -ServiceName $serviceName -ErrorAction stop
$completeDeployment = Get-AzureDeployment -ServiceName $serviceName -Slot $slot
$completeDeploymentID = $completeDeployment.deploymentid
write-progress -id 3 -activity "Creating New Deployment" -completed -Status "Complete"
Write-Output "$(Get-Date -f $timeStampFormat) - Creating New Deployment: Complete, Deployment ID: $completeDeploymentID"
}
function UpgradeDeployment()
{
write-progress -id 3 -activity "Upgrading Deployment" -Status "In progress"
Write-Output "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: In progress"
$configFile = Get-Item $serviceConfiguration
# perform Update-Deployment
$setdeployment = Set-AzureDeployment -Upgrade `
-Mode $upgradeMode `
-Slot $slot `
-Package (Get-Item $packageLocation).FullName `
-Configuration $configFile.FullName `
-label $buildLabel `
-ServiceName $serviceName -Force -ErrorAction stop
$completeDeployment = Get-AzureDeployment -ServiceName $serviceName -Slot $slot
$completeDeploymentID = $completeDeployment.deploymentid
write-progress -id 3 -activity "Upgrading Deployment" -completed -Status "Complete"
Write-Output "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: Complete, Deployment ID: $completeDeploymentID"
}
function DeleteDeployment()
{
write-progress -id 2 -activity "Deleting Deployment" -Status "In progress"
Write-Output "$(Get-Date -f $timeStampFormat) - Deleting Deployment: In progress"
#WARNING - always deletes with force
$removeDeployment = Remove-AzureDeployment -Slot $slot -ServiceName $serviceName -Force -ErrorAction stop
write-progress -id 2 -activity "Deleting Deployment: Complete" -completed -Status $removeDeployment
Write-Output "$(Get-Date -f $timeStampFormat) - Deleting Deployment: Complete"
}
Remove-AzureSubscription -SubscriptionName $SubscriptionName -Force
#configure powershell with publishsettings for your subscription
Set-AzureSubscription -SubscriptionName $SubscriptionName -SubscriptionId $subscriptionID -Certificate $cert -CurrentStorageAccountName $storageAccountName
#set remaining environment variables for Azure cmdlets
$subscription = Select-AzureSubscription $SubscriptionName
#main driver - publish & write progress to activity log
Write-Output "$(Get-Date -f $timeStampFormat) - Azure Cloud Service deploy script started."
if ($action -eq "deploy") {
Write-Output "$(Get-Date -f $timeStampFormat) - Preparing deployment of $buildLabel for $subscriptionName with Subscription ID $subscriptionID."
Publish
$deployment = Get-AzureDeployment -slot $slot -serviceName $servicename
$deploymentUrl = $deployment.Url
Write-Output "$(Get-Date -f $timeStampFormat) - Created Cloud Service with URL $deploymentUrl."
Write-Output "$(Get-Date -f $timeStampFormat) - Azure Cloud Service deploy script finished."
} elseif ($action -eq "delete") {
Write-Output "$(Get-Date -f $timeStampFormat) - Preparing to delete cloudservice in $serviceName,$slot for sub: $subscriptionName,$subscriptionID"
$deployment = Get-AzureDeployment -ServiceName $serviceName -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue
if ($a[0] -ne $null)
{
Write-Output "$(Get-Date -f $timeStampFormat) - No deployment found"
} else {
DeleteDeployment
Write-Output "$(Get-Date -f $timeStampFormat) - Deleted successfully"
}
}
Если я выполняю этот скрипт вручную на сервере сборки, он работает нормально. Я настроил шаблон процесса tfs и использую задачу execute для выполнения командлетов powershell. если эта задача выполняется, была выдана следующая ошибка,
Get-AzureDeployment : An error occurred while sending the request.
В E:\B\61\378\src\PPM Azure\Production\Web\PSScript\PublishClo udServiceThumPrint.ps1:155 char:3 + Get-AzureDeployment -ServiceName "ppmcd" -Slot "staging" # -ErrorVariable a -E ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: NotSpecified: (:) [Get-AzureDeployment], HttpReq uestException + FullyQualifiedErrorId: System.Net.Http.HttpRequestException,Microsoft.W indowsAzure.Commands.ServiceManagement.epetManagement.HetService!
В этом сценарии, если я использую файл настроек публикации Azure, все работает нормально с TFS. Если я использую сертификат, проблема возникает, хотя, если я использую сертификат и выполняю его вручную (без шаблона процесса TFS), он нормально работает на сервере. Есть какие-нибудь подсказки?
Спасибо арун
1 ответ
Наконец мне удалось решить эту проблему. Проблема в том, что я хранил сертификацию в Trusted Root ("cert: \ localmachine \ root") и ссылался на нее в PowerShell, а просто изменил ее на "cert: \ localmachine \ my", все стало работать нормально.