Skip to content

Commit 65bf8c7

Browse files
committed
Add CI metadata to bootstrap metrics
This will allow us to provide links to CI workflows, jobs and summaries in the post-merge analysis report.
1 parent a37cef9 commit 65bf8c7

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ jobs:
6969
env:
7070
CI_JOB_NAME: ${{ matrix.name }}
7171
CI_JOB_DOC_URL: ${{ matrix.doc_url }}
72+
GITHUB_WORKFLOW_RUN_ID: ${{ github.run_id }}
73+
GITHUB_REPOSITORY: ${{ github.repository }}
7274
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
7375
# commit of PR sha or commit sha. `GITHUB_SHA` is not accurate for PRs.
7476
HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}

src/bootstrap/src/utils/metrics.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
//! As this module requires additional dependencies not present during local builds, it's cfg'd
55
//! away whenever the `build.metrics` config option is not set to `true`.
66
7+
use build_helper::ci::CiEnv;
8+
use build_helper::metrics::{
9+
CiMetadata, JsonInvocation, JsonInvocationSystemStats, JsonNode, JsonRoot, JsonStepSystemStats,
10+
Test, TestOutcome, TestSuite, TestSuiteMetadata,
11+
};
712
use std::cell::RefCell;
813
use std::fs::File;
914
use std::io::BufWriter;
1015
use std::time::{Duration, Instant, SystemTime};
11-
12-
use build_helper::metrics::{
13-
JsonInvocation, JsonInvocationSystemStats, JsonNode, JsonRoot, JsonStepSystemStats, Test,
14-
TestOutcome, TestSuite, TestSuiteMetadata,
15-
};
1616
use sysinfo::{CpuRefreshKind, RefreshKind, System};
1717

1818
use crate::Build;
@@ -217,7 +217,12 @@ impl BuildMetrics {
217217
children: steps.into_iter().map(|step| self.prepare_json_step(step)).collect(),
218218
});
219219

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+
};
221226

222227
t!(std::fs::create_dir_all(dest.parent().unwrap()));
223228
let mut file = BufWriter::new(t!(File::create(&dest)));
@@ -245,6 +250,21 @@ impl BuildMetrics {
245250
}
246251
}
247252

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+
248268
struct MetricsState {
249269
finished_steps: Vec<StepMetrics>,
250270
running_steps: Vec<StepMetrics>,

src/build_helper/src/metrics.rs

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ pub struct JsonRoot {
99
pub format_version: usize,
1010
pub system_stats: JsonInvocationSystemStats,
1111
pub invocations: Vec<JsonInvocation>,
12+
#[serde(default)]
13+
pub ci_metadata: Option<CiMetadata>,
14+
}
15+
16+
/// Represents metadata about bootstrap's execution in CI.
17+
#[derive(Serialize, Deserialize)]
18+
pub struct CiMetadata {
19+
/// GitHub run ID of the workflow where bootstrap was executed.
20+
/// Note that the run ID will be shared amongst all jobs executed in that workflow.
21+
pub workflow_run_id: u64,
22+
/// Full name of a GitHub repository where bootstrap was executed in CI.
23+
/// e.g. `rust-lang-ci/rust`.
24+
pub repository: String,
1225
}
1326

1427
#[derive(Serialize, Deserialize)]

src/ci/citool/src/analysis.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use std::collections::{BTreeMap, HashMap, HashSet};
12
use std::fmt::Debug;
3+
use std::time::Duration;
24

35
use build_helper::metrics::{
46
BuildStep, JsonRoot, TestOutcome, TestSuite, TestSuiteMetadata, escape_step_name,
57
format_build_steps,
68
};
7-
use std::collections::{BTreeMap, HashMap, HashSet};
8-
use std::time::Duration;
99

1010
use crate::metrics;
1111
use crate::metrics::{JobMetrics, JobName, get_test_suites};

src/ci/docker/run.sh

+2
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ docker \
355355
--env GITHUB_ACTIONS \
356356
--env GITHUB_REF \
357357
--env GITHUB_STEP_SUMMARY="/checkout/obj/${SUMMARY_FILE}" \
358+
--env GITHUB_WORKFLOW_RUN_ID \
359+
--env GITHUB_REPOSITORY \
358360
--env RUST_BACKTRACE \
359361
--env TOOLSTATE_REPO_ACCESS_TOKEN \
360362
--env TOOLSTATE_REPO \

0 commit comments

Comments
 (0)