Skip to content

Commit bba85d9

Browse files
authored
Merge pull request #1190 from MattKotsenas/feature/1189-skip-buildnumber
Add option to set / skip CloudBuildNumber
2 parents e76f27b + 9e7ade1 commit bba85d9

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

src/Cake.GitVersioning/GitVersioningAliases.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static void GitVersioningCloud(this ICakeContext cakeContext, string proj
8787
settings.CISystem?.ToString(),
8888
settings.AllVariables,
8989
settings.CommonVariables,
90+
settings.CloudBuildNumber,
9091
settings.AdditionalVariables,
9192
false);
9293
}

src/Cake.GitVersioning/GitVersioningCloudSettings.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ public class GitVersioningCloudSettings
3030
/// <summary>
3131
/// Gets or sets a value indicating whether to define ALL version variables as cloud build variables, with a "NBGV_" prefix.
3232
/// </summary>
33+
/// <value>The default value is <see langword="false" />.</value>
3334
public bool AllVariables { get; set; }
3435

3536
/// <summary>
3637
/// Gets or sets a value indicating whether to define a few common version variables as cloud build variables, with a "Git" prefix.
3738
/// </summary>
39+
/// <value>The default value is <see langword="false" />.</value>
3840
public bool CommonVariables { get; set; }
3941

42+
/// <summary>
43+
/// Gets or sets a value indicating whether to set the cloud build number.
44+
/// </summary>
45+
/// <value>The default value is <see langword="true" />.</value>
46+
public bool CloudBuildNumber { get; set; } = true;
47+
4048
/// <summary>
4149
/// Gets additional cloud build variables to define.
4250
/// </summary>

src/NerdBank.GitVersioning/Commands/CloudCommand.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@ public enum CloudCommandError
6969
/// <param name="commonVars">
7070
/// Controls whether to define common version variables as cloud build variables.
7171
/// </param>
72+
/// <param name="cloudBuildNumber">
73+
/// Controls whether to emit the cloud build variable to set the build number.
74+
/// </param>
7275
/// <param name="additionalVariables">
7376
/// Additional cloud build variables to define.
7477
/// </param>
7578
/// <param name="alwaysUseLibGit2">
7679
/// Force usage of LibGit2 for accessing the git repository.
7780
/// </param>
78-
public void SetBuildVariables(string projectDirectory, IEnumerable<string> metadata, string version, string ciSystem, bool allVars, bool commonVars, IEnumerable<KeyValuePair<string, string>> additionalVariables, bool alwaysUseLibGit2)
81+
public void SetBuildVariables(string projectDirectory, IEnumerable<string> metadata, string version, string ciSystem, bool allVars, bool commonVars, bool cloudBuildNumber, IEnumerable<KeyValuePair<string, string>> additionalVariables, bool alwaysUseLibGit2)
7982
{
8083
Requires.NotNull(projectDirectory, nameof(projectDirectory));
8184
Requires.NotNull(additionalVariables, nameof(additionalVariables));
@@ -137,7 +140,10 @@ public void SetBuildVariables(string projectDirectory, IEnumerable<string> metad
137140
version = oracle.CloudBuildNumber;
138141
}
139142

140-
activeCloudBuild.SetCloudBuildNumber(version, this.stdout, this.stderr);
143+
if (cloudBuildNumber)
144+
{
145+
activeCloudBuild.SetCloudBuildNumber(version, this.stdout, this.stderr);
146+
}
141147

142148
foreach (KeyValuePair<string, string> pair in variables)
143149
{

src/nbgv/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ private static Parser BuildCommandLine()
194194
var ciSystem = new Option<string>(new[] { "--ci-system", "-s" }, "Force activation for a particular CI system. If not specified, auto-detection will be used. Supported values are: " + string.Join(", ", CloudProviderNames)).FromAmong(CloudProviderNames);
195195
var allVars = new Option<bool>(new[] { "--all-vars", "-a" }, "Defines ALL version variables as cloud build variables, with a \"NBGV_\" prefix.");
196196
var commonVars = new Option<bool>(new[] { "--common-vars", "-c" }, "Defines a few common version variables as cloud build variables, with a \"Git\" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion).");
197+
var skipCloudBuildNumber = new Option<bool>(new[] { "--skip-cloud-build-number" }, "Do not emit the cloud build variable to set the build number. This is useful when you want to set other cloud build variables but not the build number.");
197198
var define = new Option<string[]>(new[] { "--define", "-d" }, () => Array.Empty<string>(), "Additional cloud build variables to define. Each should be in the NAME=VALUE syntax.")
198199
{
199200
Arity = ArgumentArity.OneOrMore,
@@ -207,10 +208,11 @@ private static Parser BuildCommandLine()
207208
ciSystem,
208209
allVars,
209210
commonVars,
211+
skipCloudBuildNumber,
210212
define,
211213
};
212214

213-
cloud.SetHandler(OnCloudCommand, project, metadata, version, ciSystem, allVars, commonVars, define);
215+
cloud.SetHandler(OnCloudCommand, project, metadata, version, ciSystem, allVars, commonVars, skipCloudBuildNumber, define);
214216
}
215217

216218
Command prepareRelease;
@@ -656,7 +658,7 @@ private static Task<int> OnGetCommitsCommand(string project, bool quiet, string
656658
return Task.FromResult((int)ExitCodes.OK);
657659
}
658660

659-
private static Task<int> OnCloudCommand(string project, string[] metadata, string version, string ciSystem, bool allVars, bool commonVars, string[] define)
661+
private static Task<int> OnCloudCommand(string project, string[] metadata, string version, string ciSystem, bool allVars, bool commonVars, bool skipCloudBuildNumber, string[] define)
660662
{
661663
string searchPath = GetSpecifiedOrCurrentDirectoryPath(project);
662664
if (!Directory.Exists(searchPath))
@@ -690,7 +692,7 @@ private static Task<int> OnCloudCommand(string project, string[] metadata, strin
690692
try
691693
{
692694
var cloudCommand = new CloudCommand(Console.Out, Console.Error);
693-
cloudCommand.SetBuildVariables(searchPath, metadata, version, ciSystem, allVars, commonVars, additionalVariables, AlwaysUseLibGit2);
695+
cloudCommand.SetBuildVariables(searchPath, metadata, version, ciSystem, allVars, commonVars, !skipCloudBuildNumber, additionalVariables, AlwaysUseLibGit2);
694696
}
695697
catch (CloudCommand.CloudCommandException ex)
696698
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Nerdbank.GitVersioning;
5+
using Nerdbank.GitVersioning.Commands;
6+
using Xunit;
7+
8+
public class CommandTests : RepoTestBase
9+
{
10+
public CommandTests(ITestOutputHelper logger)
11+
: base(logger)
12+
{
13+
}
14+
15+
[Theory, CombinatorialData]
16+
public void CloudCommand_CloudBuildNumber(bool setCloudBuildNumber)
17+
{
18+
const string ciSystem = "VisualStudioTeamServices";
19+
const string buildNumberSyntax = "##vso[build.updatebuildnumber]";
20+
21+
var outWriter = new StringWriter();
22+
var errWriter = new StringWriter();
23+
24+
var command = new CloudCommand(outWriter, errWriter);
25+
26+
command.SetBuildVariables(this.RepoPath, metadata: [], version: "1.2.3.4", ciSystem, allVars: false, commonVars: false, setCloudBuildNumber, additionalVariables: [], alwaysUseLibGit2: false);
27+
28+
outWriter.Flush();
29+
errWriter.Flush();
30+
31+
if (setCloudBuildNumber)
32+
{
33+
Assert.Contains(buildNumberSyntax, outWriter.ToString());
34+
}
35+
else
36+
{
37+
Assert.DoesNotContain(buildNumberSyntax, outWriter.ToString());
38+
}
39+
40+
Assert.Empty(errWriter.ToString());
41+
}
42+
43+
protected override GitContext CreateGitContext(string path, string committish = null)
44+
=> GitContext.Create(path, committish, engine: GitContext.Engine.ReadWrite);
45+
}

0 commit comments

Comments
 (0)