首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以从命令行打包Windows云项目,但不能部署到Windows本身

可以从命令行打包Windows云项目,但不能部署到Windows本身
EN

Stack Overflow用户
提问于 2014-09-25 03:36:05
回答 4查看 5.5K关注 0票数 5

我们有一个以web应用程序项目为特色的解决方案,该项目被指定为Windows云服务中的web角色。它还支持一个只针对云服务(生产时隙)的云服务项目。

SlnRoot\WebApp1 1\WebApp1.csproj SlnRoot\CloudDeployment\CloudServiceName\CloudServiceName.ccproj

从Visual发布(部署)非常容易;只需选择发布.选项从云项目的上下文菜单中选择,然后使用所有预先配置的云服务设置单击“发布”。

现在,我们将进一步尝试自动化这个过程,因此我将在没有Visual帮助的情况下,从命令行和原始MSBuild中尝试它。

代码语言:javascript
复制
.nuget\nuget.exe restore
msbuild .\CloudDeployment\CloudServiceName\CloudServiceName.ccproj /t:Publish /p:PublishDir=..\..\pubout\ /fl1 /v:d

但在实际上,发布目标是Visual中的Package选项,只生成必须手动上传到Windows门户的cspkg文件。当然,这是行不通的。是否有一个单独的目标来执行Visual那么容易执行的附加步骤(部署不是它;没有这样的目标)?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-10-23 15:41:43

谢谢你的建议。然而,我知识不足的真正答案--首先是MSBuild和PowerShell如何结合在一起--来自我的同事,他制作了一个定制的MSBuild proj文件,让所有这些文件一起工作。下面是一个基本示例,并附有注释

代码语言:javascript
复制
<?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="&quot;$(SolutionDir).nuget\NuGet.exe&quot; restore &quot;$(SolutionPath)&quot;" />
  </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 &quot;$(PackageOutDir)$(TargetCloudService)\app.publish\$(TargetCloudService).cspkg&quot; -cloudConfigLocation &quot;$(PackageOutDir)$(TargetCloudService)\app.publish\ServiceConfiguration.Cloud.cscfg&quot; -subscriptionDataFile &quot;$(PubSettingsPath)&quot; -selectedsubscription &quot;$(SubscriptionName)&quot; -servicename $(TargetCloudService) -storageAccountName $(StorageAccount)" />
  </Target>
</Project>

因此,一次部署调用将类似于

代码语言:javascript
复制
msbuild.exe custom.proj /t:DeployCloud
票数 5
EN

Stack Overflow用户

发布于 2014-09-25 07:06:42

获得CSPKG和CSCONFIG文件后,需要“手动”发布项目。MSBuild不发布该项目。您可以使用Azure PowerShell发布项目。出版-AzureService是您要寻找的cmdlet。

您还可以在Azure AD租户(每个Azure订阅都有)中配置一个(或多个)用户,并启用使用PowerShell的全自动部署,而不需要.publishsettings文件和客户端证书。看看我的带有Azure PowerShell和Azure广告博客的非交互式登录

更新

使用PowerShell脚本在现有云服务和现有存储帐户上创建新部署相当可靠,并且易于使用:

代码语言:javascript
复制
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"

以及升级脚本:

代码语言:javascript
复制
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,您可以使用StagingProduction。对于使用发布设置文件的情况,只需将Add-AzureAccount替换为Import-AzurePublishSettingsFile即可。

注意,这些是经过验证的脚本,

票数 3
EN

Stack Overflow用户

发布于 2014-09-28 18:38:06

正如astaykov所指出的,MSBuild自己并不知道如何部署到Azure,但是您可以使用安装Azure Powershell SDK来完成部署。

即使使用发布设置文件,仍然需要执行一些额外的命令来执行部署,而不仅仅是发布一个:

  • 导入-AzurePublishSettingsFile -使用这些设置
  • Set-AzureSubscription -用于设置默认存储帐户(在那里您将从msbuild上传包)
  • 选择-AzureSubscription -用于更新powershell上下文的订阅
  • 获取-AzureStorageContainer -获取要将包上传到的容器
  • 新型AzureStorageContainer -如果容器不存在的话很有用
  • 集合-AzureStorageBlobContent -将包上传到blob存储区
  • 获得-AzureStorageBlob -阅读有关blob的信息,喜欢它的Uri
  • 获得-AzureDeployment就业 -获取有关服务时隙中的部署的信息--在就业前非常有用,等待实例启动等。
  • 删除-AzureDeployment -移除部署--如果发布到暂存和VIP交换到生产中,则非常有用。
  • 新-阿祖雷就业 -从服务中的给定包(blob)、配置等创建新部署
  • 调动-AzureDeployment - VIP交换中转/生产插槽
  • Set-AzureDeployment -可用于更改部署状态。

我在这里有一篇关于构建一个示例发布脚本的文章:自动部署到Azure托管服务

它将更深入地介绍如何使用这些脚本构建一个部署脚本,该脚本遵循“deployment和VIP交换到生产”的路径,但许多细节仍然与直接升级部署相关。

我对几个项目使用了类似的方法,但是更进一步,在msbuild调用和打包之间交换配置项目。

关于“发布设置”和凭据,我认为这取决于您将在何处运行这些构建(以及谁可以访问该环境),以及您是否对基于构建过程中的可移植性和可见性/访问的证书支持访问或一组凭据感到更舒服。不过,许多核心步骤将是相同的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26030055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档