Skip to content

Commit 332fe76

Browse files
runningcodeclaude
andcommitted
feat: auto-fetch head-ref from GitHub Actions in detached HEAD state
Add `get_github_head_ref()` function to automatically detect head branch from GitHub Actions environment variables when running in PR workflows. This resolves detached HEAD scenarios by using GITHUB_HEAD_REF instead of requiring manual --head-ref specification. Resolves EME-367 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6080089 commit 332fe76

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/commands/build/upload.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::utils::fs::TempDir;
2424
use crate::utils::fs::TempFile;
2525
use crate::utils::progress::ProgressBar;
2626
use crate::utils::vcs::{
27-
self, get_github_base_ref, get_github_pr_number, get_provider_from_remote,
27+
self, get_github_base_ref, get_github_head_ref, get_github_pr_number, get_provider_from_remote,
2828
get_repo_from_remote_preserve_case, git_repo_base_ref, git_repo_base_repo_name_preserve_case,
2929
git_repo_head_ref, git_repo_remote_url,
3030
};
@@ -150,7 +150,11 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
150150
.map(String::as_str)
151151
.map(Cow::Borrowed)
152152
.or_else(|| {
153-
// Try to get the current ref from the VCS if not provided
153+
// First try GitHub Actions environment variables
154+
get_github_head_ref().map(Cow::Owned)
155+
})
156+
.or_else(|| {
157+
// Fallback to git repository introspection
154158
// Note: git_repo_head_ref will return an error for detached HEAD states,
155159
// which the error handling converts to None - this prevents sending "HEAD" as a branch name
156160
// In that case, the user will need to provide a valid branch name.

src/utils/vcs.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,21 @@ pub fn get_github_base_ref() -> Option<String> {
375375
Some(base_ref)
376376
}
377377

378+
/// Attempts to get the head branch from GitHub Actions environment variables.
379+
/// Returns the head branch name if running in a GitHub Actions pull request environment.
380+
pub fn get_github_head_ref() -> Option<String> {
381+
let event_name = std::env::var("GITHUB_EVENT_NAME").ok()?;
382+
383+
if event_name != "pull_request" {
384+
debug!("Not running in pull_request event, got: {}", event_name);
385+
return None;
386+
}
387+
388+
let head_ref = std::env::var("GITHUB_HEAD_REF").ok()?;
389+
debug!("Auto-detected head ref from GitHub Actions: {}", head_ref);
390+
Some(head_ref)
391+
}
392+
378393
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> {
379394
let mut non_git = false;
380395
for configured_repo in repos {

0 commit comments

Comments
 (0)