@@ -6,83 +6,92 @@ public static class BuildServerDetector
66{
77 static BuildServerDetector ( )
88 {
9- // Appveyor
10- // https://www.appveyor.com/docs/environment-variables/
11- // Travis
12- // https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
13- if ( string . Equals ( Environment . GetEnvironmentVariable ( "CI" ) , "true" , StringComparison . OrdinalIgnoreCase ) )
14- {
15- Detected = true ;
16- return ;
17- }
18-
9+ var variables = Environment . GetEnvironmentVariables ( ) ;
1910 // Jenkins
2011 // https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
21- if ( Environment . GetEnvironmentVariable ( "JENKINS_URL" ) != null )
22- {
23- Detected = true ;
24- return ;
25- }
12+ IsJenkins = variables . Contains ( "JENKINS_URL" ) ;
2613
2714 // GitHub Action
2815 // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
29- if ( Environment . GetEnvironmentVariable ( "GITHUB_ACTION" ) != null )
30- {
31- Detected = true ;
32- return ;
33- }
34-
35- // AzureDevops
36- // https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#agent-variables
37- // Variable name is 'Agent.Id' to detect if this is a Azure Pipelines agent.
38- // Note that variables are upper-cased and '.' is replaced with '_' on Azure Pipelines.
39- // https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#access-variables-through-the-environment
40- if ( Environment . GetEnvironmentVariable ( "AGENT_ID" ) != null )
41- {
42- Detected = true ;
43- return ;
44- }
16+ IsGithubAction = variables . Contains ( "GITHUB_ACTION" ) ;
4517
4618 // TeamCity
4719 // https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#PredefinedBuildParameters-ServerBuildProperties
48- if ( Environment . GetEnvironmentVariable ( "TEAMCITY_VERSION" ) != null )
49- {
50- Detected = true ;
51- return ;
52- }
20+ IsTeamCity = variables . Contains ( "TEAMCITY_VERSION" ) ;
5321
5422 // MyGet
5523 // https://docs.myget.org/docs/reference/build-services#Available_Environment_Variables
56- if ( string . Equals ( Environment . GetEnvironmentVariable ( "BuildRunner" ) , "MyGet" , StringComparison . OrdinalIgnoreCase ) )
57- {
58- Detected = true ;
59- return ;
60- }
24+ IsMyGet = ValueEquals ( variables , "BuildRunner" , "MyGet" ) ;
6125
6226 // GitLab
6327 // https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
64- if ( Environment . GetEnvironmentVariable ( "GITLAB_CI" ) != null )
65- {
66- Detected = true ;
67- return ;
68- }
28+ IsGitLab = variables . Contains ( "GITLAB_CI" ) ;
29+
30+ // GoDC
31+ // https://docs.gocd.org/current/faq/dev_use_current_revision_in_build.html
32+ IsGoDc = variables . Contains ( "GO_SERVER_URL" ) ;
33+
34+ // Travis
35+ // https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
36+ IsTravis = variables . Contains ( "TRAVIS_BUILD_ID" ) ;
6937
7038 // Docker
7139 // https://www.hanselman.com/blog/detecting-that-a-net-core-app-is-running-in-a-docker-container-and-skippablefacts-in-xunit
72- if ( Environment . GetEnvironmentVariable ( "DOTNET_RUNNING_IN_CONTAINER" ) == "true" )
73- {
74- Detected = true ;
75- return ;
76- }
40+ IsDocker = ValueEquals ( variables , "DOTNET_RUNNING_IN_CONTAINER" , "true" ) ;
7741
78- // GoDC
79- // https://docs.gocd.org/current/faq/dev_use_current_revision_in_build.html
80- if ( Environment . GetEnvironmentVariable ( "GO_SERVER_URL" ) != null )
42+ // AppVeyor
43+ // https://www.appveyor.com/docs/environment-variables/
44+ IsAppVeyor = variables . Contains ( "APPVEYOR" ) ;
45+
46+ // AzureDevops
47+ // https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#agent-variables
48+ // Variable name is 'Agent.Id' to detect if this is a Azure Pipelines agent.
49+ // Note that variables are upper-cased and '.' is replaced with '_' on Azure Pipelines.
50+ // https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#access-variables-through-the-environment
51+ IsAzureDevops = ValueEquals ( variables , "TF_BUILD" , "True" ) ;
52+
53+ Detected = IsTravis ||
54+ IsJenkins ||
55+ IsGithubAction ||
56+ IsAzureDevops ||
57+ IsTeamCity ||
58+ IsGitLab ||
59+ IsMyGet ||
60+ IsGoDc ||
61+ IsDocker ||
62+ IsAppVeyor ;
63+ }
64+
65+ static bool ValueEquals ( IDictionary variables , string key , string value )
66+ {
67+ var variable = variables [ key ] ;
68+ if ( variable == null )
8169 {
82- Detected = true ;
83- return ;
70+ return false ;
8471 }
72+
73+ return string . Equals ( ( string ) variable , value , StringComparison . OrdinalIgnoreCase ) ;
8574 }
8675
76+ public static bool IsAppVeyor { get ; }
77+
78+ public static bool IsTravis { get ; }
79+
80+ public static bool IsDocker { get ; }
81+
82+ public static bool IsAzureDevops { get ; }
83+
84+ public static bool IsGitLab { get ; }
85+
86+ public static bool IsGoDc { get ; }
87+
88+ public static bool IsMyGet { get ; }
89+
90+ public static bool IsTeamCity { get ; }
91+
92+ public static bool IsGithubAction { get ; }
93+
94+ public static bool IsJenkins { get ; }
95+
8796 public static bool Detected { get ; set ; }
8897}
0 commit comments