diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81fb39cdc5692..e5054a075676b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,10 @@ name: CI on: push: branches: + # CI on master only serves for caching citool builds for the `calculate_matrix` job. + # In order to use GHA cache on PR CI (and auto/try) jobs, we need to write to it + # from the default branch. + - master - auto - try - try-perf diff --git a/src/ci/citool/src/jobs.rs b/src/ci/citool/src/jobs.rs index 2884ae08ea882..81e002edb1565 100644 --- a/src/ci/citool/src/jobs.rs +++ b/src/ci/citool/src/jobs.rs @@ -161,6 +161,8 @@ pub enum RunType { TryJob { job_patterns: Option> }, /// Merge attempt workflow AutoJob, + /// Fake job only used for sharing Github Actions cache. + MasterJob, } /// Maximum number of custom try jobs that can be requested in a single @@ -210,6 +212,7 @@ fn calculate_jobs( (jobs, "try", &db.envs.try_env) } RunType::AutoJob => (db.auto_jobs.clone(), "auto", &db.envs.auto_env), + RunType::MasterJob => return Ok(vec![]), }; let jobs = substitute_github_vars(jobs.clone()) .context("Failed to substitute GitHub context variables in jobs")?; @@ -262,7 +265,7 @@ pub fn calculate_job_matrix( eprintln!("Run type: {run_type:?}"); let jobs = calculate_jobs(&run_type, &db, channel)?; - if jobs.is_empty() { + if jobs.is_empty() && !matches!(run_type, RunType::MasterJob) { return Err(anyhow::anyhow!("Computed job list is empty")); } @@ -270,6 +273,7 @@ pub fn calculate_job_matrix( RunType::PullRequest => "pr", RunType::TryJob { .. } => "try", RunType::AutoJob => "auto", + RunType::MasterJob => "master", }; eprintln!("Output"); diff --git a/src/ci/citool/src/main.rs b/src/ci/citool/src/main.rs index bb73a5ef909f2..fe1b36673a1b0 100644 --- a/src/ci/citool/src/main.rs +++ b/src/ci/citool/src/main.rs @@ -47,6 +47,7 @@ impl GitHubContext { Some(RunType::TryJob { job_patterns: patterns }) } ("push", "refs/heads/auto") => Some(RunType::AutoJob), + ("push", "refs/heads/master") => Some(RunType::MasterJob), _ => None, } } diff --git a/src/ci/citool/tests/jobs.rs b/src/ci/citool/tests/jobs.rs index fcdca899e068a..83f2fc0ed1f3e 100644 --- a/src/ci/citool/tests/jobs.rs +++ b/src/ci/citool/tests/jobs.rs @@ -45,17 +45,31 @@ fn pr_jobs() { "#); } +#[test] +fn master_jobs() { + let stdout = get_matrix("push", "commit", "refs/heads/master"); + insta::assert_snapshot!(stdout, @r#" + jobs=[] + run_type=master + "#); +} + fn get_matrix(event_name: &str, commit_msg: &str, branch_ref: &str) -> String { - let output = Command::new("cargo") - .args(["run", "-q", "calculate-job-matrix", "--jobs-file", TEST_JOBS_YML_PATH]) + let path = std::env::var("PATH"); + let mut cmd = Command::new("cargo"); + cmd.args(["run", "-q", "calculate-job-matrix", "--jobs-file", TEST_JOBS_YML_PATH]) + .env_clear() .env("GITHUB_EVENT_NAME", event_name) .env("COMMIT_MESSAGE", commit_msg) .env("GITHUB_REF", branch_ref) .env("GITHUB_RUN_ID", "123") .env("GITHUB_RUN_ATTEMPT", "1") - .stdout(Stdio::piped()) - .output() - .expect("Failed to execute command"); + .stdout(Stdio::piped()); + if let Ok(path) = path { + cmd.env("PATH", path); + } + + let output = cmd.output().expect("Failed to execute command"); let stdout = String::from_utf8(output.stdout).unwrap(); let stderr = String::from_utf8(output.stderr).unwrap();