@@ -36,7 +36,7 @@ public class AzureDevOpsBuildServerData extends BuildServerDataProvider {
36
36
* @see <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables">Azure DevOps - Build variables</a>
37
37
*/
38
38
public static boolean isActiveServer (@ Nonnull Map <String , String > env ) {
39
- return env .containsKey ("AZURE_HTTP_USER_AGENT" );
39
+ return env .containsKey ("AZURE_HTTP_USER_AGENT" ) || env . containsKey ( "TF_BUILD" ) ;
40
40
}
41
41
42
42
@ Override
@@ -48,8 +48,37 @@ void loadBuildNumber(@Nonnull Properties properties) {
48
48
49
49
@ Override
50
50
public String getBuildBranch () {
51
- String environmentBasedBuildSourceBranchName = env .get ("BUILD_SOURCEBRANCHNAME" );
52
- log .info (String .format ("Using environment variable based branch name. BUILD_SOURCEBRANCHNAME = %s" , environmentBasedBuildSourceBranchName ));
53
- return environmentBasedBuildSourceBranchName ;
51
+ /**
52
+ * Build.SourceBranch
53
+ * The branch of the triggering repo the build was queued for. Some examples:
54
+ * - Git repo branch: refs/heads/main
55
+ * - Git repo pull request: refs/pull/1/merge
56
+ * - TFVC repo branch: $/teamproject/main
57
+ * - TFVC repo gated check-in: Gated_2016-06-06_05.20.51.4369;username@live.com
58
+ * - TFVC repo shelveset build: myshelveset;username@live.com
59
+ * - When your pipeline is triggered by a tag: refs/tags/your-tag-name
60
+ */
61
+ String environmentBasedBuildSourceBranch = env .get ("BUILD_SOURCEBRANCH" );
62
+ if (environmentBasedBuildSourceBranch != null && !environmentBasedBuildSourceBranch .isEmpty ()) {
63
+ if (environmentBasedBuildSourceBranch .startsWith (BRANCH_REF_PREFIX )) {
64
+ String branchName = environmentBasedBuildSourceBranch .substring (BRANCH_REF_PREFIX .length ());
65
+ log .info (String .format ("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)" ,
66
+ environmentBasedBuildSourceBranch , branchName ));
67
+ return branchName ;
68
+ }
69
+ if (environmentBasedBuildSourceBranch .startsWith (PULL_REQUEST_REF_PREFIX )) {
70
+ String branchName = environmentBasedBuildSourceBranch .substring (PULL_REQUEST_REF_PREFIX .length ());
71
+ log .info (String .format ("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)" ,
72
+ environmentBasedBuildSourceBranch , branchName ));
73
+ return branchName ;
74
+ }
75
+ if (environmentBasedBuildSourceBranch .startsWith (TAG_REF_PREFIX )) {
76
+ String branchName = environmentBasedBuildSourceBranch .substring (TAG_REF_PREFIX .length ());
77
+ log .info (String .format ("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)" ,
78
+ environmentBasedBuildSourceBranch , branchName ));
79
+ return branchName ;
80
+ }
81
+ }
82
+ return "" ;
54
83
}
55
84
}
0 commit comments