我们有一个以web应用程序项目为特色的解决方案,该项目被指定为Windows云服务中的web角色。它还支持一个只针对云服务(生产时隙)的云服务项目。
SlnRoot\WebApp1 1\WebApp1.csproj SlnRoot\CloudDeployment\CloudServiceName\CloudServiceName.ccproj
从Visual发布(部署)非常容易;只需选择发布.选项从云项目的上下文菜单中选择,然后使用所有预先配置的云服务设置单击“发布”。
现在,我们将进一步尝试自动化这个过程,因此我将在没有Visual帮助的情况下,从命令行和原始MSBuild中尝试它。
.nuget\nuget.exe restore
msbuild .\CloudDeployment\CloudServiceName\CloudServiceName.ccproj /t:Publish /p:PublishDir=..\..\pubout\ /fl1 /v:d但在实际上,发布目标是Visual中的Package选项,只生成必须手动上传到Windows门户的cspkg文件。当然,这是行不通的。是否有一个单独的目标来执行Visual那么容易执行的附加步骤(部署不是它;没有这样的目标)?
发布于 2014-10-23 15:41:43
谢谢你的建议。然而,我知识不足的真正答案--首先是MSBuild和PowerShell如何结合在一起--来自我的同事,他制作了一个定制的MSBuild proj文件,让所有这些文件一起工作。下面是一个基本示例,并附有注释
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Declare configuration properties for this deployment -->
<!-- This custom.proj file is in a sub-directory in solution root -->
<PropertyGroup>
<SolutionDir Condition=" '$(SolutionDir)'=='' ">$(MSBuildThisFileDirectory)..\</SolutionDir>
<SolutionPath Condition=" '$(SolutionPath)'=='' ">$(MSBuildThisFileDirectory)..\CloudService.sln</SolutionPath>
<OutDir Condition=" '$(OutDir)'=='' ">$(MSBuildThisFileDirectory)\Output\Binaries\</OutDir>
<PackageOutDir>$(MSBuildThisFileDirectory)Output\Packages\</PackageOutDir>
<TargetCloudService>targetcloudservice</TargetCloudService>
<DeployConfig>BuildConfig</DeployConfig>
<PubSettingsPath>$(MSBuildThisFileDirectory)subscription.publishsettings</PubSettingsPath>
<SubscriptionName>subscription name</SubscriptionName>
<StorageAccount>targetstorageaccount</StorageAccount>
</PropertyGroup>
<!-- Target to restore all Nuget packages on a clean repo pull. -->
<Target Name="RestorePackages">
<Message Text="Restoring nuget..."/>
<Exec Command=""$(SolutionDir).nuget\NuGet.exe" restore "$(SolutionPath)"" />
</Target>
<!--
Target to package the indicated cloud project,
which will build the referenced web role project first with desired build config.
-->
<Target Name="PackageCloud" DependsOnTargets="RestorePackages">
<Message Text="Creating package for cloud deployment ..."/>
<MSBuild
Projects="$(MSBuildThisFileDirectory)..\CloudDeployment\$(TargetCloudService)\$(TargetCloudService).ccproj"
Properties="OutputPath=$(PackageOutDir)$(TargetCloudService)\;Configuration=$(DeployConfig);"
Targets="Publish"/>
</Target>
<!--
Target to deploy the package produced by the dependency target.
This is the part that launches PowerShell to execute custom ps1 script
with all the cloud service parameters (MSBuild variables above)
and cspkg package for deployment.
The custom script uses the Azure module cmdlets to make service checks and publish.
-->
<Target Name="DeployCloud" DependsOnTargets="PackageCloud">
<Message Text="Deploying package to cloud service ..."/>
<Exec WorkingDirectory="$(MSBuildThisFileDirectory)"
Command="$(windir)\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -f $(MSBuildThisFileDirectory)PublishCloudService.ps1 -packageLocation "$(PackageOutDir)$(TargetCloudService)\app.publish\$(TargetCloudService).cspkg" -cloudConfigLocation "$(PackageOutDir)$(TargetCloudService)\app.publish\ServiceConfiguration.Cloud.cscfg" -subscriptionDataFile "$(PubSettingsPath)" -selectedsubscription "$(SubscriptionName)" -servicename $(TargetCloudService) -storageAccountName $(StorageAccount)" />
</Target>
</Project>因此,一次部署调用将类似于
msbuild.exe custom.proj /t:DeployCloud发布于 2014-09-25 07:06:42
获得CSPKG和CSCONFIG文件后,需要“手动”发布项目。MSBuild不发布该项目。您可以使用Azure PowerShell发布项目。出版-AzureService是您要寻找的cmdlet。
您还可以在Azure AD租户(每个Azure订阅都有)中配置一个(或多个)用户,并启用使用PowerShell的全自动部署,而不需要.publishsettings文件和客户端证书。看看我的带有Azure PowerShell和Azure广告博客的非交互式登录。
更新
使用PowerShell脚本在现有云服务和现有存储帐户上创建新部署相当可靠,并且易于使用:
Add-AzureAccount
Select-AzureSubscription "<subscription name>"
Set-AzureSubscription -SubscriptionName "<subscription name>" `
-CurrentStorageAccountName "<storage_account_name>"
New-AzureDeployment -ServiceName "<cloud_service_name>" `
-Package "D:/tmp/cloud/myservice.cspkg" `
-Configuration "D:/tmp/cloud/ServiceConfiguration.Cloud.cscfg" `
-Slot "Staging"以及升级脚本:
Add-AzureAccount
Select-AzureSubscription "<subscription name>"
Set-AzureSubscription -SubscriptionName "<subscription name>" `
-CurrentStorageAccountName "<storage_account_name>"
Set-AzureDeployment -Upgrade `
-ServiceName "<cloud_service_name>" `
-Package "D:/tmp/cloud/myservice.cspkg" `
-Configuration "D:/tmp/cloud/ServiceConfiguration.Cloud.cscfg" `
-Slot "Staging"对于Slot,您可以使用Staging或Production。对于使用发布设置文件的情况,只需将Add-AzureAccount替换为Import-AzurePublishSettingsFile即可。
注意,这些是经过验证的脚本,。
发布于 2014-09-28 18:38:06
正如astaykov所指出的,MSBuild自己并不知道如何部署到Azure,但是您可以使用安装Azure Powershell SDK来完成部署。
即使使用发布设置文件,仍然需要执行一些额外的命令来执行部署,而不仅仅是发布一个:
我在这里有一篇关于构建一个示例发布脚本的文章:自动部署到Azure托管服务
它将更深入地介绍如何使用这些脚本构建一个部署脚本,该脚本遵循“deployment和VIP交换到生产”的路径,但许多细节仍然与直接升级部署相关。
我对几个项目使用了类似的方法,但是更进一步,在msbuild调用和打包之间交换配置项目。
关于“发布设置”和凭据,我认为这取决于您将在何处运行这些构建(以及谁可以访问该环境),以及您是否对基于构建过程中的可移植性和可见性/访问的证书支持访问或一组凭据感到更舒服。不过,许多核心步骤将是相同的。
https://stackoverflow.com/questions/26030055
复制相似问题