-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathImageInfo.cs
125 lines (99 loc) · 4.6 KB
/
ImageInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Serilog;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
namespace CreateMatrix;
public class ImageInfo
{
// JSON serialized must be public get and set
public string Name { get; set; } = "";
public string Branch { get; set; } = "";
public string CacheScope { get; set; } = "";
public List<string> Tags { get; set; } = [];
public List<string> Args { get; set; } = [];
private static readonly string[] BaseNames = ["", "LSIO"];
private static readonly string[] RegistryNames = ["docker.io/ptr727" /*, "ghcr.io/ptr727"*/];
private void SetName(ProductInfo.ProductType productType, string baseName)
{
// E.g. NxMeta, NxMeta-LSIO
Name = string.IsNullOrEmpty(baseName) ? productType.ToString() : $"{productType}-{baseName}";
// E.g. default, lsio
CacheScope = string.IsNullOrEmpty(baseName) ? "default" : $"{baseName.ToLower(CultureInfo.InvariantCulture)}";
}
private void AddArgs(VersionInfo versionInfo)
{
Args.Add($"{DownloadVersion}={versionInfo.Version}");
Args.Add($"{DownloadX64Url}={versionInfo.UriX64}");
Args.Add($"{DownloadArm64Url}={versionInfo.UriArm64}");
}
private void AddTag(string tag, string? tagPrefix = null)
{
// E.g. latest, develop-latest
var prefixTag = string.IsNullOrEmpty(tagPrefix) ? tag : $"{tagPrefix}-{tag}";
// E.g. "docker.io/ptr727", "ghcr.io/ptr727"
foreach (var registry in RegistryNames)
{
Tags.Add($"{registry}/{Name.ToLower(CultureInfo.InvariantCulture)}:{prefixTag.ToLower(CultureInfo.InvariantCulture)}");
}
}
public static List<ImageInfo> CreateImages(List<ProductInfo> productList)
{
// Create images for all products
List<ImageInfo> imageList = [];
foreach (var productInfo in productList)
foreach (var baseName in BaseNames)
imageList.AddRange(CreateImages(productInfo, baseName));
// Set branch as "main" on all images
imageList.ForEach(item => item.Branch = "main");
// Create develop tagged images for all products
List<ImageInfo> developList = [];
foreach (var productInfo in productList)
foreach (var baseName in BaseNames)
developList.AddRange(CreateImages(productInfo, baseName, "develop"));
// Set branch as "develop"
developList.ForEach(item => item.Branch = "develop");
// Add images to list
imageList.AddRange(developList);
// Sort args and tags to make diffs easier
imageList.ForEach(item => { item.Args.Sort(); item.Tags.Sort(); } );
return imageList;
}
private static List<ImageInfo> CreateImages(ProductInfo productInfo, string baseName, string? tagPrefix = null)
{
// Create a set by unique versions
var versionSet = productInfo.Versions.ToImmutableSortedSet(new VersionInfoComparer());
Debug.Assert(versionSet.Count == productInfo.Versions.Count);
// Create images for each version
List<ImageInfo> imageList = [];
foreach (var versionUri in versionSet)
{
// Create image
ImageInfo imageInfo = new();
imageInfo.SetName(productInfo.Product, baseName);
// Add a tag for the version
imageInfo.AddTag(versionUri.Version, tagPrefix);
// Add tags for all labels
versionUri.Labels.ForEach(item => imageInfo.AddTag(item.ToString(), tagPrefix));
// Add prefix as a standalone tag for latest
if (!string.IsNullOrEmpty(tagPrefix) && versionUri.Labels.Contains(VersionInfo.LabelType.Latest))
{
imageInfo.AddTag(tagPrefix);
}
// Add args
imageInfo.AddArgs(versionUri);
// Add image to list
imageList.Add(imageInfo);
}
// Done
return imageList;
}
public void LogInformation()
{
Log.Logger.Information("Name: {Name}, Branch: {Branch}, Tags: {Tags}, Args: {Args}", Name, Branch, Tags.Count, Args.Count);
Tags.ForEach(item => Log.Logger.Information("Name: {Name}, Tag: {Tag}", Name, item));
Args.ForEach(item => Log.Logger.Information("Name: {Name}, Arg: {Arg}", Name, item));
}
public const string DownloadVersion = "DOWNLOAD_VERSION";
public const string DownloadX64Url = "DOWNLOAD_X64_URL";
public const string DownloadArm64Url = "DOWNLOAD_ARM64_URL";
}