-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci.go
56 lines (43 loc) · 1000 Bytes
/
ci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ci
import "os"
// IsCI determines whether the current environment is within a CI/CD pipeline.
func IsCI() bool {
var isCI bool
isCI, cached := isCICached()
if cached {
return isCI
}
isCI = isCIChecks()
return isCI
}
// IsNotCI negates the result of [IsCI].
// It returns true if the current environment is not within a CI/CD pipeline.
func IsNotCI() bool {
return !IsCI()
}
func isCICached() (isCI bool, found bool) {
// Check from cache
cache.m.Lock()
isCI, found = cache.val[cacheKey]
cache.m.Unlock()
return
}
func isCIChecks() bool {
var isCI bool
for env, val := range CICDEnvVars {
envVal := os.Getenv(env)
if val == "" && envVal != "" {
// Only check that the environment variable is not an empty string
isCI = true
break
} else if val != "" && envVal == val {
// Check for an exact match of environment variable & expected value
isCI = true
break
}
}
cache.m.Lock()
cache.val[cacheKey] = isCI
cache.m.Unlock()
return isCI
}