aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/ci/ci.go
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
commitdd84b9d64fb98746a230cd24233ff50a562c39c9 (patch)
treeb583261ef00b3afe72ec4d6dacb31e57779a6faf /cli/internal/ci/ci.go
parent0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff)
downloadHydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz
HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip
Diffstat (limited to 'cli/internal/ci/ci.go')
-rw-r--r--cli/internal/ci/ci.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/cli/internal/ci/ci.go b/cli/internal/ci/ci.go
new file mode 100644
index 0000000..a22ad78
--- /dev/null
+++ b/cli/internal/ci/ci.go
@@ -0,0 +1,58 @@
+// Package ci is a simple utility to check if a program is being executed in common CI/CD/PaaS vendors.
+// This is a partial port of https://github.com/watson/ci-info
+package ci
+
+import "os"
+
+var isCI = os.Getenv("BUILD_ID") != "" || os.Getenv("BUILD_NUMBER") != "" || os.Getenv("CI") != "" || os.Getenv("CI_APP_ID") != "" || os.Getenv("CI_BUILD_ID") != "" || os.Getenv("CI_BUILD_NUMBER") != "" || os.Getenv("CI_NAME") != "" || os.Getenv("CONTINUOUS_INTEGRATION") != "" || os.Getenv("RUN_ID") != "" || os.Getenv("TEAMCITY_VERSION") != "" || false
+
+// IsCi returns true if the program is executing in a CI/CD environment
+func IsCi() bool {
+ return isCI
+}
+
+// Name returns the name of the CI vendor
+func Name() string {
+ return Info().Name
+}
+
+// Constant returns the name of the CI vendor as a constant
+func Constant() string {
+ return Info().Constant
+}
+
+// Info returns information about a CI vendor
+func Info() Vendor {
+ // check both the env var key and value
+ for _, env := range Vendors {
+ if env.EvalEnv != nil {
+ for name, value := range env.EvalEnv {
+ if os.Getenv(name) == value {
+ return env
+ }
+ }
+ } else {
+ // check for any of the listed env var keys, with any value
+ if env.Env.Any != nil && len(env.Env.Any) > 0 {
+ for _, envVar := range env.Env.Any {
+ if os.Getenv(envVar) != "" {
+ return env
+ }
+ }
+ // check for all of the listed env var keys, with any value
+ } else if env.Env.All != nil && len(env.Env.All) > 0 {
+ all := true
+ for _, envVar := range env.Env.All {
+ if os.Getenv(envVar) == "" {
+ all = false
+ break
+ }
+ }
+ if all {
+ return env
+ }
+ }
+ }
+ }
+ return Vendor{}
+}