Skip to content

Instantly share code, notes, and snippets.

@RaccoonDev
Created April 21, 2015 20:14
Show Gist options
  • Save RaccoonDev/2bb8d1dd794520ee4408 to your computer and use it in GitHub Desktop.
Save RaccoonDev/2bb8d1dd794520ee4408 to your computer and use it in GitHub Desktop.
Publish ASP.Net MVC Application to Azure WebAps. CI ready PowerShell scripts.
Param(
[string][Parameter(Mandatory=$true)] $WebSiteName,
[string] $ResourceGroupName = $WebSiteName,
[string] $StorageAccountName = $ResourceGroupName.ToLowerInvariant() + "storage",
[string] $ResourceGroupLocation = "West Europe",
[string] $StorageContainerName = $WebSiteName.ToLowerInvariant(),
[string] $TemplateFile = '.\Templates\WebSiteDeploySQL.json'
)
$ErrorActionPreference = "Stop";
$TemplateFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateFile);
#Define SQL server
$sqlServerName = $WebSiteName.toLowerInvariant() + "server";
$sqlDbName = $WebSiteName.toLowerInvariant() + "db";
$sqlServerAdminLogin = "userDB"
$plainTextPassword = "P{0}!" -f ([System.Guid]::NewGuid()).Guid.Replace("-", "").Substring(0, 10);
$sqlServerAdminPassword = ConvertTo-SecureString $plainTextPassword -AsPlainText -Force
Switch-AzureMode AzureResourceManager;
$VerbosePreference = "Continue";
New-AzureResourceGroup -Name $ResourceGroupName `
-Location $ResourceGroupLocation `
-TemplateFile $TemplateFile `
-sqlServerName $sqlServerName `
-sqlServerLocation $ResourceGroupLocation `
-sqlServerAdminLogin $sqlServerAdminLogin `
-sqlServerAdminPassword $sqlServerAdminPassword `
-sqlDbName $sqlDbName `
-webSiteName $webSiteName `
-webSiteLocation $ResourceGroupLocation `
-webSiteHostingPlanName "Standard2instances" `
-webSiteHostingPlanSKU "Standard" `
-storageAccountNameFromTemplate $StorageAccountName `
-Force `
-Verbose;
Param(
[string][Parameter(Mandatory=$true)] $WebSiteName,
[string][Parameter(Mandatory=$true)] $StorageContainerName,
[string] $ResourceGroupName = $WebSiteName,
[string] $StorageAccountName = $ResourceGroupName.ToLowerInvariant() + "storage",
[string] $webSitePackage = "CustomerManager.zip",
[string] $TemplateFile = '.\Templates\PublishWebApp.json'
)
$ErrorActionPreference = "Stop";
$wasServiceManagementMode = Get-Module -Name Azure -ListAvailable;
$TemplateFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateFile);
Switch-AzureMode -Name AzureServiceManagement;
$storageAccountKey = (Get-AzureStorageKey -StorageAccountName $StorageAccountName).Primary;
$storageAccountContext = New-AzureStorageContext $StorageAccountName $storageAccountKey;
$dropLocation = $storageAccountContext.BlobEndPoint + $StorageContainerName;
$dropLocationSasToken = New-AzureStorageContainerSASToken -Container $StorageContainerName -Context $storageAccountContext -Permission r;
$dropLocationSasToken = ConvertTo-SecureString $dropLocationSasToken -AsPlainText -Force;
Switch-AzureMode AzureResourceManager;
$ResourceGroupLocation = (Get-AzureResourceGroup -Name $ResourceGroupName).Location;
New-AzureResourceGroup -Name $ResourceGroupName `
-Location $ResourceGroupLocation `
-TemplateFile $TemplateFile `
-webSiteName $webSiteName `
-dropLocation $dropLocation `
-dropLocationSasToken $dropLocationSasToken `
-webSitePackage $webSitePackage `
-Force `
-Verbose;
# Switch back to original mode before exiting
if ($wasServiceManagementMode) {
Switch-AzureMode AzureServiceManagement
}
Param(
[string][Parameter(Mandatory=$true)] $WebSiteName,
[string][ValidateScript({Test-Path $_ -PathType 'Leaf'})] [Parameter(Mandatory=$true)] $ProjectFile,
[string] $ResourceGroupName = $WebSiteName,
[string] $StorageAccountName = $ResourceGroupName.ToLowerInvariant() + "storage",
[string] $StorageContainerName = ("{0}-{1}" -f $WebSiteName.ToLowerInvariant(), (Get-Date -format "hh-mm-ss-dd-mm-yyyy")),
[string] $LocalStorageDropPath = '.\StorageDrop',
[string] $AzCopyPath = '.\Tools\AzCopy.exe',
[string] $webSitePackage = "CustomerManager.zip",
[string] $TemplateFile = '.\Templates\PublishWebApp.json'
)
$ErrorActionPreference = "Stop";
$wasServiceManagementMode = Get-Module -Name Azure -ListAvailable;
$AzCopyPath = [System.IO.Path]::Combine($PSScriptRoot, $AzCopyPath);
$TemplateFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateFile);
$LocalStorageDropPath = [System.IO.Path]::Combine($PSScriptRoot, $LocalStorageDropPath);
#Local Drop Cleanup
Remove-Item ($LocalStorageDropPath + '\*');
#Publish to Local Drop
$publishXmlFile = ".\WebDeployPackage.pubxml";
& "$env:windir\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" $ProjectFile `
/p:VisualStudioVersion=12.0 `
/p:DeployOnBuild=true `
/p:DesktopBuildPackageLocation=$LocalStorageDropPath `
/p:PublishProfile=WebDeployPackage.pubxml;
Switch-AzureMode -Name AzureServiceManagement;
#Copy application package to the storage
$storageAccountKey = (Get-AzureStorageKey -StorageAccountName $StorageAccountName).Primary;
$storageAccountContext = New-AzureStorageContext $StorageAccountName $storageAccountKey;
$dropLocation = $storageAccountContext.BlobEndPoint + $StorageContainerName;
& "$AzCopyPath" """$LocalStorageDropPath"" $dropLocation /DestKey:$storageAccountKey /S /Y";
#Set drop location for msdeploy
$dropLocationSasToken = New-AzureStorageContainerSASToken -Container $StorageContainerName -Context $storageAccountContext -Permission r;
$dropLocationSasToken = ConvertTo-SecureString $dropLocationSasToken -AsPlainText -Force;
Switch-AzureMode AzureResourceManager;
$ResourceGroupLocation = (Get-AzureResourceGroup -Name $ResourceGroupName).Location;
New-AzureResourceGroup -Name $ResourceGroupName `
-Location $ResourceGroupLocation `
-TemplateFile $TemplateFile `
-webSiteName $webSiteName `
-dropLocation $dropLocation `
-dropLocationSasToken $dropLocationSasToken `
-webSitePackage $webSitePackage `
-Force `
-Verbose;
# Switch back to original mode before exiting
if ($wasServiceManagementMode) {
Switch-AzureMode AzureServiceManagement
}
{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"dropLocation": {
"type": "string",
"metadata": {
"visualStudio.deployment": { "parameterUsedAs": { "value": "dropLocation" } }
}
},
"dropLocationSasToken": {
"type": "securestring",
"metadata": {
"visualStudio.deployment": { "parameterUsedAs": { "value": "sasToken", "refersTo": "dropLocation" } }
}
},
"webSitePackage": {
"type": "string",
"metadata": {
"visualStudio.deployment": { "parameterUsedAs": { "value": "projectOutput" } }
}
},
"webSiteName": {
"type": "string"
}
},
"resources": [
{
"apiVersion": "2014-04-01-preview",
"name": "[concat(parameters('webSiteName'), '/', 'MSDeploy')]",
"type": "Microsoft.Web/sites/extensions",
"properties": {
"packageUri": "[concat(parameters('dropLocation'), '/', parameters('webSitePackage'), parameters('dropLocationSasToken'))]",
"dbType": "None",
"connectionString": "",
"setParameters": {
"IIS Web Application Name": "[parameters('webSiteName')]"
}
}
}
]
}
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>Package</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<DesktopBuildPackageLocation>E:\Developer\azurebootcamp2015kharkiv\CustomerManagerStandard\Automation\StorageDrop\CustomerManager.zip</DesktopBuildPackageLocation>
<PackageAsSingleFile>true</PackageAsSingleFile>
<DeployIisAppPath>CustomerManager</DeployIisAppPath>
<PublishDatabaseSettings>
<Objects xmlns="">
<ObjectGroup Name="CustomerManagerContext" Order="1" Enabled="False">
<Destination Path="" />
<Object Type="DbCodeFirst">
<Source Path="DBContext" DbContext="CustomerManager.Repository.CustomerManagerContext, CustomerManager" Origin="Configuration" />
</Object>
</ObjectGroup>
</Objects>
</PublishDatabaseSettings>
</PropertyGroup>
<ItemGroup>
<MSDeployParameterValue Include="$(DeployParameterPrefix)CustomerManagerContext-Web.config Connection String" />
</ItemGroup>
</Project>
{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"webSiteName": {
"type": "string"
},
"webSiteHostingPlanName": {
"type": "string"
},
"webSiteLocation": {
"type": "string"
},
"webSiteHostingPlanSKU": {
"type": "string",
"allowedValues": [
"Free",
"Shared",
"Basic",
"Standard"
],
"defaultValue": "Free"
},
"webSiteHostingPlanWorkerSize": {
"type": "string",
"allowedValues": [
"0",
"1",
"2"
],
"defaultValue": "0"
},
"sqlServerName": {
"type": "string"
},
"sqlServerLocation": {
"type": "string"
},
"sqlServerAdminLogin": {
"type": "string"
},
"sqlServerAdminPassword": {
"type": "securestring"
},
"sqlDbName": {
"type": "string"
},
"sqlDbCollation": {
"type": "string",
"defaultValue": "SQL_Latin1_General_CP1_CI_AS"
},
"sqlDbEdition": {
"type": "string",
"defaultValue": "Web"
},
"sqlDbMaxSizeBytes": {
"type": "string",
"defaultValue": "1073741824"
},
"sqlDbServiceObjectiveId": {
"type": "string",
"defaultValue": "910b4fcb-8a29-4c3e-958f-f7ba794388b2"
},
"storageAccountName": {
"type": "string"
},
"storageAccountType" :{
"type" : "string",
"defaultValue" : "Standard_LRS"
}
},
"resources": [
{
"name": "[parameters('storageAccountName')]",
"type": "Microsoft.ClassicStorage/storageAccounts",
"apiVersion": "2014-06-01",
"location": "[parameters('webSiteLocation')]",
"properties": {
"AccountType": "[parameters('storageAccountType')]"
}
},
{
"apiVersion": "2014-04-01-preview",
"name": "[parameters('sqlServerName')]",
"type": "Microsoft.Sql/servers",
"location": "[parameters('sqlServerLocation')]",
"tags": {
"displayName": "SQLServer"
},
"properties": {
"administratorLogin": "[parameters('sqlServerAdminLogin')]",
"administratorLoginPassword": "[parameters('sqlServerAdminPassword')]"
},
"resources": [
{
"apiVersion": "2014-04-01-preview",
"name": "[parameters('sqlDbName')]",
"type": "databases",
"location": "[parameters('sqlServerLocation')]",
"tags": {
"displayName": "SQLDatabase"
},
"dependsOn": [
"[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]"
],
"properties": {
"edition": "[parameters('sqlDbEdition')]",
"collation": "[parameters('sqlDbCollation')]",
"maxSizeBytes": "[parameters('sqlDbMaxSizeBytes')]",
"requestedServiceObjectiveId": "[parameters('sqlDbServiceObjectiveId')]"
}
},
{
"apiVersion": "2014-04-01-preview",
"name": "SQLServerFirewallRules",
"type": "firewallrules",
"location": "[parameters('sqlServerLocation')]",
"dependsOn": [
"[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]"
],
"properties": {
"endIpAddress": "0.0.0.0",
"startIpAddress": "0.0.0.0"
}
}
]
},
{
"apiVersion": "2014-04-01-preview",
"name": "[parameters('webSiteName')]",
"type": "Microsoft.Web/sites",
"location": "[parameters('webSiteLocation')]",
"tags": {
"[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('webSiteHostingPlanName'))]": "Resource",
"displayName": "WebSite"
},
"dependsOn": [
"[concat('Microsoft.Web/serverfarms/', parameters('webSiteHostingPlanName'))]"
],
"properties": {
"name": "[parameters('webSiteName')]",
"serverFarm": "[parameters('webSiteHostingPlanName')]"
},
"resources": [
{
"apiVersion": "2014-04-01-preview",
"type": "config",
"name": "web",
"dependsOn": [
"[concat('Microsoft.Web/Sites/', parameters('webSiteName'))]"
],
"properties": {
"connectionStrings": [
{
"ConnectionString": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', parameters('sqlServerName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('sqlDbName'), ';User Id=', parameters('sqlServerAdminLogin'), '@', parameters('sqlServerName'), ';Password=', parameters('sqlServerAdminPassword'), ';')]",
"Name": "CustomerManagerContext",
"Type": 2
}
]
}
}
]
},
{
"apiVersion": "2014-04-01-preview",
"name": "[parameters('webSiteHostingPlanName')]",
"type": "Microsoft.Web/serverfarms",
"location": "[parameters('webSiteLocation')]",
"tags": {
"displayName": "WebSiteHostingPlan"
},
"properties": {
"name": "[parameters('webSiteHostingPlanName')]",
"sku": "[parameters('webSiteHostingPlanSKU')]",
"workerSize": "[parameters('webSiteHostingPlanWorkerSize')]",
"numberOfWorkers": 1
}
},
{
"apiVersion": "2014-04-01",
"name": "[concat(parameters('webSiteHostingPlanName'), '-', resourceGroup().name)]",
"type": "microsoft.insights/autoscalesettings",
"location": "East US",
"tags": {
"displayName": "WebSiteHostingPlanAutoScale"
},
"dependsOn": [
"[concat('Microsoft.Web/serverfarms/', parameters('webSiteHostingPlanName'))]"
],
"properties": {
"name": "[concat(parameters('webSiteHostingPlanName'), '-', resourceGroup().name)]",
"profiles": [
{
"name": "Default",
"capacity": {
"minimum": "1",
"maximum": "2",
"default": "1"
},
"rules": [
{
"metricTrigger": {
"metricName": "CpuPercentage",
"metricResourceUri": "[concat(resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('webSiteHostingPlanName'))]",
"timeGrain": "PT1M",
"statistic": "Average",
"timeWindow": "PT5M",
"timeAggregation": "Average",
"operator": "GreaterThan",
"threshold": 60.0
},
"scaleAction": {
"direction": "Increase",
"type": "ChangeCount",
"value": "1",
"cooldown": "PT1M"
}
},
{
"metricTrigger": {
"metricName": "CpuPercentage",
"metricResourceUri": "[concat(resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('webSiteHostingPlanName'))]",
"timeGrain": "PT1M",
"statistic": "Average",
"timeWindow": "PT5M",
"timeAggregation": "Average",
"operator": "LessThan",
"threshold": 40.0
},
"scaleAction": {
"direction": "Decrease",
"type": "ChangeCount",
"value": "1",
"cooldown": "PT1M"
}
}
]
}
],
"enabled": true,
"targetResourceUri": "[concat(resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('webSiteHostingPlanName'))]"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment