Skip to content
Latchkey

How to Handle the First Build When There Is No Base

When there is no valid base to diff against, the only safe choice is a full build; detecting the all-zero SHA or an unreachable base and falling back prevents skipping everything.

A new branch's first push, an orphan commit, or a shallow clone can leave you with no reachable base. Detect that case explicitly (the all-zero before SHA or a failing git cat-file) and run the full build rather than an empty diff.

Steps

  • Read the candidate base SHA for the event.
  • Check it is not the all-zero SHA and that it is reachable.
  • If not, set a full=true flag and build everything.

Workflow

.github/workflows/ci.yml
- id: base
  run: |
    BASE="${{ github.event.before }}"
    ZERO="0000000000000000000000000000000000000000"
    if [ "$BASE" = "$ZERO" ] || ! git cat-file -e "$BASE" 2>/dev/null; then
      echo "full=true" >> "$GITHUB_OUTPUT"
    else
      echo "full=false" >> "$GITHUB_OUTPUT"
      echo "sha=$BASE" >> "$GITHUB_OUTPUT"
    fi
- if: steps.base.outputs.full == 'true'
  run: npm run build:all
- if: steps.base.outputs.full == 'false'
  run: git diff --name-only ${{ steps.base.outputs.sha }}...HEAD

Gotchas

  • The all-zero SHA also appears when a branch is deleted; treat any unreachable base as a full build.
  • A shallow clone (fetch-depth: 1) makes almost every base unreachable, which is why change detection needs deeper history.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →