@@ -6,83 +6,101 @@ 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 = ! IsTravis &&
52+ ! IsJenkins &&
53+ ! IsGithubAction &&
54+ ! IsTeamCity &&
55+ ! IsGitLab &&
56+ ! IsMyGet &&
57+ ! IsGoDc &&
58+ ! IsDocker &&
59+ ! IsAppVeyor &&
60+ variables . Contains ( "AGENT_ID" ) ;
61+
62+ Detected = IsTravis ||
63+ IsJenkins ||
64+ IsGithubAction ||
65+ IsAzureDevops ||
66+ IsTeamCity ||
67+ IsGitLab ||
68+ IsMyGet ||
69+ IsGoDc ||
70+ IsDocker ||
71+ IsAppVeyor ;
72+ }
73+
74+ static bool ValueEquals ( IDictionary variables , string key , string value )
75+ {
76+ var variable = variables [ key ] ;
77+ if ( variable == null )
8178 {
82- Detected = true ;
83- return ;
79+ return false ;
8480 }
81+
82+ return string . Equals ( ( string ) variable , value , StringComparison . OrdinalIgnoreCase ) ;
8583 }
8684
85+ public static bool IsAppVeyor { get ; }
86+
87+ public static bool IsTravis { get ; }
88+
89+ public static bool IsDocker { get ; }
90+
91+ public static bool IsAzureDevops { get ; }
92+
93+ public static bool IsGitLab { get ; }
94+
95+ public static bool IsGoDc { get ; }
96+
97+ public static bool IsMyGet { get ; }
98+
99+ public static bool IsTeamCity { get ; }
100+
101+ public static bool IsGithubAction { get ; }
102+
103+ public static bool IsJenkins { get ; }
104+
87105 public static bool Detected { get ; set ; }
88106}
0 commit comments