在軟體的開發過程中,自動化的編譯和部署能夠帶來很多的優勢。如果可以通過一個腳本實作軟體的自動化部署,那麼就可以節省大量的時間去做其它事情。
下面介紹如何将雲應用程式通過 PowerShell 自動釋出到 azure 的 cloud services 上。
一、打包需要釋出的内容
首先使用 msbuild 編譯 *.ccproj 檔案,在生成的所有檔案中,我們需要用到以下兩個:
app.publish\xxx.cspkg
app.publish\yyy.cscfg
二、下載下傳 publishsettings 檔案
有以下兩種方法可以下載下傳 publishsettings 檔案:
1、如果沒有 Azure 賬号,則需要先新增賬號;如果已有 Azure 賬号,可直接登入下面的位址,下載下傳 publishsettings 檔案(國際版):
<a href="https://manage.windowsazure.com/publishsettings/index">https://manage.windowsazure.com/publishsettings/index</a>
下載下傳到的檔案的檔案名:
xxx5-18-2016-credentials.publishsettings
其中xxx是你的 subscription 名稱。
2、在 powershell 中執行 Get-AzurePublishSettingsFile 指令,實作下載下傳 publishsettings 檔案的目的。
三、安裝 powershell 的 azure module
運作安裝包,安裝 azure modules。
四、建立自動釋出的腳本
1、導入 azure module
在 powershell 中執行指令 Import-Module Azure,導入 azure module
2、設定腳本中使用的變量,其中部分參數變量需要根據自己的資訊設定
$package = app.publish\xxx.cspkg
$configuration = app.publish\yyy.cscfg
# subscription 名稱
$subscription = "your subscription name";
# service 名稱
$service = "your service name";
# storage account
$storage = "your storage account";
# slot 名稱,一般會先發到 staging 中,檢查後再進行切換
$slot = "Staging";
# 為每次釋出提供一個說明資訊
$deploymentLabel = “your demplyment label”
3、導入 publish settings
因為 publish settings 檔案中記錄了 subscription 資訊以及用于登入的驗證資訊,是以需要先把這些資訊導入進來。
執行指令:Import-AzurePublishSettingsFile publishsettings-file-path
需要注意的是:
在導入前需要先檢查一下,檢視這個檔案對應的 subscription 是否已被導入,可以通過以下指令進行驗證。
$thisSubscriptionExist = $False
$subs = Get-AzureSubscription
if ($subs.Count - gt 0)
{
Foreach($sub in $subs)
{
if ($sub.SubscriptionName - eq $subscription)
{
$thisSubscriptionExist = $True
}
}
}
如果不存在,則需要執行導入操作;如果存在,則直接進行下一步。
if (!$thisSubscriptionExist)
Import - AzurePublishSettingsFile $subscriptionSetting
// 為subscription 添加一個storage account
Set - AzureSubscription - CurrentStorageAccount $storage - SubscriptionName $subscription
4、設定目前的 subscription
從上一步中可以發現,機器上可能同時儲存了多個 subscription 的資訊。那麼,當執行釋出操作時,預設會使用哪個 subscription 的資訊呢?這裡存在“目前 subscription”的概念,釋出操作會使用目前 subscription 的資訊進行釋出。是以,在釋出操作之前一定要設定本次釋出使用的 subscription 為目前 subscription。
執行 Select-AzureSubscription -SubscriptionName $subscription –Current 指令進行設定
5、檢查 deployment 是否存在
在執行部署前需要先檢查 deployment 是否存在,這會影響到後面的部署方式。如果 deployment 不存在,則需要先建立 deployment。如果 deployment 已經存在,則需要更新 deployment。
指令邏輯如下:
$deployment = Get-AzureDeployment -ServiceName $service -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue
if ($deployment.Name -ne $null)
# deployment 已經存在,使用 Set-AzureDeployment 指令進行更新,第7步會詳細說明
else
# 需要使用 New-AzureDeployment 指令建立 deployment,第6步會詳細說明
6、建立 deployment 并檢查部署是否成功的指令
New-AzureDeployment -Slot $slot -Package $package -Configuration $configuration -label $deploymentLabel -ServiceName $service;
$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot;
//檢查部署是否成功
$completeDeploymentID = $completeDeployment.deploymentid;
7、更新已經存在的部署并檢查部署是否成功的指令
Set-AzureDeployment -Upgrade -Slot $slot -Package $package -Configuration $configuration -label $deploymentLabel -ServiceName $service -Force;
8、從網站上檢視釋出結果
釋出完成後,可以從網站上檢視釋出結果。
其中,Deployment label 是在釋出腳本中設定的,一般會寫入釋出日期和版本号;Deployment ID 是辨別本次部署的 GUID。
總結,PowerShell 的 azure 子產品已經提供了很完善的指令供我們進行自動化的釋出使用,我們隻需要将這些指令組織成腳本就可以了。