Skip to content

Commit

Permalink
GitTools#2742 - update dockerhub readme on stable releases
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Mar 5, 2023
1 parent fd3ac39 commit 30bc08d
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 1 deletion.
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 }}

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: 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
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
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")),
};
}
4 changes: 4 additions & 0 deletions build/docker/docker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<ItemGroup>
<ProjectReference Include="..\common\common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Cake.Http" />
<PackageReference Include="Cake.Json" />
</ItemGroup>
</Project>

0 comments on commit 30bc08d

Please sign in to comment.