Skip to content

Commit

Permalink
Merge pull request #102 from rasmus/chartmuseum
Browse files Browse the repository at this point in the history
ChartMuseum
  • Loading branch information
rasmus authored Aug 2, 2022
2 parents 0a9504f + c747112 commit 02390d3
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 9 deletions.
1 change: 1 addition & 0 deletions Bake.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".build", ".build", "{F11194
ProjectSection(SolutionItems) = preProject
docker-compose.yml = docker-compose.yml
.github\workflows\pull-requests.yml = .github\workflows\pull-requests.yml
README.md = README.md
.github\workflows\release.yml = .github\workflows\release.yml
RELEASE_NOTES.md = RELEASE_NOTES.md
EndProjectSection
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ Here are some examples of common used arguments to Bake
* `helm-chart>octopus@http://octopus.local/` - Sends Helm charts to the built-in
repository in [Octopus Deploy][octopus-repository]. Bake looks for the API-key
in an environment variable named `OCTOPUS_DEPLOY_APIKEY`
* `helm-chart>chart-museum@http://chart-museum.local/` - Sends Helm charts to an
instance of [ChartMuseum](https://chartmuseum.com/)
* **NuGet**
* `nuget` - An unnamed destination will send NuGet packages to the central
NuGet repository at [nuget.org](https://www.nuget.org/). Bake will look for
Expand Down
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 0.16-beta

* *Nothing yet...*
* New: Ability to upload Helm charts to ChartMuseum

# 0.15-beta

Expand Down
17 changes: 17 additions & 0 deletions Source/Bake.Tests/IntegrationTests/BakeTests/HelmChartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,22 @@ public async Task PushToOctopusDeploy(
returnCode.Should().Be(0);
octopusDeploy.ReceivedPackages.Should().HaveCount(1);
}

[Test]
public async Task PushToChartMuseum()
{
// Arrange
var version = SemVer.Random.ToString();

// Act
var returnCode = await ExecuteAsync(TestState.New(
"run",
"--convention=Release",
$"--destination=helm-chart>chart-museum@http://localhost:5556",
"--build-version", version));

// Assert
returnCode.Should().Be(0);
}
}
}
87 changes: 87 additions & 0 deletions Source/Bake/Cooking/Composers/ChartMuseumComposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// MIT License
//
// Copyright (c) 2021 Rasmus Mikkelsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bake.Services;
using Bake.ValueObjects.Artifacts;
using Bake.ValueObjects.Destinations;
using Bake.ValueObjects.Recipes;
using Bake.ValueObjects.Recipes.ChartMuseum;
using Bake.ValueObjects.Recipes.OctopusDeploy;

namespace Bake.Cooking.Composers
{
public class ChartMuseumComposer : Composer
{
private readonly IConventionInterpreter _conventionInterpreter;

public override IReadOnlyCollection<ArtifactType> Consumes { get; } = new[]
{
ArtifactType.HelmChart,
};

public ChartMuseumComposer(
IConventionInterpreter conventionInterpreter)
{
_conventionInterpreter = conventionInterpreter;
}

public override Task<IReadOnlyCollection<Recipe>> ComposeAsync(
IContext context,
CancellationToken cancellationToken)
{
if (!_conventionInterpreter.ShouldArtifactsBePublished(context.Ingredients.Convention))
{
return Task.FromResult(EmptyRecipes);
}

var chartMuseumDestination = context.Ingredients.Destinations
.OfType<ChartMuseumDestination>()
.SingleOrDefault();

if (chartMuseumDestination == null)
{
return Task.FromResult(EmptyRecipes);
}

var packages = Enumerable.Empty<string>()
.Concat(context.GetArtifacts<HelmChartArtifact>().Select(a => a.Path))
.ToArray();

if (!packages.Any())
{
return Task.FromResult(EmptyRecipes);
}

return Task.FromResult<IReadOnlyCollection<Recipe>>(new Recipe[]
{
new ChartMuseumUploadRecipe(
new Uri(chartMuseumDestination.Url, UriKind.Absolute),
packages)
});
}
}
}
63 changes: 63 additions & 0 deletions Source/Bake/Cooking/Cooks/ChartMuseum/ChartMuseumUploadCook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// MIT License
//
// Copyright (c) 2021 Rasmus Mikkelsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Threading;
using System.Threading.Tasks;
using Bake.Services;
using Bake.ValueObjects.Recipes.ChartMuseum;
using Microsoft.Extensions.Logging;

namespace Bake.Cooking.Cooks.ChartMuseum
{
public class ChartMuseumUploadCook : Cook<ChartMuseumUploadRecipe>
{
private readonly ILogger<ChartMuseumUploadCook> _logger;
private readonly IUploader _uploader;

public ChartMuseumUploadCook(
ILogger<ChartMuseumUploadCook> logger,
IUploader uploader)
{
_logger = logger;
_uploader = uploader;
}

protected override async Task<bool> CookAsync(
IContext context,
ChartMuseumUploadRecipe recipe,
CancellationToken cancellationToken)
{
var url = new Uri(recipe.Url, "/api/charts");

foreach (var packagePath in recipe.Packages)
{
await _uploader.UploadAsync(
packagePath,
url,
cancellationToken);
}

return true;
}
}
}
5 changes: 5 additions & 0 deletions Source/Bake/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Bake.Cooking;
using Bake.Cooking.Composers;
using Bake.Cooking.Cooks;
using Bake.Cooking.Cooks.ChartMuseum;
using Bake.Cooking.Cooks.Docker;
using Bake.Cooking.Cooks.DotNet;
using Bake.Cooking.Cooks.GitHub;
Expand Down Expand Up @@ -81,6 +82,7 @@ public static IServiceCollection AddBake(
.AddTransient<IComposerOrdering, ComposerOrdering>()
.AddTransient<IHelm, Helm>()
.AddTransient<IDescriptionLimiter, DescriptionLimiter>()
.AddSingleton<IUploader, Uploader>()

// Gathers
.AddTransient<IGather, GitGather>()
Expand All @@ -104,6 +106,7 @@ public static IServiceCollection AddBake(
.AddTransient<IComposer, OctopusDeployPackageComposer>()
.AddTransient<IComposer, GitHubReleaseComposer>()
.AddTransient<IComposer, PythonFlaskComposer>()
.AddTransient<IComposer, ChartMuseumComposer>()

// Cooks - .NET
.AddTransient<ICook, DotNetCleanCook>()
Expand All @@ -114,6 +117,8 @@ public static IServiceCollection AddBake(
.AddTransient<ICook, DotNetNuGetPushCook>()
.AddTransient<ICook, DotNetPublishCook>()
.AddTransient<ICook, DotNetDockerFileCook>()
// Cooks - ChartMuseum
.AddTransient<ICook, ChartMuseumUploadCook>()
// Cooks - Docker
.AddTransient<ICook, DockerBuildCook>()
.AddTransient<ICook, DockerPushCook>()
Expand Down
6 changes: 6 additions & 0 deletions Source/Bake/Names.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static class Destinations
public const string NuGetRegistry = "nuget-registry";
public const string Dynamic = "dynamic";
public const string OctopusDeploy = "octopus-deploy";
public const string ChartMuseum = "chart-museum";
}

public static class DynamicDestinations
Expand Down Expand Up @@ -136,6 +137,11 @@ public static class OctopusDeploy
public const string PackageRawPush = "octopus-deploy-package-raw-push";
}

public static class ChartMuseum
{
public const string Upload = "chart-museum-upload";
}

public static class Docker
{
public const string Build = "docker-build";
Expand Down
9 changes: 6 additions & 3 deletions Source/Bake/Services/DestinationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class DestinationParser : IDestinationParser
@"^[a-z\-0-9]+$",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex TypedDestination = new(
"^(?<type>[a-z]+)@(?<rest>.+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
"^(?<type>[a-z-]+)@(?<rest>.+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private const char Separator = '>';

private readonly IDefaults _defaults;
Expand Down Expand Up @@ -101,8 +101,11 @@ public bool TryParse(string str, out Destination destination)

destination = typedMatch.Groups["type"].Value switch
{
"octopus" => Uri.TryCreate(typedMatch.Groups["rest"].Value, UriKind.Absolute, out var octopusDeployUrl)
? new OctopusDeployDestination(octopusDeployUrl.AbsoluteUri)
"octopus" => Uri.TryCreate(typedMatch.Groups["rest"].Value, UriKind.Absolute, out var u1)
? new OctopusDeployDestination(u1.AbsoluteUri)
: null,
Names.Destinations.ChartMuseum => Uri.TryCreate(typedMatch.Groups["rest"].Value, UriKind.Absolute, out var u2)
? new ChartMuseumDestination(u2.AbsoluteUri)
: null,
_ => null
};
Expand Down
36 changes: 36 additions & 0 deletions Source/Bake/Services/IUploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// MIT License
//
// Copyright (c) 2021-2022 Rasmus Mikkelsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Bake.Services
{
public interface IUploader
{
Task UploadAsync(
string filePath,
Uri url,
CancellationToken cancellationToken);
}
}
Loading

0 comments on commit 02390d3

Please sign in to comment.