-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#17: Avoid incomplete branch names on AzureDevOps
- Loading branch information
TheSnoozer
committed
May 4, 2023
1 parent
ad7cf6e
commit 12c2b6d
Showing
3 changed files
with
36 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ public class AzureDevOpsBuildServerData extends BuildServerDataProvider { | |
* @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> | ||
*/ | ||
public static boolean isActiveServer(@Nonnull Map<String, String> env) { | ||
return env.containsKey("AZURE_HTTP_USER_AGENT"); | ||
return env.containsKey("AZURE_HTTP_USER_AGENT") || env.containsKey("TF_BUILD"); | ||
} | ||
|
||
@Override | ||
|
@@ -48,8 +48,37 @@ void loadBuildNumber(@Nonnull Properties properties) { | |
|
||
@Override | ||
public String getBuildBranch() { | ||
String environmentBasedBuildSourceBranchName = env.get("BUILD_SOURCEBRANCHNAME"); | ||
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCHNAME = %s", environmentBasedBuildSourceBranchName)); | ||
return environmentBasedBuildSourceBranchName; | ||
/** | ||
* Build.SourceBranch | ||
* The branch of the triggering repo the build was queued for. Some examples: | ||
* - Git repo branch: refs/heads/main | ||
* - Git repo pull request: refs/pull/1/merge | ||
* - TFVC repo branch: $/teamproject/main | ||
* - TFVC repo gated check-in: Gated_2016-06-06_05.20.51.4369;[email protected] | ||
* - TFVC repo shelveset build: myshelveset;[email protected] | ||
* - When your pipeline is triggered by a tag: refs/tags/your-tag-name | ||
*/ | ||
String environmentBasedBuildSourceBranch = env.get("BUILD_SOURCEBRANCH"); | ||
if (environmentBasedBuildSourceBranch != null && !environmentBasedBuildSourceBranch.isEmpty()) { | ||
if (environmentBasedBuildSourceBranch.startsWith(BRANCH_REF_PREFIX)) { | ||
String branchName = environmentBasedBuildSourceBranch.substring(BRANCH_REF_PREFIX.length()); | ||
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)", | ||
environmentBasedBuildSourceBranch, branchName)); | ||
return branchName; | ||
} | ||
if (environmentBasedBuildSourceBranch.startsWith(PULL_REQUEST_REF_PREFIX)) { | ||
String branchName = environmentBasedBuildSourceBranch.substring(PULL_REQUEST_REF_PREFIX.length()); | ||
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)", | ||
environmentBasedBuildSourceBranch, branchName)); | ||
return branchName; | ||
} | ||
if (environmentBasedBuildSourceBranch.startsWith(TAG_REF_PREFIX)) { | ||
String branchName = environmentBasedBuildSourceBranch.substring(TAG_REF_PREFIX.length()); | ||
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)", | ||
environmentBasedBuildSourceBranch, branchName)); | ||
return branchName; | ||
} | ||
} | ||
return ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters