本文介绍了如何将云应用程序通过 PowerShell 自动发布到 azure 的 cloud services 上。

在软件的开发过程中,自动化的编译和部署能够带来很多的优势。如果可以通过一个脚本实现软件的自动化部署,那么就可以节省大量的时间去做其它事情。

下面介绍如何将云应用程序通过 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” 下载安装包。

使用 PowerShell 自动化 CloudServices 发布_publishsettings

运行安装包,安装 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、从网站上查看发布结果

发布完成后,可以从网站上查看发布结果。

使用 PowerShell 自动化 CloudServices 发布_CloudServices_02

其中,Deployment label 是在发布脚本中设置的,一般会写入发布日期和版本号;Deployment ID 是标识本次部署的 GUID。

总结,PowerShell 的 azure 模块已经提供了很完善的命令供我们进行自动化的发布使用,我们只需要将这些命令组织成脚本就可以了。