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=trueflag 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 }}...HEADGotchas
- 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
How to Set fetch-depth and Base Ref for Change DetectionConfigure actions/checkout fetch-depth and resolve the correct base ref for PR versus push so git-diff-based…
How to Add a Force Full Build Option to Incremental CIGive incremental CI an escape hatch to run the full build on demand via workflow_dispatch input or a commit m…
How to Handle Changed Files Differently for PR vs PushCompute the changed file set correctly for pull_request and push events, since each provides a different base…