Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/input/docs/reference/build-servers/buildkite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
Order: 40
Title: Buildkite
Description: Details on the Buildkite support in GitVersion
RedirectFrom: docs/build-server-support/build-server/buildkite
---

If you use [Buildkite][buildkite] then you will have to use GitVersion from the command line as there is currently no GitVersion Buildkite plugin.

## Gotchas

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:
* Setting the environment variable `BUILDKITE_GIT_FETCH_FLAGS` to `-v --tags`
* Setting configuration value `git-fetch-flags` to `-v --tags` in your agent configuration file

If you are running GitVersion in a docker container make sure to propogate the `BUILDKITE` and `BUILDKITE_BRANCH` environment variables (c.f. example below).

## Example

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:

```yaml
env:
BUILDKITE_GIT_FETCH_FLAGS: "-v --tags"

steps:
- label: "Calculate version"
command: buildkite-agent meta-data set "GitVersion_SemVer" $(./dotnet-gitversion -showvariable SemVer)
plugins:
- docker#v3.9.0:
image: "gittools/gitversion"
environment:
- "BUILDKITE"
- "BUILDKITE_BRANCH"

- wait

- label: "Use calculated version"
command: echo "Calculated version is $(buildkite-agent meta-data get "GitVersion_SemVer")"
```

Another way could be via the [Buildkite hooks][hooks]. Adding the following line to the `.buildkite/hooks/post-checkout` file:

```shell
eval $(gitversion | jq -r 'to_entries[] | "buildkite-agent meta-data set GitVersion_\(.key) \(.value)"')
```

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.

[buildkite]: https://buildkite.com/
[configuration]: https://buildkite.com/docs/agent/v3/hooks
[hooks]: https://buildkite.com/docs/agent/v3/hooks
[meta-data]: https://buildkite.com/docs/agent/v3/cli-meta-data
2 changes: 1 addition & 1 deletion docs/input/docs/reference/build-servers/continua.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Order: 40
Order: 50
Title: Continua CI
Description: Details on the Continua CI support in GitVersion
RedirectFrom: docs/build-server-support/build-server/continua
Expand Down
2 changes: 1 addition & 1 deletion docs/input/docs/reference/build-servers/gitlab.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Order: 50
Order: 60
Title: GitLab CI
Description: Details on the GitLab CI support in GitVersion
RedirectFrom: docs/build-server-support/build-server/gitlab
Expand Down
2 changes: 1 addition & 1 deletion docs/input/docs/reference/build-servers/jenkins.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Order: 60
Order: 70
Title: Jenkins
Description: Details on the Jenkins support in GitVersion
RedirectFrom: docs/build-server-support/build-server/jenkins
Expand Down
2 changes: 1 addition & 1 deletion docs/input/docs/reference/build-servers/myget.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Order: 70
Order: 80
Title: MyGet
Description: Details on the MyGet support in GitVersion
RedirectFrom: docs/build-server-support/build-server/myget
Expand Down
2 changes: 1 addition & 1 deletion docs/input/docs/reference/build-servers/octopus-deploy.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Order: 80
Order: 90
Title: Octopus Deploy
Description: Details on the Octopus Deploy support in GitVersion
RedirectFrom: docs/build-server-support/build-server/octopus-deploy
Expand Down
2 changes: 1 addition & 1 deletion docs/input/docs/reference/build-servers/teamcity.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Order: 90
Order: 100
Title: TeamCity
Description: Details on the TeamCity support in GitVersion
RedirectFrom: docs/build-server-support/build-server/teamcity
Expand Down
85 changes: 85 additions & 0 deletions src/GitVersion.Core.Tests/BuildAgents/BuildKiteTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using GitVersion.BuildAgents;
using GitVersion.Core.Tests.Helpers;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using Shouldly;

namespace GitVersion.Core.Tests.BuildAgents;

[TestFixture]
public class BuildKiteTests : TestBase
{
private IEnvironment environment;
private BuildKite buildServer;

[SetUp]
public void SetUp()
{
var sp = ConfigureServices(services => services.AddSingleton<BuildKite>());
this.environment = sp.GetService<IEnvironment>();
this.buildServer = sp.GetService<BuildKite>();
this.environment.SetEnvironmentVariable(BuildKite.EnvironmentVariableName, "true");
}

[TearDown]
public void TearDown() => this.environment.SetEnvironmentVariable(BuildKite.EnvironmentVariableName, null);

[Test]
public void CanApplyToCurrentContextShouldBeTrueWhenEnvironmentVariableIsSet()
{
// Act
var result = this.buildServer.CanApplyToCurrentContext();

// Assert
result.ShouldBeTrue();
}

[Test]
public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet()
{
// Arrange
this.environment.SetEnvironmentVariable(BuildKite.EnvironmentVariableName, "");

// Act
var result = this.buildServer.CanApplyToCurrentContext();

// Assert
result.ShouldBeFalse();
}

[Test]
public void GetCurrentBranchShouldHandleBranches()
{
// Arrange
this.environment.SetEnvironmentVariable("BUILDKITE_BRANCH", MainBranch);

// Act
var result = this.buildServer.GetCurrentBranch(false);

// Assert
result.ShouldBe(MainBranch);
}

[Test]
public void GetSetParameterMessageShouldReturnEmptyArray()
{
// Act
var result = this.buildServer.GenerateSetParameterMessage("Foo", "Bar");

// Assert
result.ShouldBeEmpty();
}

[Test]
public void GetEmptyGenerateSetVersionMessage()
{
// Arrange
var vars = new TestableVersionVariables("1.0.0");

// Act
var message = this.buildServer.GenerateSetVersionMessage(vars);

// Assert
message.ShouldBeEmpty();
}
}
27 changes: 27 additions & 0 deletions src/GitVersion.Core/BuildAgents/BuildKite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using GitVersion.Logging;
using GitVersion.OutputVariables;

namespace GitVersion.BuildAgents;

public class BuildKite : BuildAgentBase
{
public BuildKite(IEnvironment environment, ILog log) : base(environment, log)
{
}

public const string EnvironmentVariableName = "BUILDKITE";

protected override string EnvironmentVariable { get; } = EnvironmentVariableName;

public override bool CanApplyToCurrentContext() => Environment.GetEnvironmentVariable(EnvironmentVariable)?.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false;

public override string GenerateSetVersionMessage(VersionVariables variables) =>
string.Empty; // There is no equivalent function in BuildKite.

public override string[] GenerateSetParameterMessage(string name, string value) =>
Array.Empty<string>(); // There is no equivalent function in BuildKite.

public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("BUILDKITE_BRANCH");

public override bool PreventFetch() => true;
}