ci_detective/
lib.rs

1//! Detect what CI this code is running on and extract the information it provides from env vars.
2//!
3//! Documentation of individual build environment variables is from the appropriate
4//! documentation and is copyright the provider under the original license
5
6#![warn(warnings)]
7#![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)]
8#![deny(bad_style, missing_docs, unsafe_code, unused)]
9#![warn(unreachable_pub)]
10#![cfg_attr(feature = "nightly", feature(non_exhaustive))]
11
12/// Grab the configuration from whatever CI you're on.
13#[derive(Clone, Debug)]
14#[cfg_attr(feature = "nightly", non_exhaustive)]
15pub enum CI {
16    /// Jenkins CI
17    Jenkins(Jenkins),
18    /// Travis CI
19    Travis(Travis),
20    /// Docker
21    Docker(Docker),
22    /// Codeship CI
23    Codeship(Codeship),
24    /// Codefresh CI
25    Codefresh(Codefresh),
26    /// Circle CI
27    Circle(Circle),
28    /// Appveyor CI
29    Appveyor(Appveyor),
30    #[doc(hidden)] __NonExhaustive,
31}
32
33impl CI {
34    /// Grab the CI environment information
35    #[cfg_attr(rustfmt, rustfmt_skip)]
36    pub fn from_env() -> Option<Self> {
37        None
38            .or_else(|| Jenkins  ::from_env().map(CI::Jenkins  ))
39            .or_else(|| Travis   ::from_env().map(CI::Travis   ))
40            .or_else(|| Docker   ::from_env().map(CI::Docker   ))
41            .or_else(|| Codeship ::from_env().map(CI::Codeship ))
42            .or_else(|| Codefresh::from_env().map(CI::Codefresh))
43            .or_else(|| Circle   ::from_env().map(CI::Circle   ))
44            .or_else(|| Appveyor ::from_env().map(CI::Appveyor ))
45    }
46}
47
48fn env(var: &str) -> Option<String> {
49    let env_var = std::env::var(var).unwrap_or_default();
50    if !env_var.is_empty() {
51        Some(env_var)
52    } else {
53        None
54    }
55}
56
57/// Jenkins CI
58pub mod jenkins;
59pub use jenkins::Jenkins;
60
61/// Travis CI
62pub mod travis;
63pub use travis::Travis;
64
65/// Docker
66pub mod docker;
67pub use docker::Docker;
68
69/// Codeship CI
70pub mod codeship;
71pub use codeship::Codeship;
72
73/// Codefresh CI
74pub mod codefresh;
75pub use codefresh::Codefresh;
76
77// TeamCity CI doesn't provide environment variables by default
78// <https://github.com/codecov/codecov-bash/blob/8b76995ad4a95a61cecd4b049a448a402d91d197/codecov#L521-L547>
79
80/// Circle CI
81pub mod circle;
82pub use circle::Circle;
83
84/// Appveyor CI
85pub mod appveyor;
86pub use appveyor::Appveyor;