Skip to content

Commit 73e5c58

Browse files
committed
Merge pull request #551 from openkas/jenkinsbuildserver_313
Support for Jenkins CI
2 parents a759194 + 208b81b commit 73e5c58

File tree

8 files changed

+206
-21
lines changed

8 files changed

+206
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Jenkins Setup
2+
3+
Injecting environment variables is not supported in Jenkins natively, but Jenkins plugins exist that provide this functionality. Of these plugins [EnvInject] appears to be the most popular with over 20k downloads per month.
4+
5+
To inject the GitVersion variables as environment variables for a build job using [EnvInject], do the following:
6+
7+
1. Add an **Execute Windows batch command** build step with *Command*:
8+
`gitversion /output buildserver`
9+
1. Add an **Inject environment variables** build step and use value 'gitversion.properties' for the *Properties File Path* parameter
10+
11+
This assumes GitVersion.exe is available on the command line.
12+
13+
You can verify correct injection of environment variables by adding another "Execute Windows batch command" build step with the following *Command*:
14+
15+
```
16+
@echo Retrieving some GitVersion environment variables:
17+
@echo %GitVersion_SemVer%
18+
@echo %GitVersion_BranchName%
19+
@echo %GitVersion_AssemblySemVer%
20+
@echo %GitVersion_MajorMinorPatch%
21+
@echo %GitVersion_Sha%
22+
```
23+
24+
[EnvInject]: https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using GitVersion;
3+
using NUnit.Framework;
4+
using Shouldly;
5+
6+
[TestFixture]
7+
public class EnvironmentVariableJenkinsTests
8+
{
9+
string key = "JENKINS_URL";
10+
11+
private void SetEnvironmentVariableForDetection()
12+
{
13+
Environment.SetEnvironmentVariable(key, "a value", EnvironmentVariableTarget.Process);
14+
}
15+
16+
private void ClearEnvironmentVariableForDetection()
17+
{
18+
Environment.SetEnvironmentVariable(key, null, EnvironmentVariableTarget.Process);
19+
}
20+
21+
[Test]
22+
public void CanApplyCurrentContextWhenEnvironmentVariableIsSet()
23+
{
24+
SetEnvironmentVariableForDetection();
25+
var j = new Jenkins();
26+
j.CanApplyToCurrentContext().ShouldBe(true);
27+
}
28+
29+
[Test]
30+
public void CanNotApplyCurrentContextWhenEnvironmentVariableIsNotSet()
31+
{
32+
ClearEnvironmentVariableForDetection();
33+
var j = new Jenkins();
34+
j.CanApplyToCurrentContext().ShouldBe(false);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using GitVersion;
5+
using NUnit.Framework;
6+
using Shouldly;
7+
8+
[TestFixture]
9+
public class JenkinsMessageGenerationTests
10+
{
11+
[Test]
12+
public void GenerateSetVersionMessageReturnsVersionAsIs_AlthoughThisIsNotUsedByJenkins()
13+
{
14+
var j = new Jenkins();
15+
j.GenerateSetVersionMessage("0.0.0-Beta4.7").ShouldBe("0.0.0-Beta4.7");
16+
}
17+
18+
[Test]
19+
public void GenerateMessageTest()
20+
{
21+
var j = new Jenkins();
22+
var generatedParameterMessages = j.GenerateSetParameterMessage("name", "value");
23+
generatedParameterMessages.Length.ShouldBe(1);
24+
generatedParameterMessages[0].ShouldBe("GitVersion_name=value");
25+
}
26+
27+
[Test, Explicit]
28+
public void WriteAllVariablesToTheTextWriter()
29+
{
30+
// this test method writes to disc, hence marked explicit
31+
var f = "this_file_should_be_deleted.properties";
32+
33+
try
34+
{
35+
AssertVariablesAreWrittenToFile(f);
36+
}
37+
finally
38+
{
39+
File.Delete(f);
40+
}
41+
}
42+
43+
static void AssertVariablesAreWrittenToFile(string f)
44+
{
45+
var writes = new List<string>();
46+
var semanticVersion = new SemanticVersion
47+
{
48+
Major = 1,
49+
Minor = 2,
50+
Patch = 3,
51+
PreReleaseTag = "beta1",
52+
BuildMetaData = "5"
53+
};
54+
55+
semanticVersion.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z");
56+
semanticVersion.BuildMetaData.Sha = "commitSha";
57+
var variables = VariableProvider.GetVariablesFor(semanticVersion, AssemblyVersioningScheme.MajorMinorPatch, VersioningMode.ContinuousDelivery, "ci", false);
58+
59+
var j = new Jenkins(f);
60+
61+
j.WriteIntegration(writes.Add, variables);
62+
63+
writes[1].ShouldBe("1.2.3-beta.1+5");
64+
65+
File.Exists(f).ShouldBe(true);
66+
67+
var props = File.ReadAllText(f);
68+
69+
props.ShouldContain("GitVersion_Major=1");
70+
props.ShouldContain("GitVersion_Minor=2");
71+
}
72+
}

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
<ItemGroup>
9898
<Compile Include="BuildServers\BuildServerBaseTests.cs" />
9999
<Compile Include="BuildServers\ContinuaCiTests.cs" />
100+
<Compile Include="BuildServers\EnvironmentVariableJenkinsTests.cs" />
101+
<Compile Include="BuildServers\JenkinsMessageGenerationTests.cs" />
100102
<Compile Include="BuildServers\MyGetTests.cs" />
101103
<Compile Include="BuildServers\TeamCityTests.cs" />
102104
<Compile Include="Fixtures\CommitCountingRepoFixture.cs" />
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
{
2-
"Major":1,
3-
"Minor":2,
4-
"Patch":3,
5-
"PreReleaseTag":"unstable.4",
6-
"PreReleaseTagWithDash":"-unstable.4",
7-
"BuildMetaData":5,
8-
"FullBuildMetaData":"5.Branch.feature1.Sha.commitSha",
9-
"MajorMinorPatch":"1.2.3",
10-
"SemVer":"1.2.3-unstable.4",
11-
"LegacySemVer":"1.2.3-unstable4",
12-
"LegacySemVerPadded":"1.2.3-unstable0004",
13-
"AssemblySemVer":"1.2.3.0",
14-
"FullSemVer":"1.2.3-unstable.4+5",
15-
"InformationalVersion":"1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha",
16-
"BranchName":"feature1",
17-
"Sha":"commitSha",
18-
"NuGetVersionV2":"1.2.3-unstable0004",
19-
"NuGetVersion":"1.2.3-unstable0004",
20-
"CommitDate":"2014-03-06"
1+
{
2+
"Major":1,
3+
"Minor":2,
4+
"Patch":3,
5+
"PreReleaseTag":"unstable.4",
6+
"PreReleaseTagWithDash":"-unstable.4",
7+
"BuildMetaData":5,
8+
"FullBuildMetaData":"5.Branch.feature1.Sha.commitSha",
9+
"MajorMinorPatch":"1.2.3",
10+
"SemVer":"1.2.3-unstable.4",
11+
"LegacySemVer":"1.2.3-unstable4",
12+
"LegacySemVerPadded":"1.2.3-unstable0004",
13+
"AssemblySemVer":"1.2.3.0",
14+
"FullSemVer":"1.2.3-unstable.4+5",
15+
"InformationalVersion":"1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha",
16+
"BranchName":"feature1",
17+
"Sha":"commitSha",
18+
"NuGetVersionV2":"1.2.3-unstable0004",
19+
"NuGetVersion":"1.2.3-unstable0004",
20+
"CommitDate":"2014-03-06"
2121
}

src/GitVersionCore/BuildServers/BuildServerList.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public static class BuildServerList
1010
new ContinuaCi(),
1111
new TeamCity(),
1212
new AppVeyor(),
13-
new MyGet()
13+
new MyGet(),
14+
new Jenkins()
1415
};
1516

1617
public static IEnumerable<IBuildServer> GetApplicableBuildServers()
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using System.IO;
5+
6+
public class Jenkins : BuildServerBase
7+
{
8+
string _file;
9+
10+
public Jenkins() : this("gitversion.properties")
11+
{
12+
}
13+
14+
public Jenkins(string propertiesFileName)
15+
{
16+
_file = propertiesFileName;
17+
}
18+
19+
public override bool CanApplyToCurrentContext()
20+
{
21+
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("JENKINS_URL"));
22+
}
23+
24+
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)
25+
{
26+
return versionToUseForBuildNumber;
27+
}
28+
29+
public override string[] GenerateSetParameterMessage(string name, string value)
30+
{
31+
return new[]
32+
{
33+
string.Format("GitVersion_{0}={1}", name, value)
34+
};
35+
}
36+
37+
public override void WriteIntegration(Action<string> writer, VersionVariables variables)
38+
{
39+
base.WriteIntegration(writer, variables);
40+
writer(string.Format("Outputting variables to '{0}' ... ", _file));
41+
WriteVariablesFile(variables);
42+
}
43+
44+
void WriteVariablesFile(VersionVariables variables)
45+
{
46+
File.WriteAllLines(_file, BuildOutputFormatter.GenerateBuildLogOutput(this, variables));
47+
}
48+
}
49+
}

src/GitVersionCore/GitVersionCore.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<Compile Include="BuildServers\ContinuaCi.cs" />
7272
<Compile Include="BuildServers\GitHelper.cs" />
7373
<Compile Include="BuildServers\IBuildServer.cs" />
74+
<Compile Include="BuildServers\Jenkins.cs" />
7475
<Compile Include="BuildServers\MyGet.cs" />
7576
<Compile Include="BuildServers\TeamCity.cs" />
7677
<Compile Include="Configuration\BranchConfig.cs" />

0 commit comments

Comments
 (0)