From f42ed7852a13592b6e1ddaa890d3ffaa90451220 Mon Sep 17 00:00:00 2001 From: Guillaume Lagrange Date: Mon, 10 Nov 2025 13:56:18 +0100 Subject: [PATCH 1/2] fix: use GITHUB_WORKSPACE env var when computing root path --- .../github_actions/provider.rs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/run/run_environment/github_actions/provider.rs b/src/run/run_environment/github_actions/provider.rs index e8ffb968..3e3a4578 100644 --- a/src/run/run_environment/github_actions/provider.rs +++ b/src/run/run_environment/github_actions/provider.rs @@ -90,7 +90,15 @@ impl TryFrom<&Config> for GitHubActionsProvider { path.push(""); path.to_string_lossy().to_string() } - None => format!("/home/runner/work/{repository}/{repository}/"), + None => { + // Fallback to GITHUB_WORKSPACE, the default repository location when using the checkout action + // https://docs.github.com/en/actions/reference/workflows-and-actions/variables + if let Ok(github_workspace) = env::var("GITHUB_WORKSPACE") { + format!("{github_workspace}/") + } else { + format!("/home/runner/work/{repository}/{repository}/") + } + } }; Ok(Self { @@ -297,6 +305,10 @@ mod tests { ("GITHUB_HEAD_REF", Some("feat/codspeed-runner")), ("GITHUB_JOB", Some("log-env")), ("GITHUB_REF", Some("refs/pull/22/merge")), + ( + "GITHUB_WORKSPACE", + Some("/home/runner/work/adrien-python-test/adrien-python-test"), + ), ("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")), ("GITHUB_RUN_ID", Some("6957110437")), ("VERSION", Some("0.1.0")), @@ -346,6 +358,10 @@ mod tests { ("GITHUB_JOB", Some("log-env")), ("GITHUB_REF", Some("refs/pull/22/merge")), ("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")), + ( + "GITHUB_WORKSPACE", + Some("/home/runner/work/adrien-python-test/adrien-python-test"), + ), ("GITHUB_RUN_ID", Some("6957110437")), ("VERSION", Some("0.1.0")), ("GH_MATRIX", Some("null")), @@ -402,6 +418,10 @@ mod tests { ("GITHUB_HEAD_REF", Some("feat/codspeed-runner")), ("GITHUB_JOB", Some("log-env")), ("GITHUB_REF", Some("refs/pull/22/merge")), + ( + "GITHUB_WORKSPACE", + Some("/home/runner/work/adrien-python-test/adrien-python-test"), + ), ("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")), ("GITHUB_RUN_ID", Some("6957110437")), ("VERSION", Some("0.1.0")), From 528bf01a8c54c5b813306f3350a67a1c62410d32 Mon Sep 17 00:00:00 2001 From: Guillaume Lagrange Date: Mon, 10 Nov 2025 16:14:49 +0100 Subject: [PATCH 2/2] feat: make `get_commit_hash` different based on the provider This allows us to add more detailed errors for github actions. --- .../github_actions/provider.rs | 19 ++++++++++ src/run/run_environment/provider.rs | 37 +++++++++++-------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/run/run_environment/github_actions/provider.rs b/src/run/run_environment/github_actions/provider.rs index 3e3a4578..baf2ade9 100644 --- a/src/run/run_environment/github_actions/provider.rs +++ b/src/run/run_environment/github_actions/provider.rs @@ -1,3 +1,4 @@ +use git2::Repository; use lazy_static::lazy_static; use regex::Regex; use serde_json::Value; @@ -217,6 +218,24 @@ impl RunEnvironmentProvider for GitHubActionsProvider { metadata, }) } + + fn get_commit_hash(&self, repository_root_path: &str) -> Result { + let repo = Repository::open(repository_root_path).context(format!( + "Failed to open repository at path: {repository_root_path}\n\ + Make sure git is installed, and that `actions/checkout` used git to fetch the repository\n\ + If necessary, install git before running `actions/checkout`.\n\ + If you run into permission issues when running in Docker, you may need to also run \ + `git config --global --add safe.directory $GITHUB_WORKSPACE` " + ))?; + + let commit_hash = repo + .head() + .and_then(|head| head.peel_to_commit()) + .context("Failed to get HEAD commit")? + .id() + .to_string(); + Ok(commit_hash) + } } #[cfg(test)] diff --git a/src/run/run_environment/provider.rs b/src/run/run_environment/provider.rs index 2e17ddaa..6ca9773a 100644 --- a/src/run/run_environment/provider.rs +++ b/src/run/run_environment/provider.rs @@ -16,20 +16,6 @@ pub trait RunEnvironmentDetector { fn detect() -> bool; } -fn get_commit_hash(repository_root_path: &str) -> Result { - let repo = Repository::open(repository_root_path).context(format!( - "Failed to open repository at path: {repository_root_path}" - ))?; - - let commit_hash = repo - .head() - .and_then(|head| head.peel_to_commit()) - .context("Failed to get HEAD commit")? - .id() - .to_string(); - Ok(commit_hash) -} - /// `RunEnvironmentProvider` is a trait that defines the necessary methods /// for a continuous integration provider. pub trait RunEnvironmentProvider { @@ -73,7 +59,7 @@ pub trait RunEnvironmentProvider { ) -> Result { let run_environment_metadata = self.get_run_environment_metadata()?; - let commit_hash = get_commit_hash(&run_environment_metadata.repository_root_path)?; + let commit_hash = self.get_commit_hash(&run_environment_metadata.repository_root_path)?; Ok(UploadMetadata { version: Some(LATEST_UPLOAD_METADATA_VERSION), @@ -94,6 +80,25 @@ pub trait RunEnvironmentProvider { run_part: self.get_run_provider_run_part(), }) } + + /// Returns the HEAD commit hash of the repository at the given path. + fn get_commit_hash(&self, repository_root_path: &str) -> Result { + get_commit_hash_default_impl(repository_root_path) + } +} + +fn get_commit_hash_default_impl(repository_root_path: &str) -> Result { + let repo = Repository::open(repository_root_path).context(format!( + "Failed to open repository at path: {repository_root_path}" + ))?; + + let commit_hash = repo + .head() + .and_then(|head| head.peel_to_commit()) + .context("Failed to get HEAD commit")? + .id() + .to_string(); + Ok(commit_hash) } #[cfg(test)] @@ -102,7 +107,7 @@ mod tests { #[test] fn test_get_commit_hash() { - let commit_hash = get_commit_hash(env!("CARGO_MANIFEST_DIR")).unwrap(); + let commit_hash = get_commit_hash_default_impl(env!("CARGO_MANIFEST_DIR")).unwrap(); // ensure that the commit hash is correct, thus it has 40 characters assert_eq!(commit_hash.len(), 40); }