@@ -8,7 +8,8 @@ namespace GitVersion.BuildAgents
8
8
public sealed class CodeBuild : BuildAgentBase
9
9
{
10
10
private string file ;
11
- public const string EnvironmentVariableName = "CODEBUILD_WEBHOOK_HEAD_REF" ;
11
+ public const string WebHookEnvironmentVariableName = "CODEBUILD_WEBHOOK_HEAD_REF" ;
12
+ public const string SourceVersionEnvironmentVariableName = "CODEBUILD_SOURCE_VERSION" ;
12
13
13
14
public CodeBuild ( IEnvironment environment , ILog log ) : base ( environment , log )
14
15
{
@@ -20,7 +21,7 @@ public void WithPropertyFile(string propertiesFileName)
20
21
file = propertiesFileName ;
21
22
}
22
23
23
- protected override string EnvironmentVariable { get ; } = EnvironmentVariableName ;
24
+ protected override string EnvironmentVariable => throw new NotSupportedException ( $ "Accessing { nameof ( EnvironmentVariable ) } is not supported as { nameof ( CodeBuild ) } supports two environment variables for branch names." ) ;
24
25
25
26
public override string GenerateSetVersionMessage ( VersionVariables variables )
26
27
{
@@ -37,7 +38,15 @@ public override string[] GenerateSetParameterMessage(string name, string value)
37
38
38
39
public override string GetCurrentBranch ( bool usingDynamicRepos )
39
40
{
40
- return Environment . GetEnvironmentVariable ( EnvironmentVariableName ) ;
41
+
42
+ var currentBranch = Environment . GetEnvironmentVariable ( WebHookEnvironmentVariableName ) ;
43
+
44
+ if ( string . IsNullOrEmpty ( currentBranch ) )
45
+ {
46
+ return Environment . GetEnvironmentVariable ( SourceVersionEnvironmentVariableName ) ;
47
+ }
48
+
49
+ return currentBranch ;
41
50
}
42
51
43
52
public override void WriteIntegration ( Action < string > writer , VersionVariables variables , bool updateBuildNumber = true )
@@ -48,5 +57,8 @@ public override void WriteIntegration(Action<string> writer, VersionVariables va
48
57
}
49
58
50
59
public override bool PreventFetch ( ) => true ;
60
+
61
+ public override bool CanApplyToCurrentContext ( )
62
+ => ! string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( WebHookEnvironmentVariableName ) ) || ! string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( SourceVersionEnvironmentVariableName ) ) ;
51
63
}
52
64
}
0 commit comments