Skip to content

Commit 32c5c35

Browse files
author
Michal Ciechan
committed
Adding UseBuildAgentBranch flag
Adding AzurePipelinesSetParamSkipIsOutput
1 parent 22b8be5 commit 32c5c35

File tree

10 files changed

+85
-12
lines changed

10 files changed

+85
-12
lines changed

src/GitVersionCore/BuildAgents/AzurePipelines.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text.RegularExpressions;
45
using GitVersion.Extensions;
56
using GitVersion.Logging;
67
using GitVersion.OutputVariables;
8+
using Microsoft.Extensions.Options;
79

810
namespace GitVersion.BuildAgents
911
{
1012
public class AzurePipelines : BuildAgentBase
1113
{
12-
public AzurePipelines(IEnvironment environment, ILog log) : base(environment, log)
14+
private readonly Func<GitVersionOptions> options;
15+
16+
public AzurePipelines(IEnvironment environment, ILog log, Func<GitVersionOptions> options) : base(environment, log)
1317
{
18+
this.options = options;
1419
}
1520

1621
public const string EnvironmentVariableName = "TF_BUILD";
@@ -19,11 +24,20 @@ public AzurePipelines(IEnvironment environment, ILog log) : base(environment, lo
1924

2025
public override string[] GenerateSetParameterMessage(string name, string value)
2126
{
22-
return new[]
23-
{
24-
$"##vso[task.setvariable variable=GitVersion.{name}]{value}",
25-
$"##vso[task.setvariable variable=GitVersion.{name};isOutput=true]{value}"
26-
};
27+
var standardSet = $"##vso[task.setvariable variable=GitVersion.{name}]{value}";
28+
var isOutputSet = $"##vso[task.setvariable variable=GitVersion.{name};isOutput=true]{value}";
29+
30+
return options().Settings.AzurePipelinesSetParamSkipIsOutput
31+
? new[]
32+
{
33+
standardSet
34+
}
35+
: new[]
36+
{
37+
standardSet,
38+
isOutputSet
39+
40+
};
2741
}
2842

2943
public override string GetCurrentBranch(bool usingDynamicRepos)

src/GitVersionCore/Core/GitVersionContextFactory.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using GitVersion.Common;
33
using GitVersion.Configuration;
44
using GitVersion.Extensions;
5+
using GitVersion.Logging;
56
using LibGit2Sharp;
67
using Microsoft.Extensions.Options;
78

@@ -13,21 +14,45 @@ public class GitVersionContextFactory : IGitVersionContextFactory
1314
private readonly IRepositoryMetadataProvider repositoryMetadataProvider;
1415
private readonly IBranchConfigurationCalculator branchConfigurationCalculator;
1516
private readonly IOptions<GitVersionOptions> options;
17+
private readonly ICurrentBuildAgent buildAgent;
18+
private readonly ILog log;
1619

17-
public GitVersionContextFactory(IConfigProvider configProvider, IRepositoryMetadataProvider repositoryMetadataProvider, IBranchConfigurationCalculator branchConfigurationCalculator, IOptions<GitVersionOptions> options)
20+
public GitVersionContextFactory(IConfigProvider configProvider, IRepositoryMetadataProvider repositoryMetadataProvider, IBranchConfigurationCalculator branchConfigurationCalculator, IOptions<GitVersionOptions> options, ICurrentBuildAgent buildAgent, ILog log)
1821
{
1922
this.configProvider = configProvider ?? throw new ArgumentNullException(nameof(configProvider));
2023
this.repositoryMetadataProvider = repositoryMetadataProvider ?? throw new ArgumentNullException(nameof(repositoryMetadataProvider));
2124
this.branchConfigurationCalculator = branchConfigurationCalculator ?? throw new ArgumentNullException(nameof(branchConfigurationCalculator));
2225
this.options = options ?? throw new ArgumentNullException(nameof(options));
26+
this.buildAgent = buildAgent;
27+
this.log = log;
2328
}
2429

2530
public GitVersionContext Create(GitVersionOptions gitVersionOptions)
2631
{
27-
var targetBranch = repositoryMetadataProvider.GetTargetBranch(gitVersionOptions.RepositoryInfo.TargetBranch);
32+
var currentBranch = gitVersionOptions.Settings.UseBuildAgentBranch
33+
? ResolveCurrentBranch()
34+
: gitVersionOptions.RepositoryInfo.TargetBranch;
35+
36+
var targetBranch = repositoryMetadataProvider.GetTargetBranch(currentBranch);
2837
return Init(targetBranch, gitVersionOptions.RepositoryInfo.CommitId, gitVersionOptions.Settings.OnlyTrackedBranches);
2938
}
3039

40+
public string ResolveCurrentBranch()
41+
{
42+
var gitVersionOptions = options.Value;
43+
var targetBranch = gitVersionOptions.RepositoryInfo.TargetBranch;
44+
if (buildAgent == null)
45+
{
46+
return targetBranch;
47+
}
48+
49+
var isDynamicRepository = !string.IsNullOrWhiteSpace(gitVersionOptions.RepositoryInfo.DynamicRepositoryClonePath);
50+
var currentBranch = buildAgent.GetCurrentBranch(isDynamicRepository) ?? targetBranch;
51+
log.Info("Branch from build environment: " + currentBranch);
52+
53+
return currentBranch;
54+
}
55+
3156
private GitVersionContext Init(Branch currentBranch, string commitId = null, bool onlyTrackedBranches = false)
3257
{
3358
if (currentBranch == null)

src/GitVersionCore/Extensions/StringExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public static bool ArgumentRequiresValue(this string argument, int argumentIndex
108108
"nofetch",
109109
"nonormalize",
110110
"nocache",
111+
"usebuildagentbranch",
112+
"azurepipelines-setparamskipisoutput"
111113
};
112114

113115
var argumentMightRequireValue = !booleanArguments.Contains(argument.Substring(1), StringComparer.OrdinalIgnoreCase);

src/GitVersionCore/Model/Settings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ public class Settings
66
public bool NoCache;
77
public bool NoNormalize;
88
public bool OnlyTrackedBranches = false;
9+
public bool UseBuildAgentBranch;
10+
public bool AzurePipelinesSetParamSkipIsOutput;
911
}
1012
}

src/GitVersionExe/ArgumentParser.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ private static bool ParseSwitches(Arguments arguments, string name, string[] val
272272
return true;
273273
}
274274

275+
if (name.IsSwitch("usebuildagentbranch"))
276+
{
277+
arguments.UseBuildAgentBranch = true;
278+
return true;
279+
}
280+
281+
if (name.IsSwitch("azurepipelines-setparamskipisoutput"))
282+
{
283+
arguments.AzurePipelinesSetParamSkipIsOutput = true;
284+
return true;
285+
}
286+
275287
if (name.IsSwitch("verbosity"))
276288
{
277289
ParseVerbosity(arguments, value);

src/GitVersionExe/Arguments.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class Arguments
3030
public bool NoFetch;
3131
public bool NoCache;
3232
public bool NoNormalize;
33+
public bool UseBuildAgentBranch;
34+
public bool AzurePipelinesSetParamSkipIsOutput;
3335

3436
public string LogFilePath;
3537
public string ShowVariable;
@@ -85,6 +87,8 @@ public GitVersionOptions ToOptions()
8587
NoFetch = NoFetch,
8688
NoCache = NoCache,
8789
NoNormalize = NoNormalize,
90+
UseBuildAgentBranch = UseBuildAgentBranch,
91+
AzurePipelinesSetParamSkipIsOutput = AzurePipelinesSetParamSkipIsOutput,
8892
},
8993

9094
WixInfo =

src/GitVersionExe/HelpWriter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ All the GitVersion variables are written to 'GitVersion_WixVersion.wxi'.
7878
/dynamicRepoLocation
7979
By default dynamic repositories will be cloned to %tmp%. Use this switch to override
8080
/nofetch Disables 'git fetch' during version calculation. Might cause GitVersion to not calculate your version as expected.
81+
/usebuildagentbranch
82+
Forces the use of Build Agent branch if available as a starting point for calculating your version.
83+
Might cause GitVersion to not calculate your version as expected for transient branches.
84+
/azurepipelines-setparamskipisoutput
85+
Azure Pipelines Build Agent Only.
86+
Forces variable outputs not to contain isoutput=true when setting params. This causes errors in certain TFS servers.
8187
8288
# Execute build args
8389
/exec Executes target executable making GitVersion variables available as environmental variables

src/GitVersionExe/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ private IHostBuilder CreateHostBuilder(string[] args) =>
3535
})
3636
.ConfigureServices((hostContext, services) =>
3737
{
38-
services.AddModule(new GitVersionCoreModule());
39-
services.AddModule(new GitVersionExeModule());
40-
4138
services.AddSingleton(sp =>
4239
{
4340
var arguments = sp.GetService<IArgumentParser>().ParseArguments(args);
4441
var gitVersionOptions = arguments.ToOptions();
4542
return Options.Create(gitVersionOptions);
4643
});
4744

45+
services.AddSingleton<Func<GitVersionOptions>>(sp => () => sp.GetRequiredService<IOptions<GitVersionOptions>>().Value);
46+
47+
services.AddModule(new GitVersionCoreModule());
48+
services.AddModule(new GitVersionExeModule());
49+
4850
overrides?.Invoke(services);
4951
services.AddHostedService<GitVersionApp>();
5052
})

src/GitVersionTask.MsBuild/GitVersionTaskBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ protected GitVersionTaskBase()
2222

2323
public bool NoNormalize { get; set; }
2424

25+
public bool UseBuildAgentBranch { get; set; }
26+
27+
public bool AzurePipelinesSetParamSkipIsOutput { get; set; }
28+
2529
public TaskLoggingHelper Log { get; }
2630

2731
public bool Execute()

src/GitVersionTask/GitVersionTasks.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ private static IServiceProvider BuildServiceProvider(GitVersionTaskBase task)
6969
Settings =
7070
{
7171
NoFetch = task.NoFetch,
72-
NoNormalize = task.NoNormalize
72+
NoNormalize = task.NoNormalize,
73+
UseBuildAgentBranch = task.UseBuildAgentBranch,
74+
AzurePipelinesSetParamSkipIsOutput = task.AzurePipelinesSetParamSkipIsOutput
7375
}
7476
};
7577

0 commit comments

Comments
 (0)