From f28807e615e9f06aec8a33c87780374e0c1f6fb8 Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Wed, 6 Apr 2022 14:17:38 +1000 Subject: [PATCH 1/9] Created initial version of build agent for BitBucket Pipelines --- .../BuildAgents/BitBucketPipelinesTests.cs | 102 ++++++++++++++++++ .../BuildAgents/BitBucketPipelines.cs | 38 +++++++ 2 files changed, 140 insertions(+) create mode 100644 src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs create mode 100644 src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs diff --git a/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs b/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs new file mode 100644 index 0000000000..4b63d890c0 --- /dev/null +++ b/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs @@ -0,0 +1,102 @@ +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 BitBucketPipelinesTests : TestBase +{ + private IEnvironment environment; + private BitBucketPipelines buildServer; + + [SetUp] + public void SetEnvironmentVariableForTest() + { + var sp = ConfigureServices(services => services.AddSingleton()); + this.environment = sp.GetRequiredService(); + this.buildServer = sp.GetRequiredService(); + + this.environment.SetEnvironmentVariable("BITBUCKET_WORKSPACE", "MyWorkspace"); + } + + [Test] + public void CalculateVersionOnMainBranch() + { + // Arrange + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/main"); + + var vars = new TestableVersionVariables(fullSemVer: "1.2.3"); + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); + + vsVersion.ShouldBe("1.2.3"); + } + + [Test] + public void CalculateVersionOnDevelopBranch() + { + // Arrange + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/develop"); + + var vars = new TestableVersionVariables(fullSemVer: "1.2.3-unstable.4"); + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); + + vsVersion.ShouldBe("1.2.3-unstable.4"); + } + + [Test] + public void CalculateVersionOnFeatureBranch() + { + // Arrange + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/feature/my-work"); + + var vars = new TestableVersionVariables(fullSemVer: "1.2.3-beta.4"); + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); + + vsVersion.ShouldBe("1.2.3-beta.4"); + } + + [Test] + public void GetCurrentBranchShouldHandleBranches() + { + // Arrange + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/feature/my-work"); + + // Act + var result = this.buildServer.GetCurrentBranch(false); + + // Assert + result.ShouldBe($"refs/heads/feature/my-work"); + } + + [Test] + public void GetCurrentBranchShouldHandleTags() + { + // Arrange + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", null); + this.environment.SetEnvironmentVariable("BITBUCKET_TAG", "refs/heads/tags/1.2.3"); + + // Act + var result = this.buildServer.GetCurrentBranch(false); + + // Assert + result.ShouldBeNull(); + } + + [Test] + public void GetCurrentBranchShouldHandlePullRequests() + { + // Arrange + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", null); + this.environment.SetEnvironmentVariable("BITBUCKET_TAG", null); + this.environment.SetEnvironmentVariable("BITBUCKET_PR_ID", "refs/pull/1/merge"); + + // Act + var result = this.buildServer.GetCurrentBranch(false); + + // Assert + result.ShouldBeNull(); + } +} diff --git a/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs b/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs new file mode 100644 index 0000000000..cc2249489a --- /dev/null +++ b/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs @@ -0,0 +1,38 @@ +using GitVersion.Logging; +using GitVersion.OutputVariables; + +namespace GitVersion.BuildAgents; + +public class BitBucketPipelines : BuildAgentBase +{ + public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) + { + } + + protected override string EnvironmentVariable => "BITBUCKET_WORKSPACE"; + + public override string? GenerateSetVersionMessage(VersionVariables variables) => variables.FullSemVer; + + public override string[] GenerateSetParameterMessage(string name, string value) => new[] + { + $"GITVERSION_{name.ToUpperInvariant()}={value}" + }; + + public override string? GetCurrentBranch(bool usingDynamicRepos) + { + var branchName = EvaluateEnvironmentVariable("BITBUCKET_BRANCH"); + if (branchName != null && branchName.StartsWith("refs/heads/")) + { + return branchName; + } + + return null; + } + + private string? EvaluateEnvironmentVariable(string variableName) + { + var branchName = Environment.GetEnvironmentVariable(variableName); + Log.Info("Evaluating environment variable {0} : {1}", variableName, branchName!); + return branchName; + } +} From e15413fdd2affc3859b13d6d556e59ec4eb6f4f4 Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Wed, 6 Apr 2022 16:12:31 +1000 Subject: [PATCH 2/9] Build agent now writes properties to file as environment variable exports --- .../BuildAgents/BitBucketPipelinesTests.cs | 93 ++++++++++++++++--- .../BuildAgents/BitBucketPipelines.cs | 32 ++++++- 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs b/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs index 4b63d890c0..71bc0160be 100644 --- a/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs +++ b/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs @@ -1,5 +1,7 @@ using GitVersion.BuildAgents; using GitVersion.Core.Tests.Helpers; +using GitVersion.Helpers; +using GitVersion.VersionCalculation; using Microsoft.Extensions.DependencyInjection; using NUnit.Framework; using Shouldly; @@ -11,22 +13,37 @@ public class BitBucketPipelinesTests : TestBase { private IEnvironment environment; private BitBucketPipelines buildServer; + private IServiceProvider sp; [SetUp] public void SetEnvironmentVariableForTest() { - var sp = ConfigureServices(services => services.AddSingleton()); + this.sp = ConfigureServices(services => services.AddSingleton()); this.environment = sp.GetRequiredService(); this.buildServer = sp.GetRequiredService(); - this.environment.SetEnvironmentVariable("BITBUCKET_WORKSPACE", "MyWorkspace"); + this.environment.SetEnvironmentVariable(BitBucketPipelines.EnvironmentVariableName, "MyWorkspace"); + } + + + [Test] + public void CanNotApplyToCurrentContextWhenEnvironmentVariableNotSet() + { + // Arrange + this.environment.SetEnvironmentVariable(BitBucketPipelines.EnvironmentVariableName, ""); + + // Act + var result = this.buildServer.CanApplyToCurrentContext(); + + // Assert + result.ShouldBeFalse(); } [Test] public void CalculateVersionOnMainBranch() { // Arrange - this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/main"); + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/main"); var vars = new TestableVersionVariables(fullSemVer: "1.2.3"); var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); @@ -38,7 +55,7 @@ public void CalculateVersionOnMainBranch() public void CalculateVersionOnDevelopBranch() { // Arrange - this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/develop"); + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/develop"); var vars = new TestableVersionVariables(fullSemVer: "1.2.3-unstable.4"); var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); @@ -50,7 +67,7 @@ public void CalculateVersionOnDevelopBranch() public void CalculateVersionOnFeatureBranch() { // Arrange - this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/feature/my-work"); + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/feature/my-work"); var vars = new TestableVersionVariables(fullSemVer: "1.2.3-beta.4"); var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); @@ -62,7 +79,7 @@ public void CalculateVersionOnFeatureBranch() public void GetCurrentBranchShouldHandleBranches() { // Arrange - this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/feature/my-work"); + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/feature/my-work"); // Act var result = this.buildServer.GetCurrentBranch(false); @@ -75,8 +92,8 @@ public void GetCurrentBranchShouldHandleBranches() public void GetCurrentBranchShouldHandleTags() { // Arrange - this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", null); - this.environment.SetEnvironmentVariable("BITBUCKET_TAG", "refs/heads/tags/1.2.3"); + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, null); + this.environment.SetEnvironmentVariable(BitBucketPipelines.TagEnvironmentVariableName, "refs/heads/tags/1.2.3"); // Act var result = this.buildServer.GetCurrentBranch(false); @@ -89,9 +106,9 @@ public void GetCurrentBranchShouldHandleTags() public void GetCurrentBranchShouldHandlePullRequests() { // Arrange - this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", null); - this.environment.SetEnvironmentVariable("BITBUCKET_TAG", null); - this.environment.SetEnvironmentVariable("BITBUCKET_PR_ID", "refs/pull/1/merge"); + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, null); + this.environment.SetEnvironmentVariable(BitBucketPipelines.TagEnvironmentVariableName, null); + this.environment.SetEnvironmentVariable(BitBucketPipelines.PullRequestEnvironmentVariableName, "refs/pull/1/merge"); // Act var result = this.buildServer.GetCurrentBranch(false); @@ -99,4 +116,58 @@ public void GetCurrentBranchShouldHandlePullRequests() // Assert result.ShouldBeNull(); } + + + [Test] + public void WriteAllVariablesToTheTextWriter() + { + var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + assemblyLocation.ShouldNotBeNull(); + var f = PathHelper.Combine(assemblyLocation, "gitversion.env"); + + try + { + AssertVariablesAreWrittenToFile(f); + } + finally + { + File.Delete(f); + } + } + + private void AssertVariablesAreWrittenToFile(string file) + { + var writes = new List(); + var semanticVersion = new SemanticVersion + { + Major = 1, + Minor = 2, + Patch = 3, + PreReleaseTag = "beta1", + BuildMetaData = "5" + }; + + semanticVersion.BuildMetaData.CommitDate = new DateTimeOffset(2022, 4, 6, 16, 10, 59, TimeSpan.FromHours(10)); + semanticVersion.BuildMetaData.Sha = "f28807e615e9f06aec8a33c87780374e0c1f6fb8"; + + var config = new TestEffectiveConfiguration(); + var variableProvider = this.sp.GetRequiredService(); + + var variables = variableProvider.GetVariablesFor(semanticVersion, config, false); + + this.buildServer.WithPropertyFile(file); + + this.buildServer.WriteIntegration(writes.Add, variables); + + writes[1].ShouldBe("1.2.3-beta.1+5"); + + File.Exists(file).ShouldBe(true); + + var props = File.ReadAllText(file); + + props.ShouldContain("export GITVERSION_MAJOR=1"); + props.ShouldContain("export GITVERSION_MINOR=2"); + props.ShouldContain("export GITVERSION_SHA=f28807e615e9f06aec8a33c87780374e0c1f6fb8"); + props.ShouldContain("export GITVERSION_COMMITDATE=2022-04-06"); + } } diff --git a/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs b/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs index cc2249489a..a732e263f0 100644 --- a/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs +++ b/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs @@ -5,14 +5,20 @@ namespace GitVersion.BuildAgents; public class BitBucketPipelines : BuildAgentBase { - public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) - { - } + public const string EnvironmentVariableName = "BITBUCKET_WORKSPACE"; + public const string BranchEnvironmentVariableName = "BITBUCKET_BRANCH"; + public const string TagEnvironmentVariableName = "BITBUCKET_TAG"; + public const string PullRequestEnvironmentVariableName = "BITBUCKET_PR_ID"; + private string? file; + + public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) => WithPropertyFile("gitversion.env"); - protected override string EnvironmentVariable => "BITBUCKET_WORKSPACE"; + protected override string EnvironmentVariable => EnvironmentVariableName; public override string? GenerateSetVersionMessage(VersionVariables variables) => variables.FullSemVer; + public void WithPropertyFile(string propertiesFileName) => this.file = propertiesFileName; + public override string[] GenerateSetParameterMessage(string name, string value) => new[] { $"GITVERSION_{name.ToUpperInvariant()}={value}" @@ -20,7 +26,7 @@ public override string[] GenerateSetParameterMessage(string name, string value) public override string? GetCurrentBranch(bool usingDynamicRepos) { - var branchName = EvaluateEnvironmentVariable("BITBUCKET_BRANCH"); + var branchName = EvaluateEnvironmentVariable(BranchEnvironmentVariableName); if (branchName != null && branchName.StartsWith("refs/heads/")) { return branchName; @@ -29,6 +35,22 @@ public override string[] GenerateSetParameterMessage(string name, string value) return null; } + public override void WriteIntegration(Action writer, VersionVariables variables, bool updateBuildNumber = true) + { + if (this.file is null) + return; + + base.WriteIntegration(writer, variables, updateBuildNumber); + writer($"Outputting variables to '{this.file}' ... "); + + var exports = variables + .Select(variable => $"export GITVERSION_{variable.Key.ToUpperInvariant()}={variable.Value}") + .ToList(); + + File.WriteAllLines(this.file, exports); + } + + private string? EvaluateEnvironmentVariable(string variableName) { var branchName = Environment.GetEnvironmentVariable(variableName); From 96a3546b106b8d3c2057e066e3472ac412aa9ed1 Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Wed, 6 Apr 2022 18:34:29 +1000 Subject: [PATCH 3/9] Added some extra messaging to the build output to help get things set up straight --- .../BuildAgents/BitBucketPipelines.cs | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs b/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs index a732e263f0..ba713f7474 100644 --- a/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs +++ b/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs @@ -1,4 +1,4 @@ -using GitVersion.Logging; +using GitVersion.Logging; using GitVersion.OutputVariables; namespace GitVersion.BuildAgents; @@ -24,17 +24,6 @@ public override string[] GenerateSetParameterMessage(string name, string value) $"GITVERSION_{name.ToUpperInvariant()}={value}" }; - public override string? GetCurrentBranch(bool usingDynamicRepos) - { - var branchName = EvaluateEnvironmentVariable(BranchEnvironmentVariableName); - if (branchName != null && branchName.StartsWith("refs/heads/")) - { - return branchName; - } - - return null; - } - public override void WriteIntegration(Action writer, VersionVariables variables, bool updateBuildNumber = true) { if (this.file is null) @@ -42,6 +31,12 @@ public override void WriteIntegration(Action writer, VersionVariables v base.WriteIntegration(writer, variables, updateBuildNumber); writer($"Outputting variables to '{this.file}' ... "); + writer("To import the file into your build environment, add the following line to your build step:"); + writer($" - source {this.file}"); + writer(""); + writer("To reuse the file across build steps, add the file as a build artifact:"); + writer(" artifacts:"); + writer($" - {this.file}"); var exports = variables .Select(variable => $"export GITVERSION_{variable.Key.ToUpperInvariant()}={variable.Value}") @@ -50,6 +45,16 @@ public override void WriteIntegration(Action writer, VersionVariables v File.WriteAllLines(this.file, exports); } + public override string? GetCurrentBranch(bool usingDynamicRepos) + { + var branchName = EvaluateEnvironmentVariable(BranchEnvironmentVariableName); + if (branchName != null && branchName.StartsWith("refs/heads/")) + { + return branchName; + } + + return null; + } private string? EvaluateEnvironmentVariable(string variableName) { From 624e30768377b8616081b2ea8ea9cc4e4fb3fcb4 Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Thu, 7 Apr 2022 08:44:01 +1000 Subject: [PATCH 4/9] Added tests for pull requests --- .../PullRequestInBuildAgentTest.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs b/src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs index cf9e7427e3..7e12374d66 100644 --- a/src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs +++ b/src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs @@ -128,6 +128,19 @@ public async Task VerifyTravisCIPullRequest(string pullRequestRef) await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); } + + [TestCaseSource(nameof(PrMergeRefs))] + public async Task VerifyBitBucketPipelinesPullRequest(string pullRequestRef) + { + + var env = new Dictionary + { + { BitBucketPipelines.EnvironmentVariableName, "MyWorkspace" }, + { BitBucketPipelines.PullRequestEnvironmentVariableName, pullRequestRef } + }; + await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); + } + private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pullRequestRef, Dictionary env) { using var fixture = new EmptyRepositoryFixture(); From 00261df3d9891e7523b9ff4ae1669afeda955d05 Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Fri, 8 Apr 2022 11:22:59 +1000 Subject: [PATCH 5/9] Added documentation for the BitBucket Pipelines integration --- .../build-servers/bitbucket-pipelines.md | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 docs/input/docs/reference/build-servers/bitbucket-pipelines.md diff --git a/docs/input/docs/reference/build-servers/bitbucket-pipelines.md b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md new file mode 100644 index 0000000000..8f4b88488b --- /dev/null +++ b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md @@ -0,0 +1,78 @@ +--- +Order: 35 +Title: BitBucket Pipelines +Description: Details on the Atlassian BitBucket Pipelines support in GitVersion +RedirectFrom: docs/build-server-support/build-server/bitbucket-pipelines +--- + +## Basic Usage + +To use GitVersion with Atlassian BitBucket Pipelines, you will need to install and run the GitVersion CLI tool +in your build step. + +## Executing GitVersion + +### Using the GitVersion CLI tool + +An example pipeline is shown below: + +```yml +image: mcr.microsoft.com/dotnet/sdk:6.0 + +clone: + depth: full + +pipelines: + default: + - step: + name: Version and build + script: + - export PATH="$PATH:/root/.dotnet/tools" + - dotnet tool install --global GitVersion.Tool --version 5.* + - dotnet-gitversion /buildserver + - source gitversion.env + - echo Building with semver $GITVERSION_FULLSEMVER + - dotnet build +``` + +:::{.alert .alert-danger} +**Important** + +You must set the `clone:depth` setting as shown above; without it, BitBucket Pipelines will perform a shallow clone, which will +cause GitVersion will display an error message. +::: + +When the action `dotnet-gitversion /buildserver` is executed, it will detect that it is running in BitBucket Pipelines by the presence of +the `BITBUCKET_WORKSPACE` environment variable, which is set by the BitBucket Pipelines engine. It will generate a text file named `gitversion.env` +which contains all the output of the GitVersion tool, exported as individual environment variables prefixed with `GITVERSION_`. +These environment variables can then be imported back into the build step using the `source gitversion.env` action. + +If you want to share the text file across multiple build steps, then you will need to save it as an artifact. A more complex example pipeline +is shown below: + +```yml +image: mcr.microsoft.com/dotnet/sdk:6.0 + +clone: + depth: full + +pipelines: + default: + - step: + name: Version + script: + - export PATH="$PATH:/root/.dotnet/tools" + - dotnet tool install --global GitVersion.Tool --version 5.* + - dotnet-gitversion /buildserver + artifacts: + - gitversion.env + - step: + name: Build + script: + - source gitversion.env + - echo Building with semver $GITVERSION_FULLSEMVER + - dotnet build +``` + +[Variables and Secrets](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/) +[Clone Options](https://bitbucket.org/blog/support-for-more-clone-options-at-the-step-level) \ No newline at end of file From b6ccb39c2b6c1fdc4c47a98dd63f9eadf32f0963 Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Fri, 8 Apr 2022 11:22:59 +1000 Subject: [PATCH 6/9] Added documentation for the BitBucket Pipelines integration --- .../build-servers/bitbucket-pipelines.md | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 docs/input/docs/reference/build-servers/bitbucket-pipelines.md diff --git a/docs/input/docs/reference/build-servers/bitbucket-pipelines.md b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md new file mode 100644 index 0000000000..2e69ac3a14 --- /dev/null +++ b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md @@ -0,0 +1,78 @@ +--- +Order: 35 +Title: BitBucket Pipelines +Description: Details on the Atlassian BitBucket Pipelines support in GitVersion +RedirectFrom: docs/build-server-support/build-server/bitbucket-pipelines +--- + +## Basic Usage + +To use GitVersion with Atlassian BitBucket Pipelines, you will need to install and run the GitVersion CLI tool +in your build step. + +## Executing GitVersion + +### Using the GitVersion CLI tool + +An example pipeline is shown below: + +```yml +image: mcr.microsoft.com/dotnet/sdk:6.0 + +clone: + depth: full + +pipelines: + default: + - step: + name: Version and build + script: + - export PATH="$PATH:/root/.dotnet/tools" + - dotnet tool install --global GitVersion.Tool --version 5.* + - dotnet-gitversion /buildserver + - source gitversion.env + - echo Building with semver $GITVERSION_FULLSEMVER + - dotnet build +``` + +:::{.alert .alert-danger} +**Important** + +You must set the `clone:depth` setting as shown above; without it, BitBucket Pipelines will perform a shallow clone, which will +cause GitVersion will display an error message. +::: + +When the action `dotnet-gitversion /buildserver` is executed, it will detect that it is running in BitBucket Pipelines by the presence of +the `BITBUCKET_WORKSPACE` environment variable, which is set by the BitBucket Pipelines engine. It will generate a text file named `gitversion.env` +which contains all the output of the GitVersion tool, exported as individual environment variables prefixed with `GITVERSION_`. +These environment variables can then be imported back into the build step using the `source gitversion.env` action. + +If you want to share the text file across multiple build steps, then you will need to save it as an artifact. A more complex example pipeline +is shown below: + +```yml +image: mcr.microsoft.com/dotnet/sdk:6.0 + +clone: + depth: full + +pipelines: + default: + - step: + name: Version + script: + - export PATH="$PATH:/root/.dotnet/tools" + - dotnet tool install --global GitVersion.Tool --version 5.* + - dotnet-gitversion /buildserver + artifacts: + - gitversion.env + - step: + name: Build + script: + - source gitversion.env + - echo Building with semver $GITVERSION_FULLSEMVER + - dotnet build +``` + +[Variables and Secrets](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/) +[Clone Options](https://bitbucket.org/blog/support-for-more-clone-options-at-the-step-level) From de90e2e5a3c5b14e038da7844b438b776c3fd68d Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Wed, 13 Apr 2022 07:37:03 +1000 Subject: [PATCH 7/9] Update docs/input/docs/reference/build-servers/bitbucket-pipelines.md Co-authored-by: Gary Ewan Park --- docs/input/docs/reference/build-servers/bitbucket-pipelines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/input/docs/reference/build-servers/bitbucket-pipelines.md b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md index 3b4cf3fc1f..1f4e8084c5 100644 --- a/docs/input/docs/reference/build-servers/bitbucket-pipelines.md +++ b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md @@ -38,7 +38,7 @@ pipelines: **Important** You must set the `clone:depth` setting as shown above; without it, BitBucket Pipelines will perform a shallow clone, which will -cause GitVersion will display an error message. +cause GitVersion to display an error message. ::: When the action `dotnet-gitversion /buildserver` is executed, it will detect that it is running in BitBucket Pipelines by the presence of From adcfd367de082c0cac9707f352a931e8c04cf4fa Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Wed, 13 Apr 2022 07:40:34 +1000 Subject: [PATCH 8/9] Renamed output file from gitversion.env to gitversion.properties, to be more in line with other build agents --- .../reference/build-servers/bitbucket-pipelines.md | 10 +++++----- .../BuildAgents/BitBucketPipelinesTests.cs | 2 +- src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/input/docs/reference/build-servers/bitbucket-pipelines.md b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md index 3b4cf3fc1f..f3299447ee 100644 --- a/docs/input/docs/reference/build-servers/bitbucket-pipelines.md +++ b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md @@ -29,7 +29,7 @@ pipelines: - export PATH="$PATH:/root/.dotnet/tools" - dotnet tool install --global GitVersion.Tool --version 5.* - dotnet-gitversion /buildserver - - source gitversion.env + - source gitversion.properties - echo Building with semver $GITVERSION_FULLSEMVER - dotnet build ``` @@ -42,9 +42,9 @@ cause GitVersion will display an error message. ::: When the action `dotnet-gitversion /buildserver` is executed, it will detect that it is running in BitBucket Pipelines by the presence of -the `BITBUCKET_WORKSPACE` environment variable, which is set by the BitBucket Pipelines engine. It will generate a text file named `gitversion.env` +the `BITBUCKET_WORKSPACE` environment variable, which is set by the BitBucket Pipelines engine. It will generate a text file named `gitversion.properties` which contains all the output of the GitVersion tool, exported as individual environment variables prefixed with `GITVERSION_`. -These environment variables can then be imported back into the build step using the `source gitversion.env` action. +These environment variables can then be imported back into the build step using the `source gitversion.properties` action. If you want to share the text file across multiple build steps, then you will need to save it as an artifact. A more complex example pipeline is shown below: @@ -64,11 +64,11 @@ pipelines: - dotnet tool install --global GitVersion.Tool --version 5.* - dotnet-gitversion /buildserver artifacts: - - gitversion.env + - gitversion.properties - step: name: Build script: - - source gitversion.env + - source gitversion.properties - echo Building with semver $GITVERSION_FULLSEMVER - dotnet build ``` diff --git a/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs b/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs index 71bc0160be..ecd5218fa0 100644 --- a/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs +++ b/src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs @@ -123,7 +123,7 @@ public void WriteAllVariablesToTheTextWriter() { var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); assemblyLocation.ShouldNotBeNull(); - var f = PathHelper.Combine(assemblyLocation, "gitversion.env"); + var f = PathHelper.Combine(assemblyLocation, "gitversion.properties"); try { diff --git a/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs b/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs index ba713f7474..ae58d7f1fc 100644 --- a/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs +++ b/src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs @@ -11,7 +11,7 @@ public class BitBucketPipelines : BuildAgentBase public const string PullRequestEnvironmentVariableName = "BITBUCKET_PR_ID"; private string? file; - public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) => WithPropertyFile("gitversion.env"); + public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) => WithPropertyFile("gitversion.properties"); protected override string EnvironmentVariable => EnvironmentVariableName; From ae3622428202ea99237ef1c8aeeddba881a6917d Mon Sep 17 00:00:00 2001 From: David Keaveny Date: Wed, 13 Apr 2022 08:26:21 +1000 Subject: [PATCH 9/9] Added LF to end of file to keep linter happy --- .../input/docs/reference/build-servers/bitbucket-pipelines.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/input/docs/reference/build-servers/bitbucket-pipelines.md b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md index 23e4742cbb..610031ecbe 100644 --- a/docs/input/docs/reference/build-servers/bitbucket-pipelines.md +++ b/docs/input/docs/reference/build-servers/bitbucket-pipelines.md @@ -38,7 +38,7 @@ pipelines: **Important** You must set the `clone:depth` setting as shown above; without it, BitBucket Pipelines will perform a shallow clone, which will -cause GitVersion to display an error message. +cause GitVersion will display an error message. ::: When the action `dotnet-gitversion /buildserver` is executed, it will detect that it is running in BitBucket Pipelines by the presence of @@ -74,4 +74,4 @@ pipelines: ``` [Variables and Secrets](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/) -[Clone Options](https://bitbucket.org/blog/support-for-more-clone-options-at-the-step-level) \ No newline at end of file +[Clone Options](https://bitbucket.org/blog/support-for-more-clone-options-at-the-step-level)