我正在使用以下脚本使用 powershell 部署我的 .cspkg。我已经创建了适当的证书并上传到 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 进程模板并使用执行任务来执行 powershell cmdlet。如果正在执行此任务,则会引发以下错误,
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.HostedServices.GetAzureDeploymentCommand !
在此脚本中,如果我使用 azure 发布设置文件,则 TFS 工作正常。如果我使用 certificate ,则会出现问题,但如果我使用 certificate 并手动执行它(没有 TFS process template )在服务器上工作正常。任何线索?
谢谢,阿伦