Skip to content

Commit 0f4dc7f

Browse files
committed
(build) build docker images with buildx
1 parent b52f242 commit 0f4dc7f

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

.github/workflows/ci.yml

+13-2
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ jobs:
265265
with:
266266
name: nuget
267267
path: ${{ github.workspace }}/artifacts/packages/nuget
268+
-
269+
name: '[Docker Build/Test] DockerHub'
270+
shell: pwsh
271+
run: dotnet run/docker.dll --target=DockerTest --docker_dotnetversion=${{ matrix.targetFramework }} --docker_distro=${{ matrix.distro }} --docker_registry dockerhub
268272
-
269273
name: Login to DockerHub
270274
if: success() && github.event_name != 'pull_request'
@@ -273,9 +277,15 @@ jobs:
273277
username: ${{ secrets.DOCKER_USERNAME }}
274278
password: ${{ secrets.DOCKER_PASSWORD }}
275279
-
276-
name: '[Docker Build/Test/Publish] DockerHub'
280+
name: '[Docker Publish] DockerHub'
281+
if: success() && github.event_name != 'pull_request'
277282
shell: pwsh
278283
run: dotnet run/docker.dll --target=DockerPublish --docker_dotnetversion=${{ matrix.targetFramework }} --docker_distro=${{ matrix.distro }} --docker_registry dockerhub
284+
285+
-
286+
name: '[Docker Build/Test] GitHub Container Registry'
287+
shell: pwsh
288+
run: dotnet run/docker.dll --target=DockerTest --docker_dotnetversion=${{ matrix.targetFramework }} --docker_distro=${{ matrix.distro }} --docker_registry github
279289
-
280290
name: Login to GitHub Container Registry
281291
if: success() && github.event_name != 'pull_request'
@@ -285,7 +295,8 @@ jobs:
285295
username: ${{ github.repository_owner }}
286296
password: ${{ secrets.GITHUB_TOKEN }}
287297
-
288-
name: '[Docker Build/Test/Publish] GitHub Docker'
298+
name: '[Docker Publish] GitHub Container Registry'
299+
if: success() && github.event_name != 'pull_request'
289300
shell: pwsh
290301
run: dotnet run/docker.dll --target=DockerPublish --docker_dotnetversion=${{ matrix.targetFramework }} --docker_distro=${{ matrix.distro }} --docker_registry github
291302

build/common/Utilities/BuildContextBase.cs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ protected BuildContextBase(ICakeContext context) : base(context)
1515
public bool IsPullRequest { get; set; }
1616
public bool IsTagged { get; set; }
1717
public bool IsLocalBuild { get; set; }
18-
public bool IsAppVeyorBuild { get; set; }
1918
public bool IsAzurePipelineBuild { get; set; }
2019
public bool IsGitHubActionsBuild { get; set; }
2120
public bool IsOnWindows { get; set; }

build/common/Utilities/DockerContextExtensions.cs

+39-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ namespace Common.Utilities
1212
{
1313
public static class DockerContextExtensions
1414
{
15-
public static void DockerBuild(this BuildContextBase context, DockerImage dockerImage)
15+
public static void DockerBuild(this BuildContextBase context, DockerImage dockerImage, bool pushImages)
1616
{
17+
if (context.Version == null) return;
18+
1719
var (distro, targetFramework, registry, _) = dockerImage;
1820
var workDir = Paths.Src.Combine("Docker");
1921
var tags = context.GetDockerTags(dockerImage);
2022

21-
if (context.Version == null) return;
23+
var platforms = new List<string> { "linux/amd64" };
24+
// if (targetFramework != "3.1" || !distro.StartsWith("alpine"))
25+
// {
26+
// platforms.Add("linux/arm64");
27+
// }
28+
2229
var buildSettings = new DockerImageBuildSettings
2330
{
2431
Rm = true,
@@ -32,11 +39,13 @@ public static void DockerBuild(this BuildContextBase context, DockerImage docker
3239
$"DISTRO={distro}",
3340
$"VERSION={context.Version.NugetVersion}"
3441
},
35-
// Pull = true,
36-
// Platform = platform // TODO this one is not supported on docker versions < 18.02
42+
Pull = true,
43+
Platform = string.Join(",", platforms),
3744
};
3845

39-
context.DockerBuild(buildSettings, workDir.ToString());
46+
var pushArg = pushImages ? "--push" : string.Empty;
47+
48+
context.DockerBuild(buildSettings, workDir.ToString(), pushArg);
4049
}
4150

4251
public static void DockerPush(this BuildContextBase context, DockerImage dockerImage)
@@ -69,6 +78,31 @@ public static void DockerTestArtifact(this BuildContextBase context, DockerImage
6978
context.DockerTestRun(tag, "sh", cmd);
7079
}
7180

81+
private static void DockerBuild(
82+
this ICakeContext context,
83+
DockerImageBuildSettings settings,
84+
string path, params string[] args)
85+
{
86+
GenericDockerRunner<DockerImageBuildSettings> genericDockerRunner =
87+
new(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
88+
89+
string str1;
90+
switch (string.IsNullOrEmpty(path))
91+
{
92+
case false:
93+
{
94+
string str2 = path.Trim();
95+
str1 = str2.Length <= 1 || !str2.StartsWith("\"") || !str2.EndsWith("\"") ? "\"" + path + "\"" : path;
96+
break;
97+
}
98+
default:
99+
str1 = path;
100+
break;
101+
}
102+
var additional = args.Concat(new[] { str1 }).ToArray();
103+
genericDockerRunner.Run("buildx build", settings, additional);
104+
}
105+
72106
private static void DockerTestRun(this BuildContextBase context, string image, string command, params string[] args)
73107
{
74108
var settings = GetDockerRunSettings(context);

build/docker/Tasks/DockerBuild.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace Docker.Tasks
1111
[TaskArgument(Arguments.DockerDistro, Constants.Alpine312, Constants.Debian10, Constants.Ubuntu2004)]
1212
public class DockerBuild : FrostingTask<BuildContext>
1313
{
14+
protected bool PushImages { get; init; }
15+
public DockerBuild() => this.PushImages = false;
16+
1417
public override bool ShouldRun(BuildContext context)
1518
{
1619
var shouldRun = true;
@@ -28,7 +31,7 @@ public override void Run(BuildContext context)
2831

2932
foreach (var dockerImage in context.Images)
3033
{
31-
context.DockerBuild(dockerImage);
34+
context.DockerBuild(dockerImage, PushImages);
3235
}
3336
}
3437
}

build/docker/Tasks/DockerPublish.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public override bool ShouldRun(BuildContext context)
2121

2222
[TaskName(nameof(DockerPublishInternal))]
2323
[TaskDescription("Publish the docker images containing the GitVersion Tool")]
24-
[IsDependentOn(typeof(DockerTest))]
25-
public class DockerPublishInternal : FrostingTask<BuildContext>
24+
public class DockerPublishInternal : DockerBuild
2625
{
26+
public DockerPublishInternal() => this.PushImages = true;
2727
public override bool ShouldRun(BuildContext context)
2828
{
2929
var shouldRun = true;
@@ -33,13 +33,5 @@ public override bool ShouldRun(BuildContext context)
3333

3434
return shouldRun;
3535
}
36-
37-
public override void Run(BuildContext context)
38-
{
39-
foreach (var dockerImage in context.Images)
40-
{
41-
context.DockerPush(dockerImage);
42-
}
43-
}
4436
}
4537
}

0 commit comments

Comments
 (0)