Skip to content

Commit

Permalink
Added support for building ASP.NET Core app using Debug configuration (
Browse files Browse the repository at this point in the history
  • Loading branch information
kichalla committed May 21, 2019
1 parent 6ee490a commit 15ac482
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 16 deletions.
21 changes: 11 additions & 10 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ can apply minor adjustments and still utilize the automatic build process. The f

> NOTE: In Azure Web Apps, these variables are set as App Settings [App Settings][].
Setting name | Description | Default | Example
---------------------------------|----------------------------------------------------------------|---------|----------------
PRE\_BUILD\_SCRIPT\_PATH | repo-relative path to a shell script to be run before build | "" | scripts/prebuild.sh
POST\_BUILD\_SCRIPT\_PATH | repo-relative path to a shell script to be run after build | "" | scripts/postbuild.sh
DISABLE\_COLLECTSTATIC | Disable running `collecstatic` when building Django apps. | `false` | `true`, `false`
PROJECT | repo-relative path to directory with `.csproj` file for build | "" | src/WebApp1/WebApp1.csproj
ENABLE\_MULTIPLATFORM\_BUILD | apply more than one toolset if repo indicates it | `false` | `true`, `false`
DISABLE\_DOTNETCORE\_BUILD | do not apply .NET Core build even if repo indicates it | `false` | `true`, `false`
DISABLE\_PYTHON\_BUILD | do not apply Python build even if repo indicates it | `false` | `true`, `false`
DISABLE\_NODEJS\_BUILD | do not apply Node.js build even if repo indicates it | `false` | `true`, `false`
Setting name | Description | Default | Example
-----------------------------|----------------------------------------------------------------|---------|----------------
PRE\_BUILD\_COMMAND | Command or a repo-relative path to a shell script to be run before build | "" | `echo foo`, `scripts/prebuild.sh`
POST\_BUILD\_COMMAND | Command or a repo-relative path to a shell script to be run after build | "" | `echo foo`, `scripts/postbuild.sh`
DISABLE\_COLLECTSTATIC | Disable running `collecstatic` when building Django apps. | `false` | `true`, `false`
PROJECT | repo-relative path to directory with `.csproj` file for build | "" | src/WebApp1/WebApp1.csproj
ENABLE\_MULTIPLATFORM\_BUILD | apply more than one toolset if repo indicates it | `false` | `true`, `false`
DISABLE\_DOTNETCORE\_BUILD | do not apply .NET Core build even if repo indicates it | `false` | `true`, `false`
DISABLE\_PYTHON\_BUILD | do not apply Python build even if repo indicates it | `false` | `true`, `false`
DISABLE\_NODEJS\_BUILD | do not apply Node.js build even if repo indicates it | `false` | `true`, `false`
MSBUILD\_CONFIGURATION | Configuration (Debug or Relase) that is used to build a .NET Core project | `Release` | `Debug`, `Release`

# Azure App Service configuration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function publishToDirectory()
echo
echo "Publishing to directory '$directoryToPublishTo'..."
echo
dotnet publish "{{ ProjectFile }}" -c Release -o "$directoryToPublishTo"
dotnet publish "{{ ProjectFile }}" -c {{ Configuration }} -o "$directoryToPublishTo"
}

TOTAL_EXECUTION_START_TIME=$SECONDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ public class DotNetCoreBashBuildSnippetProperties
public string ManifestFileName { get; set; }

public Dictionary<string, string> BuildProperties { get; set; }

public string Configuration { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/BuildScriptGenerator/DotNetCore/DotnetCoreConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ public static class DotnetCoreConstants
public const string ProjectSdkAttributeValueXPathExpression = "string(/Project/@Sdk)";
public const string ProjectSdkElementNameAttributeValueXPathExpression = "string(/Project/Sdk/@Name)";
public const string TargetFrameworkElementXPathExpression = "/Project/PropertyGroup/TargetFramework";

public const string DefaultMSBuildConfiguration = "Release";
}
}
18 changes: 17 additions & 1 deletion src/BuildScriptGenerator/DotNetCore/DotnetCorePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.IO;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.Oryx.BuildScriptGenerator.DotNetCore
{
Expand All @@ -21,19 +22,22 @@ internal class DotnetCorePlatform : IProgrammingPlatform
private readonly IEnvironmentSettingsProvider _environmentSettingsProvider;
private readonly ILogger<DotnetCorePlatform> _logger;
private readonly DotnetCoreLanguageDetector _detector;
private readonly DotnetCoreScriptGeneratorOptions _options;

public DotnetCorePlatform(
IDotnetCoreVersionProvider versionProvider,
IAspNetCoreWebAppProjectFileProvider aspNetCoreWebAppProjectFileProvider,
IEnvironmentSettingsProvider environmentSettingsProvider,
ILogger<DotnetCorePlatform> logger,
DotnetCoreLanguageDetector detector)
DotnetCoreLanguageDetector detector,
IOptions<DotnetCoreScriptGeneratorOptions> options)
{
_versionProvider = versionProvider;
_aspNetCoreWebAppProjectFileProvider = aspNetCoreWebAppProjectFileProvider;
_environmentSettingsProvider = environmentSettingsProvider;
_logger = logger;
_detector = detector;
_options = options.Value;
}

public string Name => DotnetCoreConstants.LanguageName;
Expand Down Expand Up @@ -75,6 +79,7 @@ public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorCon
PostBuildCommand = postBuildCommand,
ManifestFileName = Constants.ManifestFileName,
ZipAllOutput = zipAllOutput,
Configuration = GetBuildConfiguration()
};
var script = TemplateHelpers.Render(
TemplateHelpers.TemplateResource.DotNetCoreSnippet,
Expand Down Expand Up @@ -143,6 +148,17 @@ public IEnumerable<string> GetDirectoriesToExcludeFromCopyToIntermediateDir(
return dirs;
}

private string GetBuildConfiguration()
{
var configuration = _options.MSBuildConfiguration;
if (string.IsNullOrEmpty(configuration))
{
configuration = DotnetCoreConstants.DefaultMSBuildConfiguration;
}

return configuration;
}

private static bool ShouldZipAllOutput(BuildScriptGeneratorContext context)
{
return BuildPropertiesHelper.IsTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class DotnetCoreScriptGeneratorOptions
public IList<string> SupportedVersions { get; set; }

public string Project { get; set; }

public string MSBuildConfiguration { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Licensed under the MIT license.
// --------------------------------------------------------------------------------------------

using System.IO;
using Microsoft.Extensions.Options;

namespace Microsoft.Oryx.BuildScriptGenerator.DotNetCore
Expand Down Expand Up @@ -33,6 +32,8 @@ public void Configure(DotnetCoreScriptGeneratorOptions options)
options.SupportedVersions = _environment.GetEnvironmentVariableAsList(
EnvironmentSettingsKeys.DotnetCoreSupportedVersions);
options.Project = _environment.GetEnvironmentVariable(EnvironmentSettingsKeys.Project);
options.MSBuildConfiguration = _environment.GetEnvironmentVariable(
EnvironmentSettingsKeys.MSBuildConfiguration);
}
}
}
5 changes: 5 additions & 0 deletions src/BuildScriptGenerator/EnvironmentSettingsKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ public static class EnvironmentSettingsKeys
public const string Project = "PROJECT";

public const string DisableCollectStatic = "DISABLE_COLLECTSTATIC";

/// <summary>
/// Represents the 'Configuration' switch of a build, for example: dotnet build --configuration Release
/// </summary>
public const string MSBuildConfiguration = "MSBUILD_CONFIGURATION";
}
}
45 changes: 45 additions & 0 deletions tests/Oryx.Integration.Tests/DotNetCoreEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Oryx.BuildScriptGenerator;
using Microsoft.Oryx.BuildScriptGenerator.DotNetCore;
using Microsoft.Oryx.Tests.Common;
using Xunit;
Expand Down Expand Up @@ -440,6 +441,50 @@ await EndToEndTestHelper.BuildRunAndAssertAppAsync(
});
}

[Fact]
public async Task CanBuildAndRun_NetCore22WebApp_UsingDebugConfiguration()
{
// Arrange
var dotnetcoreVersion = "2.2";
var hostDir = Path.Combine(_hostSamplesDir, "DotNetCore", NetCoreApp22WebApp);
var volume = DockerVolume.Create(hostDir);
var appDir = volume.ContainerDir;
var appOutputDir = $"{appDir}/myoutputdir";
var buildImageScript = new ShellScriptBuilder()
.SetEnvironmentVariable(EnvironmentSettingsKeys.MSBuildConfiguration, "Debug")
.AddCommand($"oryx build {appDir} -l dotnet --language-version {dotnetcoreVersion} -o {appOutputDir}")
.ToString();
var runtimeImageScript = new ShellScriptBuilder()
.AddCommand(
$"oryx -appPath {appOutputDir} -bindPort {ContainerPort}")
.AddCommand(DefaultStartupFilePath)
.ToString();

await EndToEndTestHelper.BuildRunAndAssertAppAsync(
NetCoreApp22WebApp,
_output,
volume,
"/bin/sh",
new[]
{
"-c",
buildImageScript
},
$"oryxdevms/dotnetcore-{dotnetcoreVersion}",
ContainerPort,
"/bin/sh",
new[]
{
"-c",
runtimeImageScript
},
async (hostPort) =>
{
var data = await _httpClient.GetStringAsync($"http://localhost:{hostPort}/");
Assert.Contains("Hello World! from Debug build.", data);
});
}

[Fact]
public async Task CanBuildAndRun_NetCore22WebApp_WithStartupFileName_HavingWhitespace()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Sdk Name="Microsoft.NET.Sdk.Web" Version="1.0.0" />
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
Expand Down
9 changes: 8 additions & 1 deletion tests/SampleApps/DotNetCore/NetCoreApp22WebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)

app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
var responseContent = "Hello World!";

// For verifying scenarios where we want to make sure this app is being using Debug configuration.
#if DEBUG
responseContent += " from Debug build.";
#endif

await context.Response.WriteAsync(responseContent);
});
}
}
Expand Down

0 comments on commit 15ac482

Please sign in to comment.