Sub topic 7
What to Defer to Nightly Builds
This chapter assumes you can read a GitHub Actions workflow, know what unit, integration, and end-to-end tests are, and have used pull requests and branch protection. It covers the third stage of the pipeline: checks that are too slow, too broad, or too unstable to run on every change, so they run on a schedule instead.
What a nightly build is for
A nightly build runs on a timer against the current tip of the default branch. No pull request triggers it and no one waits for it. This changes what it can do:
- It can take hours, because no one is blocked on the result.
- It can use more machines and wider test matrices than a pull request could justify.
- It reports a problem after the fact, so it cannot stop a bad change from merging.
The last point sets the main design rule. Anything that must prevent a bad merge belongs earlier. A nightly check exists to catch what earlier stages cannot afford to look for, and to do it within a day instead of weeks.
Candidates for the nightly stage
A check is a good nightly candidate when it fails one or more of these tests:
- It is too slow for a pull request. A pull request budget is usually 10 to 15 minutes, and a full end-to-end suite may need an hour or more.
- It is too expensive to run per change. Examples are large device or browser matrices, load tests, and checks that rent cloud resources.
- It is too broad. Dependency audits, license scans, and full-repository static analysis look at everything, not at one diff.
- It depends on things that change without a commit. A new vulnerability advisory or a new upstream release can break a build that no one touched.
- It is too unstable for a blocking gate. A test with a 5% failure rate cannot guard merges, but it can still produce useful trends overnight.
| Check | Why it fits nightly | Typical runtime |
|---|---|---|
| Full end-to-end suite | Slow, needs a full environment | 30 to 120 minutes |
| Cross-browser or cross-OS matrix | Cost multiplies with each combination | 1 to 3 hours |
| Load and performance tests | Needs a quiet environment and many repeated runs | 30 to 90 minutes |
| Dependency vulnerability scan | Results change as advisories are published | 5 to 20 minutes |
| Fuzzing and mutation testing | Gets better with more time, has no natural end | 1 to 4 hours |
| Latest-dependency build | Detects upstream breakage with no commit of yours | 10 to 30 minutes |
What does not belong in nightly
Deferring a check has a cost: a defect found at 02:00 may have been merged up to a day earlier, along with other changes. Finding the culprit gets harder as more commits pile up. Keep these out of the nightly stage:
- Fast checks such as lint, type checks, and unit tests. They are cheap, so deferring them only delays feedback.
- Checks that guard a core behavior, where a regression would block other developers all day. Run a small smoke version on the pull request and keep the full version nightly.
- Checks that nobody owns. A nightly job with no owner turns red and stays red.
A useful pattern is to split a slow suite. Run a short critical-path subset at merge time, and run the complete suite nightly. The subset catches most breakage quickly and the full suite catches the rest.
Configuring the nightly trigger
In GitHub Actions, a nightly build uses the schedule event. Adding workflow_dispatch lets a person start the same workflow by hand, which is useful for reruns after a fix.
name: nightly
on:
schedule:
- cron: '0 2 * * *'
workflow_dispatch:
jobs:
e2e:
runs-on: ubuntu-latest
timeout-minutes: 120
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run test:e2eSeveral details in this workflow matter:
- Time zone. Cron expressions in GitHub Actions use UTC.
0 2 * * *means 02:00 UTC every day. - Default branch only. Scheduled workflows run from the workflow file on the default branch. A schedule added on a feature branch does nothing until it merges.
- Delays. Scheduled runs can start late when GitHub is busy, especially at the top of the hour. Picking an odd minute such as
17 2 * * *avoids the busiest moment. - Inactivity. On public repositories, GitHub disables scheduled workflows after 60 days without repository activity. A quiet project may find its nightly job silently switched off.
- Timeout.
timeout-minutescaps a hung job. Without it, a stuck run holds a runner until the platform limit, which is much longer than you want.
Running several variants with a matrix
Nightly is where a matrix pays off, because the extra runtime does not block anyone. The fail-fast: false setting is important here. By default, one failing matrix entry cancels the others, and you lose the information about which other combinations also fail.
name: nightly-matrix
on:
schedule:
- cron: '17 2 * * *'
workflow_dispatch:
jobs:
compat:
runs-on: ${{ matrix.os }}
timeout-minutes: 60
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm testThis runs nine jobs. Running the same matrix on every pull request would multiply cost by nine for each push. Running it nightly costs nine jobs per day regardless of how many changes merged.
Making nightly results actionable
A nightly build that nobody reads has no value. Three practices keep it useful.
Report to a person or channel. A red run in the Actions tab is easy to miss. Send failures to a chat channel or open an issue. A final job that runs only on failure can do this:
notify:
needs: [e2e]
if: failure()
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- uses: actions/github-script@v7
with:
script: |
await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `Nightly failed: ${context.runId}`,
body: `Run: ${context.serverUrl}/${context.repo}/actions/runs/${context.runId}`,
labels: ['nightly-failure']
});This job goes under the same jobs: key as e2e in the nightly workflow. If you create an issue on every failure, a multi-day outage produces many duplicates. Check for an open issue with the label first, or comment on it instead.
Assign an owner. One named person or rotation owns each night's triage. The owner decides whether a failure is a real regression, a flaky test, or an infrastructure problem. Each outcome has a different action: revert or fix, quarantine, or escalate to the platform team.
Keep a history. Record pass rate and runtime per test over time. A suite that takes 40 minutes now and 75 minutes in three months needs attention before it reaches the timeout.
Narrowing the blame window
When a nightly run fails, the suspects are all commits since the last green run. Two techniques shrink that list:
- Record the commit SHA tested in each run, and compare it with the last passing SHA. The range between them is the candidate list.
- Use
git bisecton that range, with the failing test as the check command. Automated bisection finds the first bad commit in about log2(n) steps, so 64 commits need about six runs.
Running the nightly build more often, such as twice a day, also narrows the window, at twice the cost. Choose the frequency by how expensive a late discovery is compared with the compute bill.
Common mistakes
- Using nightly as a dumping ground. Slow checks get moved there to make pull requests faster, then they are ignored. Every nightly check needs an owner and a response expectation.
- Letting red become normal. If the build is red for weeks, people stop looking. Fix, quarantine, or delete the failing check within an agreed time.
- Testing a stale target. The schedule trigger uses the default branch. It will not test a release branch unless you add a separate workflow for it there.
- No manual trigger. Without
workflow_dispatch, verifying a fix means waiting until the next night. - Forgetting the timeout. A hung nightly job can occupy a runner for hours and hide a problem.
Exercise
Your end-to-end suite takes 80 minutes and has caught three real regressions in the past quarter, each found a day or more after the merge. The team wants pull requests to finish in under 15 minutes. Where should the suite run, and what would you change to shorten the time between a bad merge and its discovery?
Show answer
Keep the full suite in the nightly workflow, since 80 minutes breaks the pull request budget. To shorten the discovery gap, extract a smoke subset of about 5 minutes covering the critical paths and run it in the merge queue or on pushes to main. The three regressions would likely be caught there. Also add failure notifications with a named owner, record the tested SHA, and consider a second daily run if late discovery is still costly. Do not make the nightly job a required check, because scheduled runs do not report on pull requests.
Exercise
A teammate adds a nightly workflow on a feature branch and says it will start running tomorrow night. Is that correct, and why?
Show answer
No. Scheduled workflows run only from the workflow file on the default branch. The schedule has no effect until the file is merged there. Until then, the teammate can test the job with a workflow_dispatch trigger, which can be run manually against the feature branch once the workflow file exists on the default branch, or temporarily add a pull_request or push trigger to try the steps.