Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Include service args in the docker file generator #1463

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Microsoft.Tye.Core/DockerfileGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -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");
Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions test/E2ETest/TyeBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,35 @@ public async Task BuildDoesNotRequireRegistry()
await DockerAssert.DeleteDockerImagesAsync(output, "test-project");
}
}

[ConditionalFact]
[SkipIfDockerNotRunning]
public async Task ProjectWithArgsBuildTest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this test asset the arguments are being passed and working?

{
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", null);

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");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<Startup>();
});
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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!");
});
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>test_project_with_args</RootNamespace>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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