is_ci/
lib.rs

1use std::sync::atomic::{AtomicBool, Ordering};
2use std::sync::Once;
3
4static INIT: Once = Once::new();
5static IS_CI: AtomicBool = AtomicBool::new(false);
6
7/// Returns true if the current environment is found to probably be a CI
8/// environment or service. That's it, that's all it does.
9#[deprecated(since = "1.1.0", note = "Use `cached` or `uncached` instead")]
10pub fn is_ci() -> bool {
11    uncached()
12}
13
14/// Returns true if the current environment is found to probably be a CI
15/// environment or service, and caches the result for future calls. If you
16/// expect the environment to change, use [uncached].
17pub fn cached() -> bool {
18    INIT.call_once(|| IS_CI.store(uncached(), Ordering::Relaxed));
19    IS_CI.load(Ordering::Relaxed)
20}
21
22/// Returns true if the current environment is found to probably be a CI
23/// environment or service. If you expect to call this multiple times without
24/// the environment changing, use [cached].
25pub fn uncached() -> bool {
26    let ci_var = std::env::var("CI");
27    ci_var == Ok("true".into())
28        || ci_var == Ok("1".into())
29        || ci_var == Ok("woodpecker".into())
30        || check("CI_NAME")
31        || check("GITHUB_ACTION")
32        || check("GITLAB_CI")
33        || check("NETLIFY")
34        || check("TRAVIS")
35        || matches!(std::env::var("NODE"), Ok(node) if node.ends_with("//heroku/node/bin/node"))
36        || check("CODEBUILD_SRC_DIR")
37        || check("BUILDER_OUTPUT")
38        || check("GITLAB_DEPLOYMENT")
39        || check("NOW_GITHUB_DEPLOYMENT")
40        || check("NOW_BUILDER")
41        || check("BITBUCKET_DEPLOYMENT")
42        || check("GERRIT_PROJECT")
43        || check("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI")
44        || check("BITRISE_IO")
45        || check("BUDDY_WORKSPACE_ID")
46        || check("BUILDKITE")
47        || check("CIRRUS_CI")
48        || check("APPVEYOR")
49        || check("CIRCLECI")
50        || check("SEMAPHORE")
51        || check("DRONE")
52        || check("DSARI")
53        || check("TDDIUM")
54        || check("STRIDER")
55        || check("TASKCLUSTER_ROOT_URL")
56        || check("JENKINS_URL")
57        || check("bamboo.buildKey")
58        || check("GO_PIPELINE_NAME")
59        || check("HUDSON_URL")
60        || check("WERCKER")
61        || check("MAGNUM")
62        || check("NEVERCODE")
63        || check("RENDER")
64        || check("SAIL_CI")
65        || check("SHIPPABLE")
66}
67
68fn check(name: &str) -> bool {
69    std::env::var(name).is_ok()
70}