-
Notifications
You must be signed in to change notification settings - Fork 0
/
Git.cake
72 lines (61 loc) · 1.88 KB
/
Git.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
public static class Git
{
public static readonly string Username = Environment.GetEnvironmentVariable("GIT_USERNAME");
public static readonly string Password = Environment.GetEnvironmentVariable("GIT_PASSWORD");
public static string Execute(string arguments)
{
var process = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "git",
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
});
return process.StandardOutput.ReadToEnd().Trim();
}
public static string CurrentBranch()
{
return Execute("rev-parse --abbrev-ref HEAD");
}
public static string GetOriginUri()
{
return Execute("config --get remote.origin.url");
}
public static string GetOriginUriWithoutSuffix()
{
var uri = GetOriginUri();
var index = uri.LastIndexOf(".git");
if (index >= 0)
uri = uri.Remove(index);
return uri;
}
}
public static bool IsMaster(this string branch)
{
return branch.Equals("master", StringComparison.OrdinalIgnoreCase);
}
Task("git-tag-build")
.WithCriteria(!BuildSystem.IsLocalBuild) // Only during CI/CD
.Does(() =>
{
Information("Adding tag to git repo ...");
if (string.IsNullOrWhiteSpace(Git.Username))
{
Information($"Git username was not found, tag could not be created.");
}
else if (string.IsNullOrWhiteSpace(Git.Password))
{
Information($"Git password was not found, tag could not be created.");
}
else
{
Information(Git.Execute($"tag {Build.Version}"));
var origin = new Uri(Git.GetOriginUri());
var username = System.Net.WebUtility.UrlEncode(Git.Username);
var password = System.Net.WebUtility.UrlEncode(Git.Password);
var uri = $"http://{username}:{password}@{origin.Host}{origin.PathAndQuery}";
Information($"Pushing tag '{Build.Version}' to '{origin}' ...");
Information(Git.Execute($"push {uri} {Build.Version}"));
}
});