Skip to content

Commit

Permalink
Initializing Dockerfile for .NET isolated project does not honor exis…
Browse files Browse the repository at this point in the history
…ting TFM (#3800)

* Initializing Dockerfile for .NET isolated project does not honor existing TFM (#3771)

-> Add Check for dotnet worker runtime and get the targetframework from the FunctionApp root directory.

* Review changes : #3771

---------

Co-authored-by: v-vreyya <v-vreyya@DESKTOP-RT7L5HG>
  • Loading branch information
VineethReyya and v-vreyya committed Sep 12, 2024
1 parent 96adc24 commit 2c5b814
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Azure.Functions.Cli/Actions/LocalActions/InitAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ private static async Task WriteLocalSettingsJson(WorkerRuntime workerRuntime, Pr

private static async Task WriteDockerfile(WorkerRuntime workerRuntime, string language, string targetFramework, bool csx)
{
if (WorkerRuntimeLanguageHelper.IsDotnet(workerRuntime) && string.IsNullOrEmpty(targetFramework) && !csx)
{
var functionAppRoot = ScriptHostHelpers.GetFunctionAppRootDirectory(Environment.CurrentDirectory);
if (functionAppRoot != null)
{
targetFramework = await DotnetHelpers.DetermineTargetFramework(functionAppRoot);
}
}

if (workerRuntime == Helpers.WorkerRuntime.dotnet)
{
if (csx)
Expand Down
13 changes: 11 additions & 2 deletions src/Azure.Functions.Cli/Helpers/DotnetHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ public static void EnsureDotnet()
/// e.g. in Directory.Build.props
/// </summary>
/// <param name="projectDirectory">Directory containing the .csproj file</param>
/// <param name="projectFilename">Name of the .csproj file</param>
/// <returns>Target framework, e.g. net8.0</returns>
/// <exception cref="CliException"></exception>
public static async Task<string> DetermineTargetFramework(string projectDirectory)
public static async Task<string> DetermineTargetFramework(string projectDirectory, string projectFilename = null)
{
EnsureDotnet();
if (projectFilename == null)
{
var projectFilePath = ProjectHelpers.FindProjectFile(projectDirectory);
if (projectFilePath != null)
{
projectFilename = Path.GetFileName(projectFilePath);
}
}
var exe = new Executable(
"dotnet",
"build -getproperty:TargetFramework",
$"build {projectFilename} -getproperty:TargetFramework",
workingDirectory: projectDirectory,
environmentVariables: new Dictionary<string, string>
{
Expand Down
27 changes: 27 additions & 0 deletions test/Azure.Functions.Cli.Tests/E2E/InitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -895,5 +895,32 @@ public Task init_python_app_generates_getting_started_md()
},
}, _output);
}

[Theory]
[InlineData("dotnet-isolated","4", "net6.0")]
[InlineData("dotnet-isolated", "4", "net7.0")]
[InlineData("dotnet-isolated","4", "net8.0")]
public Task init_docker_only_for_existing_project_TargetFramework(string workerRuntime, string version, string TargetFramework)
{
var TargetFrameworkstr = TargetFramework.Replace("net", string.Empty);
return CliTester.Run(new RunConfiguration
{
Commands = new[]
{
$"init . --worker-runtime {workerRuntime} --target-framework {TargetFramework}",
$"init . --docker-only",
},
CheckFiles = new[]
{
new FileResult
{
Name = "Dockerfile",
ContentContains = new[] { $"FROM mcr.microsoft.com/azure-functions/{workerRuntime}:{version}-{workerRuntime}{TargetFrameworkstr}" }
}
},
OutputContains = new[] { "Dockerfile" },
CommandTimeout = TimeSpan.FromSeconds(300)
}, _output);
}
}
}

0 comments on commit 2c5b814

Please sign in to comment.