使用 PowerShell 自動化 CloudServices 釋出

77rou發表於2016-06-21

在軟體的開發過程中,自動化的編譯和部署能夠帶來很多的優勢。如果可以通過一個指令碼實現軟體的自動化部署,那麼就可以節省大量的時間去做其它事情。

下面介紹如何將雲應用程式通過 PowerShell 自動釋出到 azure 的 cloud services 上。

一、打包需要釋出的內容

首先使用 msbuild 編譯 *.ccproj 檔案,在生成的所有檔案中,我們需要用到以下兩個:

app.publish\xxx.cspkg

app.publish\yyy.cscfg

二、下載 publishsettings 檔案

有以下兩種方法可以下載 publishsettings 檔案:

1、如果沒有 Azure 賬號,則需要先註冊賬號;如果已有 Azure 賬號,可直接登入下面的地址,下載 publishsettings 檔案(國際版):

https://manage.windowsazure.com/publishsettings/index

下載到的檔案的檔名:

xxx5-18-2016-credentials.publishsettings

其中xxx是你的 subscription 名稱。

2、在 powershell 中執行 Get-AzurePublishSettingsFile 命令,實現下載 publishsettings 檔案的目的。

三、安裝 powershell 的 azure module

訪問 https://azure.microsoft.com/en-us/downloads/#cmd-line-tools 網址, 點選 “Command-line tools->PowerShell” 下面的 “Windows install” 下載安裝包。

1

執行安裝包,安裝 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;

$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot;

//檢查部署是否成功

$completeDeploymentID = $completeDeployment.deploymentid;

8、從網站上檢視釋出結果

釋出完成後,可以從網站上檢視釋出結果。

2

其中,Deployment label 是在釋出指令碼中設定的,一般會寫入釋出日期和版本號;Deployment ID 是標識本次部署的 GUID。

 

總結,PowerShell 的 azure 模組已經提供了很完善的命令供我們進行自動化的釋出使用,我們只需要將這些命令組織成指令碼就可以了。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28298702/viewspace-2120591/,如需轉載,請註明出處,否則將追究法律責任。

相關文章