Skip to content

Commit cae0f62

Browse files
committed
Add support for Jenkins Pipeline (multi-branch)
1 parent ed3d41b commit cae0f62

File tree

7 files changed

+42
-5
lines changed

7 files changed

+42
-5
lines changed

src/GitVersionCore.Tests/ConfigProviderTests.CanWriteOutEffectiveConfiguration.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ branches:
4646
tag: PullRequest
4747
increment: Inherit
4848
prevent-increment-of-merged-branch-version: false
49-
tag-number-pattern: '[/-](?<number>\d+)[-/]'
49+
tag-number-pattern: '[/-](?<number>\d+)'
5050
track-merge-target: false
5151
regex: (pull|pull\-requests|pr)[/-]
5252
tracks-release-branches: false

src/GitVersionCore/BuildServers/BuildServerBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@ public virtual void WriteIntegration(Action<string> writer, VersionVariables var
3333
writer(buildParameter);
3434
}
3535
}
36+
37+
public virtual bool ShouldCleanUpRemotes()
38+
{
39+
return false;
40+
}
3641
}
3742
}

src/GitVersionCore/BuildServers/IBuildServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public interface IBuildServer
1313
/// If the build server should not try and fetch
1414
/// </summary>
1515
bool PreventFetch();
16+
bool ShouldCleanUpRemotes();
1617
}
1718
}

src/GitVersionCore/BuildServers/Jenkins.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,31 @@ public override string[] GenerateSetParameterMessage(string name, string value)
3636

3737
public override string GetCurrentBranch(bool usingDynamicRepos)
3838
{
39-
return Environment.GetEnvironmentVariable("GIT_LOCAL_BRANCH") ?? Environment.GetEnvironmentVariable("GIT_BRANCH");
39+
return IsPipelineAsCode()
40+
? Environment.GetEnvironmentVariable("BRANCH_NAME")
41+
: (Environment.GetEnvironmentVariable("GIT_LOCAL_BRANCH") ?? Environment.GetEnvironmentVariable("GIT_BRANCH"));
42+
}
43+
44+
private bool IsPipelineAsCode()
45+
{
46+
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BRANCH_NAME"));
4047
}
4148

4249
public override bool PreventFetch()
4350
{
4451
return true;
4552
}
4653

54+
/// <summary>
55+
/// When Jenkins uses pipeline-as-code, it creates two remotes: "origin" and "origin1".
56+
/// This should be cleaned up, so that normizaling the Git repo will not fail.
57+
/// </summary>
58+
/// <returns></returns>
59+
public override bool ShouldCleanUpRemotes()
60+
{
61+
return IsPipelineAsCode();
62+
}
63+
4764
public override void WriteIntegration(Action<string> writer, VersionVariables variables)
4865
{
4966
base.WriteIntegration(writer, variables);

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static void ApplyDefaultsTo(Config config)
122122
GetOrCreateBranchDefaults(config, PullRequestBranchKey),
123123
PullRequestRegex,
124124
defaultTag: "PullRequest",
125-
defaultTagNumberPattern: @"[/-](?<number>\d+)[-/]",
125+
defaultTagNumberPattern: @"[/-](?<number>\d+)",
126126
defaultIncrementStrategy: IncrementStrategy.Inherit);
127127
ApplyBranchDefaults(config,
128128
GetOrCreateBranchDefaults(config, HotfixBranchKey),

src/GitVersionCore/ExecuteCore.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
2525
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
2626
var buildServer = applicableBuildServers.FirstOrDefault();
2727
var fetch = noFetch || (buildServer != null && buildServer.PreventFetch());
28+
var shouldCleanUpRemotes = buildServer != null && buildServer.ShouldCleanUpRemotes();
2829
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, fetch, workingDirectory);
29-
gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch, !string.IsNullOrWhiteSpace(dynamicRepositoryLocation)));
30+
gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch, !string.IsNullOrWhiteSpace(dynamicRepositoryLocation)), shouldCleanUpRemotes);
3031
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
3132
var projectRoot = gitPreparer.GetProjectRootDirectory();
3233

src/GitVersionCore/GitPreparer.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ public bool IsDynamicGitRepository
5151

5252
public string DynamicGitRepositoryPath { get; private set; }
5353

54-
public void Initialise(bool normaliseGitDirectory, string currentBranch)
54+
public void Initialise(bool normaliseGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false)
5555
{
5656
if (string.IsNullOrWhiteSpace(targetUrl))
5757
{
5858
if (normaliseGitDirectory)
5959
{
6060
using (Logger.IndentLog(string.Format("Normalizing git directory for branch '{0}'", currentBranch)))
6161
{
62+
if (shouldCleanUpRemotes)
63+
{
64+
CleanupDuplicateOrigin();
65+
}
6266
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
6367
}
6468
}
@@ -70,6 +74,15 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch)
7074
DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, targetUrl, currentBranch, noFetch);
7175
}
7276

77+
private void CleanupDuplicateOrigin()
78+
{
79+
var repo = new Repository(GetDotGitDirectory());
80+
if (repo.Network.Remotes.Any(remote => remote.Name == "origin1"))
81+
{
82+
repo.Network.Remotes.Remove("origin1");
83+
}
84+
}
85+
7386
public TResult WithRepository<TResult>(Func<IRepository, TResult> action)
7487
{
7588
using (IRepository repo = new Repository(GetDotGitDirectory()))

0 commit comments

Comments
 (0)