Skip to content
Merged
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
9 changes: 9 additions & 0 deletions .github/workflows/_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,12 @@ jobs:
docker_registry_password: ${{ secrets.DOCKER_PASSWORD }}
github_registry_username: ${{ github.repository_owner }}
github_registry_password: ${{ secrets.DOCKER_GITHUB_TOKEN }}
-
name: DockerHub Publish Readme
if: success() && github.event_name != 'pull_request' && github.repository_owner == 'GitTools' && github.ref_name == 'main'
shell: pwsh
run: dotnet run/docker.dll --target=DockerHubReadmePublish
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ releaseArtifacts
.dotnet
artifacts
!build/artifacts
src/Docker/**/content
build/docker/content
src/PublicAPI.empty.txt

!**/*.cake
Expand Down
24 changes: 24 additions & 0 deletions build/.run/DockerHub Readme Publish.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="DockerHub Readme Publish" type="DotNetProject" factoryName=".NET Project" folderName="Docker">
<option name="EXE_PATH" value="$PROJECT_DIR$/../run/docker.exe" />
<option name="PROGRAM_PARAMETERS" value="--target=DockerHubReadmePublish" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.." />
<option name="PASS_PARENT_ENVS" value="1" />
<envs>
<env name="DOCKER_USERNAME" value="" />
<env name="DOCKER_PASSWORD" value="" />
</envs>
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/docker/docker.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net7.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
3 changes: 2 additions & 1 deletion build/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
<PackageVersion Include="Cake.Codecov" Version="1.0.1" />
<PackageVersion Include="Cake.Coverlet" Version="3.0.4" />
<PackageVersion Include="Cake.Frosting" Version="3.0.0" />
<PackageVersion Include="Cake.Http" Version="2.0.0" />
<PackageVersion Include="Cake.Incubator" Version="8.0.0" />
<PackageVersion Include="Cake.DotNetLocalTools.Module" Version="3.0.12" />
<PackageVersion Include="Cake.Docker" Version="1.2.0" />
<PackageVersion Include="Cake.Git" Version="3.0.0" />
<PackageVersion Include="Cake.Json" Version="7.0.1" />
<PackageVersion Include="xunit.assert" Version="2.4.2" />
</ItemGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion build/artifacts/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Artifacts;

public class BuildContext : BuildContextBase
{
public string MsBuildConfiguration { get; set; } = Constants.DefaultConfiguration;
public string MsBuildConfiguration { get; } = Constants.DefaultConfiguration;

public bool IsDockerOnLinux { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion build/build/Utilities/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Build.Utilities;

public class Credentials
{
public CodeCovCredentials? CodeCov { get; private set; }
public CodeCovCredentials? CodeCov { get; private init; }

public static Credentials GetCredentials(ICakeContext context) => new()
{
Expand Down
2 changes: 1 addition & 1 deletion build/common/Utilities/DockerContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void DockerBuildImage(this BuildContextBase context, DockerImage d

context.Information($"Building image: {dockerImage}");

var workDir = Paths.Src.Combine("Docker");
var workDir = Paths.Build.Combine("docker");
var tags = context.GetDockerTags(dockerImage, arch);

var suffix = arch.ToSuffix();
Expand Down
2 changes: 2 additions & 0 deletions build/common/Utilities/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public record GitHubCredentials(string Token, string? UserName = null);

public record NugetCredentials(string ApiKey);

public record DockerHubCredentials(string Username, string Password);

public record ChocolateyCredentials(string ApiKey);

public record BuildVersion(GitVersion GitVersion, string? Version, string? Milestone, string? SemVersion, string? NugetVersion, string? ChocolateyVersion, bool IsPreRelease)
Expand Down
4 changes: 2 additions & 2 deletions build/common/Utilities/TaskArgumentsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Common.Utilities;
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public class TaskArgumentAttribute : Attribute
{
public string Name { get; set; }
public string[] PossibleValues { get; set; }
public string Name { get; }
public string[] PossibleValues { get; }
public TaskArgumentAttribute(string name, params string[] possibleValues)
{
Name = name;
Expand Down
2 changes: 2 additions & 0 deletions build/docker/BuildContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Common.Utilities;
using Docker.Utilities;

namespace Docker;

public class BuildContext : BuildContextBase
{
public Credentials? Credentials { get; set; }
public bool IsDockerOnLinux { get; set; }

public IEnumerable<DockerImage> Images { get; set; } = new List<DockerImage>();
Expand Down
3 changes: 3 additions & 0 deletions build/docker/BuildLifetime.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Common.Lifetime;
using Common.Utilities;
using Docker.Utilities;

namespace Docker;

Expand All @@ -9,6 +10,8 @@ public override void Setup(BuildContext context, ISetupContext info)
{
base.Setup(context, info);

context.Credentials = Credentials.GetCredentials(context);

context.IsDockerOnLinux = context.DockerCustomCommand("info --format '{{.OSType}}'").First().Replace("'", "") == "linux";

var architecture = context.HasArgument(Arguments.Architecture) ? context.Argument<Architecture>(Arguments.Architecture) : (Architecture?)null;
Expand Down
6 changes: 3 additions & 3 deletions src/Docker/Dockerfile → build/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG REGISTRY='docker.io'
ARG DISTRO='debian-9'
ARG DOTNET_VERSION='3.1'
ARG VERSION='5.5.1'
ARG DISTRO='debian-11'
ARG DOTNET_VERSION='6.0'
ARG VERSION='5.12.0'

FROM $REGISTRY/gittools/build-images:$DISTRO-sdk-$DOTNET_VERSION as installer
ARG contentFolder
Expand Down
2 changes: 1 addition & 1 deletion build/docker/Tasks/DockerBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override bool ShouldRun(BuildContext context)
public override void Run(BuildContext context)
{
var tool = Paths.Nuget.CombineWithFilePath("GitVersion.Tool*");
var dest = Paths.Src.Combine("Docker").Combine("content");
var dest = Paths.Build.Combine("docker").Combine("content");
context.EnsureDirectoryExists(dest);
context.CopyFiles(tool.FullPath, dest);

Expand Down
82 changes: 82 additions & 0 deletions build/docker/Tasks/DockerHubReadmePublish.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Cake.Http;
using Cake.Json;
using Common.Utilities;

namespace Docker.Tasks;

[TaskName(nameof(DockerHubReadmePublish))]
[TaskDescription("Publish the DockerHub updated README.md")]
public class DockerHubReadmePublish : FrostingTask<BuildContext>
{
public override bool ShouldRun(BuildContext context)
{
var shouldRun = false;
if (context.DockerRegistry == DockerRegistry.DockerHub)
{
shouldRun &= context.ShouldRun(context.IsStableRelease, $"{nameof(DockerHubReadmePublish)} works only for tagged releases.");
}

return shouldRun;
}

public override void Run(BuildContext context)
{
var readme = GetReadmeContent(context);

var response = context.HttpPost("https://hub.docker.com/v2/users/login", settings =>
{
var credentials = context.Credentials!.DockerHub!;
settings
.SetContentType("application/json")
.SetJsonRequestBody(new { username = credentials.Username, password = credentials.Password });
});


context.HttpPatch("https://hub.docker.com/v2/repositories/gittools/gitversion", settings =>
{
var token = context.ParseJson(response).Value<string>("token");
settings
.SetContentType("application/json")
.SetAuthorization($"JWT", token)
.SetJsonRequestBody(new { full_description = readme });
});
}

private static string GetReadmeContent(BuildContextBase context)
{
var version = context.Version!.GitVersion.MajorMinorPatch;
const string distro = Constants.AlpineLatest;
const string dotnetVersion = Constants.VersionLatest;
var tag = $"{version}-{distro}-{dotnetVersion}";
// language=markdown
var readme = $"""
# GitVersion

This repository contains the Docker images for [GitVersion](https://gitversion.net).

## Usage

The recommended image to run is `alpine`, as they are the smallest Docker images we provide (83 MB). This will execute GitVersion for the current working directory (`$(pwd)`) on Linux and Unix or powershell on Windows:

```sh
docker run --rm -v "$(pwd):/repo" gittools/gitversion:{tag} /repo
```
The following command will execute GitVersion for the current working directory (`%CD%`) on Windows with CMD:

```sh
docker run --rm -v "%CD%:/repo" gittools/gitversion:{tag} /repo
```
Note that the path `/repo` needs to be passed as an argument since the `gitversion` executable within the container is not aware of the fact that it's running inside a container.

### Tags

Most of the tags we provide have both arm64 and amd64 variants. If you need to pull a architecture specific tag you can do that like:

```sh
docker run --rm -v "$(pwd):/repo" gittools/gitversion:{tag}-amd64 /repo
docker run --rm -v "$(pwd):/repo" gittools/gitversion:{tag}-arm64 /repo
```
""";
return readme;
}
}
15 changes: 15 additions & 0 deletions build/docker/Utilities/Credentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Common.Utilities;

namespace Docker.Utilities;

public class Credentials
{
public DockerHubCredentials? DockerHub { get; private init; }

public static Credentials GetCredentials(ICakeContext context) => new()
{
DockerHub = new DockerHubCredentials(
context.EnvironmentVariable("DOCKER_USERNAME"),
context.EnvironmentVariable("DOCKER_PASSWORD")),
};
}
9 changes: 9 additions & 0 deletions build/docker/docker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@
<ItemGroup>
<ProjectReference Include="..\common\common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Cake.Http" />
<PackageReference Include="Cake.Json" />
</ItemGroup>
<ItemGroup>
<Compile Remove="content\**" />
<EmbeddedResource Remove="content\**" />
<None Remove="content\**" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion build/docs/Utilities/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Docs.Utilities;

public class Credentials
{
public GitHubCredentials? GitHub { get; private set; }
public GitHubCredentials? GitHub { get; private init; }
public static Credentials GetCredentials(ICakeContext context) => new()
{
GitHub = new GitHubCredentials(
Expand Down
2 changes: 1 addition & 1 deletion build/publish/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class BuildContext : BuildContextBase
{
public Credentials? Credentials { get; set; }

public List<NugetPackage> Packages { get; set; } = new();
public List<NugetPackage> Packages { get; } = new();
public BuildContext(ICakeContext context) : base(context)
{
}
Expand Down
6 changes: 3 additions & 3 deletions build/publish/Utilities/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Publish.Utilities;

public class Credentials
{
public GitHubCredentials? GitHub { get; private set; }
public NugetCredentials? Nuget { get; private set; }
public ChocolateyCredentials? Chocolatey { get; private set; }
public GitHubCredentials? GitHub { get; private init; }
public NugetCredentials? Nuget { get; private init; }
public ChocolateyCredentials? Chocolatey { get; private init; }

public static Credentials GetCredentials(ICakeContext context) => new()
{
Expand Down
2 changes: 1 addition & 1 deletion build/release/Utilities/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Release.Utilities;

public class Credentials
{
public GitHubCredentials? GitHub { get; private set; }
public GitHubCredentials? GitHub { get; private init; }
public static Credentials GetCredentials(ICakeContext context) => new()
{
GitHub = new GitHubCredentials(context.EnvironmentVariable("GITHUB_TOKEN")),
Expand Down