linera-io/linera-protocol · error

GITHUB_PR_BRANCH is not set! This must be run from within CI

Error message

GITHUB_PR_BRANCH is not set! This must be run from within CI

What it means

In CI mode, from_env reads GITHUB_PR_BRANCH — the source branch of the pull request — and requires it to be set. The tool uses it (with the base branch) to scope workflow-run and job queries to the PR's branches, so without it the context is incomplete and construction aborts.

Source

Thrown at linera-summary/src/github.rs:127

        let (pr_commit_hash, pr_branch, base_branch, pr_number) = if is_local {
            let (commit_hash, branch_name, base) = Self::get_local_git_info()?;
            (
                commit_hash,
                branch_name,
                base,
                pr_number.ok_or_else(|| anyhow!("pr_number is None"))?,
            )
        } else {
            let pr_string = env_pr_number.map_err(|_| {
                anyhow!("GITHUB_PR_NUMBER is not set! This must be run from within CI")
            })?;
            (
                env_pr_commit_hash.map_err(|_| {
                    anyhow!("GITHUB_PR_COMMIT_HASH is not set! This must be run from within CI")
                })?,
                env_pr_branch.map_err(|_| {
                    anyhow!("GITHUB_PR_BRANCH is not set! This must be run from within CI")
                })?,
                env_base_branch.map_err(|_| {
                    anyhow!("GITHUB_BASE_BRANCH is not set! This must be run from within CI")
                })?,
                pr_string
                    .parse()
                    .map_err(|_| anyhow!("GITHUB_PR_NUMBER is not a valid number: {pr_string}"))?,
            )
        };

        Ok(Self {
            repository: GithubRepository::from_env(is_local)?,
            pr_commit_hash,
            pr_branch,
            base_branch,
            pr_number,
        })
    }

View on GitHub (pinned to 6c226ddcb3)

Solutions

  1. Export the PR head branch: GITHUB_PR_BRANCH: ${{ github.head_ref }} in the workflow env
  2. Or set it manually together with GITHUB_PR_NUMBER, GITHUB_PR_COMMIT_HASH and GITHUB_BASE_BRANCH when emulating CI
  3. Verify all four GITHUB_PR_*/GITHUB_BASE_* variables are present — they are checked as a group

Example fix

# before
env:
  GITHUB_PR_HEAD: ${{ github.head_ref }}
# after
env:
  GITHUB_PR_BRANCH: ${{ github.head_ref }}
Defensive patterns

Strategy: validation

Validate before calling

#!/usr/bin/env bash
: "${GITHUB_PR_BRANCH:?GITHUB_PR_BRANCH is required (PR head branch)}"

Try / catch

if let Err(e) = Github::new(false, None) {
    if e.to_string().contains("GITHUB_PR_BRANCH") {
        eprintln!("export GITHUB_PR_BRANCH=${{ github.head_ref }} in the workflow env");
    }
    return Err(e.into());
}

Prevention

When it happens

Trigger: A CI environment where GITHUB_PR_BRANCH was never exported: non-Actions execution, or a workflow env block that maps github.head_ref under a different name.

Common situations: Hand-written workflows exporting github.head_ref as something else; jobs spawned via workflow_dispatch; scripts borrowing CI code paths outside Actions.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of linera-io/linera-protocol@6c226ddcb3 (2026-08-22). Data as JSON: /api/errors/3991662f0ee1d5e8. Report an issue: GitHub.