Skip to content

Commit debc84f

Browse files
authored
Merge pull request #2957 from aidenhaak/feature/buildkite
Add BuildKite build agent
2 parents 6fa8426 + 925ff8c commit debc84f

File tree

9 files changed

+171
-6
lines changed

9 files changed

+171
-6
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
Order: 40
3+
Title: Buildkite
4+
Description: Details on the Buildkite support in GitVersion
5+
RedirectFrom: docs/build-server-support/build-server/buildkite
6+
---
7+
8+
If you use [Buildkite][buildkite] then you will have to use GitVersion from the command line as there is currently no GitVersion Buildkite plugin.
9+
10+
## Gotchas
11+
12+
By default Buildkite calls `git fetch` with the flags `-v --prune` which can cause issues on new build agents since branches or tags might not be available locally on the build agent when GitVersion runs. This can be fixed by altering the [Buildkite agent configuration][configuration] either by:
13+
* Setting the environment variable `BUILDKITE_GIT_FETCH_FLAGS` to `-v --tags`
14+
* Setting configuration value `git-fetch-flags` to `-v --tags` in your agent configuration file
15+
16+
If you are running GitVersion in a docker container make sure to propogate the `BUILDKITE` and `BUILDKITE_BRANCH` environment variables (c.f. example below).
17+
18+
## Example
19+
20+
There are many ways to run GitVersion in a Buildkite pipeline. One way using the GitVersion docker image and using [build meta-data][meta-data] to share version info between build steps. Such a pipeline might look like the following:
21+
22+
```yaml
23+
env:
24+
BUILDKITE_GIT_FETCH_FLAGS: "-v --tags"
25+
26+
steps:
27+
- label: "Calculate version"
28+
command: buildkite-agent meta-data set "GitVersion_SemVer" $(./dotnet-gitversion -showvariable SemVer)
29+
plugins:
30+
- docker#v3.9.0:
31+
image: "gittools/gitversion"
32+
environment:
33+
- "BUILDKITE"
34+
- "BUILDKITE_BRANCH"
35+
36+
- wait
37+
38+
- label: "Use calculated version"
39+
command: echo "Calculated version is $(buildkite-agent meta-data get "GitVersion_SemVer")"
40+
```
41+
42+
Another way could be via the [Buildkite hooks][hooks]. Adding the following line to the `.buildkite/hooks/post-checkout` file:
43+
44+
```shell
45+
eval $(gitversion | jq -r 'to_entries[] | "buildkite-agent meta-data set GitVersion_\(.key) \(.value)"')
46+
```
47+
48+
Assuming your Buildkite agent has dotnet and gitversion installed and on the path, all the calculated GitVersion variables will have a corresponding meta-data key set.
49+
50+
[buildkite]: https://buildkite.com/
51+
[configuration]: https://buildkite.com/docs/agent/v3/hooks
52+
[hooks]: https://buildkite.com/docs/agent/v3/hooks
53+
[meta-data]: https://buildkite.com/docs/agent/v3/cli-meta-data

docs/input/docs/reference/build-servers/continua.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Order: 40
2+
Order: 50
33
Title: Continua CI
44
Description: Details on the Continua CI support in GitVersion
55
RedirectFrom: docs/build-server-support/build-server/continua

docs/input/docs/reference/build-servers/gitlab.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Order: 50
2+
Order: 60
33
Title: GitLab CI
44
Description: Details on the GitLab CI support in GitVersion
55
RedirectFrom: docs/build-server-support/build-server/gitlab

docs/input/docs/reference/build-servers/jenkins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Order: 60
2+
Order: 70
33
Title: Jenkins
44
Description: Details on the Jenkins support in GitVersion
55
RedirectFrom: docs/build-server-support/build-server/jenkins

docs/input/docs/reference/build-servers/myget.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Order: 70
2+
Order: 80
33
Title: MyGet
44
Description: Details on the MyGet support in GitVersion
55
RedirectFrom: docs/build-server-support/build-server/myget

docs/input/docs/reference/build-servers/octopus-deploy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Order: 80
2+
Order: 90
33
Title: Octopus Deploy
44
Description: Details on the Octopus Deploy support in GitVersion
55
RedirectFrom: docs/build-server-support/build-server/octopus-deploy

docs/input/docs/reference/build-servers/teamcity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Order: 90
2+
Order: 100
33
Title: TeamCity
44
Description: Details on the TeamCity support in GitVersion
55
RedirectFrom: docs/build-server-support/build-server/teamcity
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using GitVersion.BuildAgents;
2+
using GitVersion.Core.Tests.Helpers;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using NUnit.Framework;
5+
using Shouldly;
6+
7+
namespace GitVersion.Core.Tests.BuildAgents;
8+
9+
[TestFixture]
10+
public class BuildKiteTests : TestBase
11+
{
12+
private IEnvironment environment;
13+
private BuildKite buildServer;
14+
15+
[SetUp]
16+
public void SetUp()
17+
{
18+
var sp = ConfigureServices(services => services.AddSingleton<BuildKite>());
19+
this.environment = sp.GetService<IEnvironment>();
20+
this.buildServer = sp.GetService<BuildKite>();
21+
this.environment.SetEnvironmentVariable(BuildKite.EnvironmentVariableName, "true");
22+
}
23+
24+
[TearDown]
25+
public void TearDown() => this.environment.SetEnvironmentVariable(BuildKite.EnvironmentVariableName, null);
26+
27+
[Test]
28+
public void CanApplyToCurrentContextShouldBeTrueWhenEnvironmentVariableIsSet()
29+
{
30+
// Act
31+
var result = this.buildServer.CanApplyToCurrentContext();
32+
33+
// Assert
34+
result.ShouldBeTrue();
35+
}
36+
37+
[Test]
38+
public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet()
39+
{
40+
// Arrange
41+
this.environment.SetEnvironmentVariable(BuildKite.EnvironmentVariableName, "");
42+
43+
// Act
44+
var result = this.buildServer.CanApplyToCurrentContext();
45+
46+
// Assert
47+
result.ShouldBeFalse();
48+
}
49+
50+
[Test]
51+
public void GetCurrentBranchShouldHandleBranches()
52+
{
53+
// Arrange
54+
this.environment.SetEnvironmentVariable("BUILDKITE_BRANCH", MainBranch);
55+
56+
// Act
57+
var result = this.buildServer.GetCurrentBranch(false);
58+
59+
// Assert
60+
result.ShouldBe(MainBranch);
61+
}
62+
63+
[Test]
64+
public void GetSetParameterMessageShouldReturnEmptyArray()
65+
{
66+
// Act
67+
var result = this.buildServer.GenerateSetParameterMessage("Foo", "Bar");
68+
69+
// Assert
70+
result.ShouldBeEmpty();
71+
}
72+
73+
[Test]
74+
public void GetEmptyGenerateSetVersionMessage()
75+
{
76+
// Arrange
77+
var vars = new TestableVersionVariables("1.0.0");
78+
79+
// Act
80+
var message = this.buildServer.GenerateSetVersionMessage(vars);
81+
82+
// Assert
83+
message.ShouldBeEmpty();
84+
}
85+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using GitVersion.Logging;
2+
using GitVersion.OutputVariables;
3+
4+
namespace GitVersion.BuildAgents;
5+
6+
public class BuildKite : BuildAgentBase
7+
{
8+
public BuildKite(IEnvironment environment, ILog log) : base(environment, log)
9+
{
10+
}
11+
12+
public const string EnvironmentVariableName = "BUILDKITE";
13+
14+
protected override string EnvironmentVariable { get; } = EnvironmentVariableName;
15+
16+
public override bool CanApplyToCurrentContext() => Environment.GetEnvironmentVariable(EnvironmentVariable)?.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false;
17+
18+
public override string GenerateSetVersionMessage(VersionVariables variables) =>
19+
string.Empty; // There is no equivalent function in BuildKite.
20+
21+
public override string[] GenerateSetParameterMessage(string name, string value) =>
22+
Array.Empty<string>(); // There is no equivalent function in BuildKite.
23+
24+
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("BUILDKITE_BRANCH");
25+
26+
public override bool PreventFetch() => true;
27+
}

0 commit comments

Comments
 (0)