From e96c20534c789d1b5a5fb11b4d5e12a75903ae70 Mon Sep 17 00:00:00 2001 From: Gunnar Peterson Date: Tue, 18 Oct 2022 14:32:43 -0500 Subject: [PATCH 1/4] Include service args in the docker file generator. --- src/Microsoft.Tye.Core/DockerfileGenerator.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.Tye.Core/DockerfileGenerator.cs b/src/Microsoft.Tye.Core/DockerfileGenerator.cs index 39941bfd8..5987eef08 100644 --- a/src/Microsoft.Tye.Core/DockerfileGenerator.cs +++ b/src/Microsoft.Tye.Core/DockerfileGenerator.cs @@ -4,7 +4,6 @@ using System; using System.IO; -using System.Linq; using System.Text; using System.Threading.Tasks; @@ -46,16 +45,16 @@ public static async Task WriteDockerfileAsync(OutputContext output, ApplicationB output.WriteDebugLine($"Writing Dockerfile to '{filePath}'."); if (container.UseMultiphaseDockerfile ?? true) { - await WriteMultiphaseDockerfileAsync(writer, entryPoint, container); + await WriteMultiphaseDockerfileAsync(writer, entryPoint, project, container); } else { - await WriteLocalPublishDockerfileAsync(writer, entryPoint, container); + await WriteLocalPublishDockerfileAsync(writer, entryPoint, project, container); } output.WriteDebugLine("Done writing Dockerfile."); } - private static async Task WriteMultiphaseDockerfileAsync(StreamWriter writer, string applicationEntryPoint, ContainerInfo container) + private static async Task WriteMultiphaseDockerfileAsync(StreamWriter writer, string applicationEntryPoint, DotnetProjectServiceBuilder project, ContainerInfo container) { await writer.WriteLineAsync($"FROM {container.BuildImageName}:{container.BuildImageTag} as SDK"); await writer.WriteLineAsync($"WORKDIR /src"); @@ -65,14 +64,18 @@ private static async Task WriteMultiphaseDockerfileAsync(StreamWriter writer, st await writer.WriteLineAsync($"WORKDIR /app"); await writer.WriteLineAsync($"COPY --from=SDK /out ."); await writer.WriteLineAsync($"ENTRYPOINT [\"dotnet\", \"{applicationEntryPoint}.dll\"]"); + if (!string.IsNullOrWhiteSpace(project.Args)) + await writer.WriteLineAsync($"CMD [\"{project.Args}\"]"); } - private static async Task WriteLocalPublishDockerfileAsync(StreamWriter writer, string applicationEntryPoint, ContainerInfo container) + private static async Task WriteLocalPublishDockerfileAsync(StreamWriter writer, string applicationEntryPoint, DotnetProjectServiceBuilder project, ContainerInfo container) { await writer.WriteLineAsync($"FROM {container.BaseImageName}:{container.BaseImageTag}"); await writer.WriteLineAsync($"WORKDIR /app"); await writer.WriteLineAsync($"COPY . /app"); await writer.WriteLineAsync($"ENTRYPOINT [\"dotnet\", \"{applicationEntryPoint}.dll\"]"); + if (!string.IsNullOrWhiteSpace(project.Args)) + await writer.WriteLineAsync($"CMD [\"{project.Args}\"]"); } public static void ApplyContainerDefaults(ApplicationBuilder application, DotnetProjectServiceBuilder project, ContainerInfo container) From b9cfbf57c8405ae8c558cab7e84b207f3592853d Mon Sep 17 00:00:00 2001 From: Gunnar Peterson Date: Tue, 20 Dec 2022 15:35:18 -0600 Subject: [PATCH 2/4] Add test to ensure that docker file is still getting built with arguments. --- test/E2ETest/TyeBuildTests.cs | 30 ++++++++++++++ .../single-project-with-args.sln | 34 ++++++++++++++++ .../test-project-with-args/Program.cs | 29 ++++++++++++++ .../Properties/launchSettings.json | 27 +++++++++++++ .../test-project-with-args/Startup.cs | 40 +++++++++++++++++++ .../appsettings.Development.json | 9 +++++ .../test-project-with-args/appsettings.json | 10 +++++ .../test-project-with-args.csproj | 8 ++++ .../single-project-with-args/tye.yaml | 7 ++++ 9 files changed, 194 insertions(+) create mode 100644 test/E2ETest/testassets/projects/single-project-with-args/single-project-with-args.sln create mode 100644 test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Program.cs create mode 100644 test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Properties/launchSettings.json create mode 100644 test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Startup.cs create mode 100644 test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/appsettings.Development.json create mode 100644 test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/appsettings.json create mode 100644 test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/test-project-with-args.csproj create mode 100644 test/E2ETest/testassets/projects/single-project-with-args/tye.yaml diff --git a/test/E2ETest/TyeBuildTests.cs b/test/E2ETest/TyeBuildTests.cs index c817fa03d..9aeeaedcd 100644 --- a/test/E2ETest/TyeBuildTests.cs +++ b/test/E2ETest/TyeBuildTests.cs @@ -147,5 +147,35 @@ public async Task BuildDoesNotRequireRegistry() await DockerAssert.DeleteDockerImagesAsync(output, "test-project"); } } + + [ConditionalFact] + [SkipIfDockerNotRunning] + public async Task ProjectWithArgsBuildTest() + { + await DockerAssert.DeleteDockerImagesAsync(output, "test/test-project-with-args"); + + var projectName = "single-project-with-args"; + var environment = "production"; + + using var projectDirectory = CopyTestProjectDirectory(projectName); + + var projectFile = new FileInfo(Path.Combine(projectDirectory.DirectoryPath, "tye.yaml")); + + var outputContext = new OutputContext(sink, Verbosity.Debug); + var application = await ApplicationFactory.CreateAsync(outputContext, projectFile); + + application.Registry = new ContainerRegistry("test"); + + try + { + await BuildHost.ExecuteBuildAsync(outputContext, application, environment, interactive: false); + + await DockerAssert.AssertImageExistsAsync(output, "test/test-project-with-args"); + } + finally + { + await DockerAssert.DeleteDockerImagesAsync(output, "test/test-project-with-args"); + } + } } } diff --git a/test/E2ETest/testassets/projects/single-project-with-args/single-project-with-args.sln b/test/E2ETest/testassets/projects/single-project-with-args/single-project-with-args.sln new file mode 100644 index 000000000..e7900f22d --- /dev/null +++ b/test/E2ETest/testassets/projects/single-project-with-args/single-project-with-args.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test-project-with-args", "test-project-with-args\test-project-with-args.csproj", "{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|x64.ActiveCfg = Debug|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|x64.Build.0 = Debug|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|x86.ActiveCfg = Debug|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|x86.Build.0 = Debug|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|Any CPU.Build.0 = Release|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|x64.ActiveCfg = Release|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|x64.Build.0 = Release|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|x86.ActiveCfg = Release|Any CPU + {7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Program.cs b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Program.cs new file mode 100644 index 000000000..70811e810 --- /dev/null +++ b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Program.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; + +namespace test_project_with_args +{ + public class Program + { + public static void Main(string[] args) + { + var argCount = 0; + foreach (var arg in args) + Console.WriteLine($"Argument {++argCount}: {arg}"); + + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Properties/launchSettings.json b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Properties/launchSettings.json new file mode 100644 index 000000000..a759bf561 --- /dev/null +++ b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:18482", + "sslPort": 44344 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "test_project_with_args": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Startup.cs b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Startup.cs new file mode 100644 index 000000000..a8486bb61 --- /dev/null +++ b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/Startup.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace test_project_with_args +{ + public class Startup + { + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + 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, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapGet("/", async context => + { + await context.Response.WriteAsync("Hello World!"); + }); + }); + } + } +} diff --git a/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/appsettings.Development.json b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/appsettings.Development.json new file mode 100644 index 000000000..8983e0fc1 --- /dev/null +++ b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/appsettings.json b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/appsettings.json new file mode 100644 index 000000000..d9d9a9bff --- /dev/null +++ b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/test-project-with-args.csproj b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/test-project-with-args.csproj new file mode 100644 index 000000000..15fc04da1 --- /dev/null +++ b/test/E2ETest/testassets/projects/single-project-with-args/test-project-with-args/test-project-with-args.csproj @@ -0,0 +1,8 @@ + + + + net6.0 + test_project_with_args + + + diff --git a/test/E2ETest/testassets/projects/single-project-with-args/tye.yaml b/test/E2ETest/testassets/projects/single-project-with-args/tye.yaml new file mode 100644 index 000000000..0b4c8ca2f --- /dev/null +++ b/test/E2ETest/testassets/projects/single-project-with-args/tye.yaml @@ -0,0 +1,7 @@ +# tye application configuration file +# read all about it at https://github.com/dotnet/tye +name: single-project-with-args +services: +- name: test-project-with-args + project: test-project-with-args/test-project-with-args.csproj + args: Argument1 Argument2 From 0de7217e9681bdbd2e6eb5c4e2aa55fe63a04cec Mon Sep 17 00:00:00 2001 From: Gunnar Peterson Date: Tue, 20 Dec 2022 16:19:21 -0600 Subject: [PATCH 3/4] Get branch up to date and fix error. --- test/E2ETest/TyeBuildTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/E2ETest/TyeBuildTests.cs b/test/E2ETest/TyeBuildTests.cs index 9aeeaedcd..04900f5be 100644 --- a/test/E2ETest/TyeBuildTests.cs +++ b/test/E2ETest/TyeBuildTests.cs @@ -103,7 +103,7 @@ public async Task MultipleProjectBuildTest() var outputContext = new OutputContext(sink, Verbosity.Debug); var application = await ApplicationFactory.CreateAsync(outputContext, projectFile); - application.Registry = new ContainerRegistry("test"); + application.Registry = new ContainerRegistry("test", null); try { From 5d728eae52347cb63a451884919de50ec3cbed12 Mon Sep 17 00:00:00 2001 From: Gunnar Peterson Date: Tue, 20 Dec 2022 16:30:36 -0600 Subject: [PATCH 4/4] Update branch to latest from fork, fix the build test. --- test/E2ETest/TyeBuildTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/E2ETest/TyeBuildTests.cs b/test/E2ETest/TyeBuildTests.cs index 4b184506c..55e520b6a 100644 --- a/test/E2ETest/TyeBuildTests.cs +++ b/test/E2ETest/TyeBuildTests.cs @@ -164,7 +164,7 @@ public async Task ProjectWithArgsBuildTest() var outputContext = new OutputContext(sink, Verbosity.Debug); var application = await ApplicationFactory.CreateAsync(outputContext, projectFile); - application.Registry = new ContainerRegistry("test"); + application.Registry = new ContainerRegistry("test", null); try {