Skip to content

Commit

Permalink
Azure deployment glue (PuzzleServer#118)
Browse files Browse the repository at this point in the history
* Adds logic required to deploy to Azure using Continuous Deployment.
* Yaml updates
  • Loading branch information
asyasky authored and Megan Quinn committed Feb 20, 2019
1 parent 60d94f9 commit d0726db
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 20 deletions.
29 changes: 29 additions & 0 deletions ServerCore/Areas/Deployment/DeploymentConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
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)
{
// Use SQL Database if in Azure, otherwise, use localdb
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
{

services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(configuration.GetConnectionString("PuzzleServerSQLConnectionString")));
}
else
{
services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(configuration.GetConnectionString("PuzzleServerContextLocal")));
}
}
}
}
21 changes: 16 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,21 @@ 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);
//services.AddDbContext<PuzzleServerContext>(options =>
// options.UseLazyLoadingProxies()
// .UseSqlServer(
// context.Configuration.GetConnectionString("PuzzleServerContext")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<PuzzleServerContext>();
Expand Down
25 changes: 22 additions & 3 deletions ServerCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;

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

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((ctx, builder) =>
{
var keyVaultEndpoint = GetKeyVaultEndpoint();
if (!string.IsNullOrEmpty(keyVaultEndpoint))
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(
azureServiceTokenProvider.KeyVaultTokenCallback));
builder.AddAzureKeyVault(
keyVaultEndpoint, keyVaultClient, new DefaultKeyVaultSecretManager());
}
}
).UseStartup<Startup>()
.Build();

private static string GetKeyVaultEndpoint() => "https://PuzzleServerTestKeyVault.vault.azure.net";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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>
<KeyVaultUri>https://PuzzleServerTestKeyVault.vault.azure.net</KeyVaultUri>
<TargetFramework>netcoreapp2.1</TargetFramework>
<SelfContained>false</SelfContained>
<_IsPortable>true</_IsPortable>
</PropertyGroup>
</Project>
21 changes: 17 additions & 4 deletions ServerCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServerCore.Areas.Identity.UserAuthorizationPolicy;
using ServerCore.Areas.Deployment;
using ServerCore.DataModel;

namespace ServerCore
Expand All @@ -19,6 +20,17 @@ public Startup(IConfiguration configuration)
Configuration = configuration;
}

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();
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
Expand All @@ -40,9 +52,11 @@ public void ConfigureServices(IServiceCollection services)
options.Conventions.AuthorizeFolder("/ModelBases");
});

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

//services.AddDbContext<PuzzleServerContext>
// (options => options.UseLazyLoadingProxies()
// .UseSqlServer(Configuration.GetConnectionString("PuzzleServerContext")));

services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
{
Expand Down Expand Up @@ -99,7 +113,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
// Use KeyVault to get secrets
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
PuzzleServerContext.UpdateDatabase(app);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion ServerCore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
},
"ConnectionStrings": {
"PuzzleServerContext": "Server=(localdb)\\MSSQLLocalDB;Database=PuzzleServer;Trusted_Connection=True;MultipleActiveResultSets=true",
"PuzzleServerContextLocal": "Server=(localdb)\\MSSQLLocalDB;Database=PuzzleServer;Trusted_Connection=True;MultipleActiveResultSets=true",
"AzureStorageConnectionString-1": "UseDevelopmentStorage=true"
}
}
8 changes: 1 addition & 7 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ variables:

steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
- task: DotNetCoreCLI@2
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
dotnet publish --configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)

- task: PublishBuildArtifacts@1

0 comments on commit d0726db

Please sign in to comment.