Skip to content

Commit

Permalink
Azure Deployment Logic (#251)
Browse files Browse the repository at this point in the history
* Adds logic required to deploy to Azure using Continuous Deployment or manual release pipeline
  • Loading branch information
asyasky authored Mar 12, 2019
1 parent 5fa93d9 commit c71ae40
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 35 deletions.
35 changes: 35 additions & 0 deletions ServerCore/Areas/Deployment/DeploymentConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServerCore.DataModel;

namespace ServerCore.Areas.Deployment
{
public class DeploymentConfiguration
{
internal static void ConfigureDatabase(IConfiguration configuration, IServiceCollection services, IHostingEnvironment env)
{
// Use SQL Database if in Azure, otherwise, use localdb
if (env.IsStaging() && Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "PuzzleServerTestDeploy")
{
services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(configuration.GetConnectionString("PuzzleServerSQLConnectionString")));
}
else if (env.IsProduction() && (Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "puzzlehunt" || Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "puzzleday"))
{
services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(configuration.GetConnectionString("PuzzleServerSQLConnectionString")));
}
else
{
services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(configuration.GetConnectionString("PuzzleServerContextLocal")));
}
}
}
}
16 changes: 11 additions & 5 deletions ServerCore/Areas/Identity/IdentityHostingStartup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServerCore.Areas.Deployment;
using ServerCore.DataModel;

[assembly: HostingStartup(typeof(ServerCore.Areas.Identity.IdentityHostingStartup))]
Expand All @@ -14,10 +14,16 @@ public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) =>
{
services.AddDbContext<PuzzleServerContext>(options =>
options.UseLazyLoadingProxies()
.UseSqlServer(
context.Configuration.GetConnectionString("PuzzleServerContext")));
// Set up to use Azure settings
IHostingEnvironment env = context.HostingEnvironment;
IConfigurationBuilder configBuilder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
context.Configuration = configBuilder.Build();
DeploymentConfiguration.ConfigureDatabase(context.Configuration, services, env);
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<PuzzleServerContext>();
Expand Down
13 changes: 7 additions & 6 deletions ServerCore/FileManager.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace ServerCore
{
public class FileManager
{
public static string ConnectionString { get; set; }

/// <summary>
/// Uploads a file to blob storage
/// </summary>
Expand Down Expand Up @@ -57,9 +59,8 @@ private static async Task<CloudBlobContainer> GetOrCreateEventContainerAsync(int
private static CloudStorageAccount StorageAccount
{
get
{
// todo: read the account info from configuration
return CloudStorageAccount.Parse("UseDevelopmentStorage=true");
{
return CloudStorageAccount.Parse(ConnectionString);
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions ServerCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;

namespace ServerCore
{
Expand All @@ -11,8 +14,8 @@ public static void Main(string[] args)
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<ResourceId>/subscriptions/3ffc52f4-eed1-4312-8170-9caf82ff0949/resourcegroups/PuzzleServerTestDeploy/providers/Microsoft.Web/sites/PuzzleServerTestDeploy</ResourceId>
<ResourceGroup>PuzzleServerTestDeploy</ResourceGroup>
<PublishProvider>AzureWebSite</PublishProvider>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish>https://puzzleservertestdeploy.azurewebsites.net</SiteUrlToLaunchAfterPublish>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<ProjectGuid>67b91cce-4e3d-4f0f-8a76-eafd37c5f949</ProjectGuid>
<MSDeployServiceURL>puzzleservertestdeploy.scm.azurewebsites.net:443</MSDeployServiceURL>
<DeployIisAppPath>PuzzleServerTestDeploy</DeployIisAppPath>
<RemoteSitePhysicalPath />
<SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
<MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
<EnableMSDeployBackup>True</EnableMSDeployBackup>
<UserName>$PuzzleServerTestDeploy</UserName>
<_SavePWD>True</_SavePWD>
<_DestinationType>AzureWebSite</_DestinationType>
<InstallAspNetCoreSiteExtension>False</InstallAspNetCoreSiteExtension>
<TargetFramework>netcoreapp2.1</TargetFramework>
<SelfContained>false</SelfContained>
<_IsPortable>true</_IsPortable>
</PropertyGroup>
</Project>
18 changes: 9 additions & 9 deletions ServerCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
Expand All @@ -8,13 +8,13 @@
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},\
"ServerCore": {
"commandName": "Project",
"launchBrowser": true,
Expand All @@ -24,4 +24,4 @@
"applicationUrl": "http://localhost:44319/"
}
}
}
}
1 change: 1 addition & 0 deletions ServerCore/ServerCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.AzureKeyVault.HostingStartup" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="2.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.2" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.1" />
Expand Down
41 changes: 34 additions & 7 deletions ServerCore/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
using Microsoft.AspNetCore.Authorization;
using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServerCore.Areas.Deployment;
using ServerCore.Areas.Identity.UserAuthorizationPolicy;
using ServerCore.DataModel;

namespace ServerCore
{
public class Startup
{
public Startup(IConfiguration configuration)
private IHostingEnvironment hostingEnvironment;

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
hostingEnvironment = env;
}

public Startup(IHostingEnvironment env)
{
// Set up to use Azure settings
IConfigurationBuilder configBuilder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = configBuilder.Build();

hostingEnvironment = env;
}

public IConfiguration Configuration { get; }
Expand All @@ -39,9 +56,8 @@ public void ConfigureServices(IServiceCollection services)
options.Conventions.AuthorizeFolder("/ModelBases");
});

services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString("PuzzleServerContext")));
DeploymentConfiguration.ConfigureDatabase(Configuration, services, hostingEnvironment);
FileManager.ConnectionString = Configuration.GetConnectionString("AzureStorageConnectionString");

services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
{
Expand Down Expand Up @@ -87,12 +103,23 @@ public void ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
if (env.IsDevelopment() && String.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")))
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
PuzzleServerContext.UpdateDatabase(app);
}
else if (env.IsStaging() && Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "PuzzleServerTestDeploy")
{
app.UseExceptionHandler("/Error");
app.UseHsts();
PuzzleServerContext.UpdateDatabase(app);
}
else if (env.IsProduction() && (Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "puzzlehunt" || Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "puzzleday"))
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
else
{
app.UseExceptionHandler("/Error");
Expand Down
12 changes: 12 additions & 0 deletions ServerCore/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:44319/",
"sslPort": 0
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"PuzzleServerContextLocal": "Server=(localdb)\\MSSQLLocalDB;Database=PuzzleServer;Trusted_Connection=True;MultipleActiveResultSets=true",
"AzureStorageConnectionString": "UseDevelopmentStorage=true"
}
}
15 changes: 15 additions & 0 deletions ServerCore/appsettings.Production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://puzzlehunt.azurewebsites.net",
"sslPort": 0
}
},
"ConnectionStrings": {
"PuzzleServerContext": "",
"AzureStorageConnectionString": "",
"PuzzleServerSQLConnectionString": ""
}
}
15 changes: 15 additions & 0 deletions ServerCore/appsettings.Staging.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://puzzleservertestdeploy.azurewebsites.net",
"sslPort": 0
}
},
"ConnectionStrings": {
"PuzzleServerContext": "",
"AzureStorageConnectionString": "",
"PuzzleServerSQLConnectionString": ""
}
}
4 changes: 0 additions & 4 deletions ServerCore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"PuzzleServerContext": "Server=(localdb)\\MSSQLLocalDB;Database=PuzzleServer;Trusted_Connection=True;MultipleActiveResultSets=true",
"AzureStorageConnectionString-1": "UseDevelopmentStorage=true"
}
}
1 change: 0 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ steps:
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True

- task: PublishBuildArtifacts@1

0 comments on commit c71ae40

Please sign in to comment.