Can package Windows Azure cloud project from the command line, but not deploy on Windows Azure itself

We have a solution with a web application project designed as a web role in the Windows Azure cloud service. He also has a cloud services project designed only for a cloud service (production slot)

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

Publishing (deploying) from Visual Studio is very simple; just select the "Publish ..." option in the context menu of the cloud project and click "Publish" with all the pre-configured settings for cloud services.

Now we take another step, trying to automate this process, so I try to execute it from the command line and raw MSBuild without the help of Visual Studio.

.nuget\nuget.exe restore msbuild .\CloudDeployment\CloudServiceName\CloudServiceName.ccproj /t:Publish /p:PublishDir=..\..\pubout\ /fl1 /v:d 

But it looks like the Publish object is actually a Package option in Visual Studio, creating only the cspkg file, which must be manually uploaded to the Windows Azure portal. Of course, this will not happen. Is there a separate purpose for pointing out an additional step (Deployment right?) That Visual Studio performs so easily?

+5
source share
4 answers

Thank you for your advice. The true answer to the gap in my knowledge - like, in fact, both MSBuild and PowerShell, in the first place - is my colleague who created his own MSBuild proj file so that it all works together. Below is a basic example with comments

 <?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> 

So a one-time deployment call will look like

 msbuild.exe custom.proj /t:DeployCloud 
+5
source

After you receive the CSPKG and CSCONFIG files, you need to manually publish your project. MSBuild does not publish the project. You can use Azure PowerShell to publish a project. Publish-AzureService is the cmdlet you are looking for.

You can also configure one (or several) users in your Azure AD tenant (what every Azure subscription has) and enable fully automatic deployment using PowerShell without the need for a .publishsettings file and client certificates. Check out my Silent Login with Azure PowerShell and Azure AD message .

UPDATE

A fairly simple and easy-to-use PowerShell Script to create a new deployment in an existing cloud service and existing storage account:

 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" 

And update Script:

 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" 

For Slot you can use Staging or Production . For the case when you are using the publish settings file, simply replace Add-AzureAccount with Import-AzurePublishSettingsFile .

Please note that these are proven scripts .

+3
source

As Astaikov noted, MSBuild itself does not know how to deploy Azure, but you can install the Azure Powershell SDK deployment.

Even using the publication file, there are still some additional commands that need to be executed for a deployment that goes beyond just published:

I have a post about creating a sample publication script here: Automatic Deployment for Azure Hosted Services

Looks at this process in more detail to use these scenarios when building a deployment script that follows the path of β€œdeploying to an intermediate and VIP exchange into production,” but many details are still relevant for direct upgrade deployment.

I use a similar method for several projects, but take one more step and replace the configuration projects between the msbuild call and the packaging.

In the "publish settings" and credentials, I think it boils down to where you are going to run these assemblies (and who has access to this environment), and whether you feel more comfortable with a certificate that allows access or a set of credentials, based on portability and visibility / access during the build process. However, many basic steps will be the same.

+3
source

A good example script that deploys to the Azure Cloud Service from the vNext source code assembly: Publish-AzureCloudDeployment.ps1

0
source

Source: https://habr.com/ru/post/1203352/


All Articles