-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
appbuild.cake
135 lines (127 loc) · 3.66 KB
/
appbuild.cake
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
126
127
128
129
130
131
132
133
134
135
#addin nuget:?package=Cake.Docker&version=0.11.0
using System.Xml.Linq;
var target = Argument("target", "Default");
var pull = Argument<bool>("pull", true);
var push = Argument<bool>("push", false);
var dokreg = Argument<string>("dokreg", "covid19sledilnik");
var setVersion = Argument<string>("buildVersion", null);
var rootDirectory = Directory("./sources/SloCovidServer");
var solution = rootDirectory + File("SloCovidServer.sln");
var appDirectory = rootDirectory + Directory("SloCovidServer");
var appProject = appDirectory + File("SloCovidServer.csproj");
var versionFile = Directory(".") + File("version.xml");
const string versionsRoot = "version";
Task("BuildImage")
.Does(() =>{
string version = GetVersion();
BuildAndPush("data-api-server", new []{ "latest", version });
});
void BuildAndPush(string tag, string[] tagSuffixes)
{
string[] tags = new string[tagSuffixes.Length];
for (int i=0; i< tags.Length; i++)
{
tags[i] = $"{dokreg}/{tag}:{tagSuffixes[i]}";
}
var settings = new DockerImageBuildSettings { Tag = tags, Pull=pull, NoCache=true };
DockerBuild(settings, rootDirectory);
if (push)
{
Information($"Pushing {(string.Join(",", tags))}");
for (int i=0; i<tags.Length; i++)
{
DockerPush(tags[i]);
}
}
else
{
Information($"Won't push {(string.Join(",", tags))}. Set --docker-push=true when push is required.");
}
}
// version has to be in Major.Minor.Build format
bool CheckVersion(string text)
{
if (string.IsNullOrEmpty(text))
{
Error("Version is null");
throw new Exception("Version is null");
}
string[] parts = text.Split('.');
if (parts.Length != 3) {
Error($"Version {text} is not in major.minor.build format");
throw new Exception($"Version {text} is not in major.minor.build format");
}
foreach (string part in parts)
{
if (int.TryParse(part, out int v))
{
if (v < 0) {
Error($"Version {text} part {part} is less than zero");
throw new Exception($"Version {text} part {part} is less than zero");
}
}
else {
Error($"Version {text} part {part} is not an integer");
throw new Exception($"Version {text} part {part} is not an integer");
}
}
return true;
}
XDocument LoadVersions()
{
if (System.IO.File.Exists(versionFile))
{
return XDocument.Load(versionFile);
}
var doc = new XDocument();
doc.Add(new XElement(versionsRoot, "0.0.0"));
return doc;
}
void SaveVersions(XDocument doc) => doc.Save(versionFile);
string GetVersion()
{
var doc = LoadVersions();
return doc.Element(versionsRoot).Value;
}
Task("GetVersion")
.Does(() =>{
var root = GetVersion();
Information($"Version: {root}");
});
Task("SetVersion")
.Does(() => {
var doc = LoadVersions();
var element = doc.Element(versionsRoot);
Information($"Current version is {element.Value}");
if (!string.IsNullOrEmpty(setVersion))
{
CheckVersion(setVersion);
element.Value = setVersion.ToString();
Information($"After setting version is {element.Value}");
SaveVersions(doc);
}
else
{
Warning("No version set. -setVersion is required");
}
});
Task("IncreaseVersion")
.Does(() =>{
var doc = LoadVersions();
var element = doc.Element(versionsRoot);
Information($"Current version is {element.Value}");
string[] versionParts = element.Value.Split('.');
versionParts[2] = (int.Parse(versionParts[2])+1).ToString();
element.Value = string.Join(".", versionParts);
SaveVersions(doc);
Information($"After changing version is {element.Value}");
});
Task("Default")
.Does(() => {
Information("Sledilnik Data API build process targets");
foreach (string target in new string[]{"Default", "BuildImage", "GetVersion", "SetVersion", "IncreaseVersion"})
{
Information($"\t{target}");
}
});
RunTarget(target);