|  | 
| 4 | 4 | //! As this module requires additional dependencies not present during local builds, it's cfg'd | 
| 5 | 5 | //! away whenever the `build.metrics` config option is not set to `true`. | 
| 6 | 6 | 
 | 
|  | 7 | +use build_helper::ci::CiEnv; | 
|  | 8 | +use build_helper::metrics::{ | 
|  | 9 | +    CiMetadata, JsonInvocation, JsonInvocationSystemStats, JsonNode, JsonRoot, JsonStepSystemStats, | 
|  | 10 | +    Test, TestOutcome, TestSuite, TestSuiteMetadata, | 
|  | 11 | +}; | 
| 7 | 12 | use std::cell::RefCell; | 
| 8 | 13 | use std::fs::File; | 
| 9 | 14 | use std::io::BufWriter; | 
| 10 | 15 | use std::time::{Duration, Instant, SystemTime}; | 
| 11 |  | - | 
| 12 |  | -use build_helper::metrics::{ | 
| 13 |  | -    JsonInvocation, JsonInvocationSystemStats, JsonNode, JsonRoot, JsonStepSystemStats, Test, | 
| 14 |  | -    TestOutcome, TestSuite, TestSuiteMetadata, | 
| 15 |  | -}; | 
| 16 | 16 | use sysinfo::{CpuRefreshKind, RefreshKind, System}; | 
| 17 | 17 | 
 | 
| 18 | 18 | use crate::Build; | 
| @@ -217,7 +217,12 @@ impl BuildMetrics { | 
| 217 | 217 |             children: steps.into_iter().map(|step| self.prepare_json_step(step)).collect(), | 
| 218 | 218 |         }); | 
| 219 | 219 | 
 | 
| 220 |  | -        let json = JsonRoot { format_version: CURRENT_FORMAT_VERSION, system_stats, invocations }; | 
|  | 220 | +        let json = JsonRoot { | 
|  | 221 | +            format_version: CURRENT_FORMAT_VERSION, | 
|  | 222 | +            system_stats, | 
|  | 223 | +            invocations, | 
|  | 224 | +            ci_metadata: get_ci_metadata(CiEnv::current()), | 
|  | 225 | +        }; | 
| 221 | 226 | 
 | 
| 222 | 227 |         t!(std::fs::create_dir_all(dest.parent().unwrap())); | 
| 223 | 228 |         let mut file = BufWriter::new(t!(File::create(&dest))); | 
| @@ -245,6 +250,21 @@ impl BuildMetrics { | 
| 245 | 250 |     } | 
| 246 | 251 | } | 
| 247 | 252 | 
 | 
|  | 253 | +fn get_ci_metadata(ci_env: CiEnv) -> Option<CiMetadata> { | 
|  | 254 | +    if ci_env != CiEnv::GitHubActions { | 
|  | 255 | +        return None; | 
|  | 256 | +    } | 
|  | 257 | +    let Some(workflow_run_id) = | 
|  | 258 | +        std::env::var("GITHUB_WORKFLOW_RUN_ID").ok().and_then(|id| id.parse::<u64>().ok()) | 
|  | 259 | +    else { | 
|  | 260 | +        return None; | 
|  | 261 | +    }; | 
|  | 262 | +    let Ok(repository) = std::env::var("GITHUB_REPOSITORY") else { | 
|  | 263 | +        return None; | 
|  | 264 | +    }; | 
|  | 265 | +    Some(CiMetadata { workflow_run_id, repository }) | 
|  | 266 | +} | 
|  | 267 | + | 
| 248 | 268 | struct MetricsState { | 
| 249 | 269 |     finished_steps: Vec<StepMetrics>, | 
| 250 | 270 |     running_steps: Vec<StepMetrics>, | 
|  | 
0 commit comments