diff --git a/src/executor/orchestrator.rs b/src/executor/orchestrator.rs index ca2dbdf4f..ad2c9c5f7 100644 --- a/src/executor/orchestrator.rs +++ b/src/executor/orchestrator.rs @@ -226,6 +226,10 @@ impl Orchestrator { let last_upload_result = self.upload_all(&mut completed_runs, api_client).await?; end_group!(); + if let Err(e) = self.provider.export_run_id(&last_upload_result.run_id) { + warn!("Failed to expose the run id as a step output: {e}"); + } + if self.is_local() { poll_results( api_client, diff --git a/src/run_environment/github_actions/provider.rs b/src/run_environment/github_actions/provider.rs index 4a411db2b..0e55f1670 100644 --- a/src/run_environment/github_actions/provider.rs +++ b/src/run_environment/github_actions/provider.rs @@ -7,6 +7,7 @@ use serde::Deserialize; use serde_json::Value; use simplelog::SharedLogger; use std::collections::BTreeMap; +use std::io::Write; use std::{env, fs}; use crate::api_client::{Authentication, CodSpeedAPIClient}; @@ -382,6 +383,17 @@ impl RunEnvironmentProvider for GitHubActionsProvider { Ok(()) } + + fn export_run_id(&self, run_id: &str) -> Result<()> { + let output_path = get_env_variable("GITHUB_OUTPUT")?; + let mut output_file = fs::OpenOptions::new() + .append(true) + .create(true) + .open(output_path)?; + writeln!(output_file, "run-id={run_id}")?; + + Ok(()) + } } #[cfg(test)] @@ -833,4 +845,37 @@ mod tests { }, ) } + + #[test] + fn test_export_run_id() { + let output_file = tempfile::NamedTempFile::new().unwrap(); + fs::write(output_file.path(), "previous=value\n").unwrap(); + + with_var("GITHUB_OUTPUT", Some(output_file.path()), || { + let github_actions_provider = GitHubActionsProvider { + owner: "owner".into(), + repository: "repository".into(), + ref_: "refs/head/my-branch".into(), + head_ref: Some("my-branch".into()), + base_ref: None, + sender: None, + run_id: "123789".into(), + job_name: "my_job".into(), + event: RunEvent::Push, + repository_root_path: "/home/work/my-repo".into(), + is_head_repo_fork: false, + is_repository_private: false, + oidc_config: None, + }; + + github_actions_provider + .export_run_id("66f7e2c1a3b4d5e6f7a8b9c0") + .unwrap(); + }); + + assert_eq!( + fs::read_to_string(output_file.path()).unwrap(), + "previous=value\nrun-id=66f7e2c1a3b4d5e6f7a8b9c0\n" + ); + } } diff --git a/src/run_environment/provider.rs b/src/run_environment/provider.rs index 317ac2178..8858e16fa 100644 --- a/src/run_environment/provider.rs +++ b/src/run_environment/provider.rs @@ -100,6 +100,11 @@ pub trait RunEnvironmentProvider { Ok(()) } + /// Expose the CodSpeed run id to the next steps of the CI job. + fn export_run_id(&self, _run_id: &str) -> Result<()> { + Ok(()) + } + /// Returns the metadata necessary for uploading results to CodSpeed. /// /// `orchestrator_run_part_suffix` is structured data from the orchestrator used to differentiate