pr-tests workflow (ChatGPTBox-dev/chatGPTBox)
The pr-tests workflow from ChatGPTBox-dev/chatGPTBox, explained and optimized by Latchkey.
CI health: D - needs work
Point runs-on at Latchkey and get caching, run de-duplication, job timeouts, self-healing for flaky steps, and up to 58% lower cost, applied automatically.
What it does
This is the pr-tests workflow from the ChatGPTBox-dev/chatGPTBox repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
name: pr-tests
on:
pull_request:
types:
- "opened"
- "reopened"
- "synchronize"
paths:
- "src/**"
- "build.mjs"
- "tests/**"
- "package.json"
- "package-lock.json"
- ".github/workflows/scripts/**"
- ".github/workflows/pr-tests.yml"
push:
branches:
- "master"
paths:
- "src/**"
- "build.mjs"
- "tests/**"
- "package.json"
- "package-lock.json"
- ".github/workflows/scripts/**"
- ".github/workflows/pr-tests.yml"
jobs:
tests:
runs-on: ubuntu-22.04
permissions:
contents: read
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v6
with:
node-version: 22
- run: npm ci
- run: npm run test:coverage
- run: npm run lint
- run: npm run build
update_coverage_badge:
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
needs: tests
runs-on: ubuntu-22.04
permissions:
contents: write
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: actions/setup-node@v6
with:
node-version: 22
- run: npm ci
- run: npm run test:coverage
- run: node .github/workflows/scripts/update-coverage-badge.mjs
- name: Commit coverage badge
run: |
branch="${GITHUB_REF#refs/heads/}"
max_retries=3
if git diff --quiet -- badges/coverage.json; then
echo "Coverage badge unchanged"
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add badges/coverage.json
git commit -m "Update coverage badge [skip ci]"
for attempt in $(seq 1 "${max_retries}"); do
echo "Attempt ${attempt}/${max_retries}: pushing coverage badge update to ${branch}"
if git push origin "HEAD:${branch}"; then
echo "Coverage badge push succeeded"
exit 0
fi
if [ "${attempt}" -eq "${max_retries}" ]; then
echo "::warning::Failed to push coverage badge after ${max_retries} attempts due to concurrent updates. Skipping without failing CI."
exit 0
fi
echo "Push rejected. Fetching latest origin/${branch} and retrying with rebase..."
if ! git fetch origin "${branch}"; then
echo "::warning::Failed to fetch origin/${branch} while retrying coverage badge push. Skipping without failing CI."
exit 0
fi
if ! git rebase "origin/${branch}"; then
git rebase --abort || true
echo "::warning::Rebase conflict while retrying coverage badge push. Skipping without failing CI."
exit 0
fi
sleep $((attempt * 2))
done
The same workflow, on Latchkey
Estimated ~20% faster on cache hits, plus fewer wasted runs and a safer supply chain. Added and changed lines are highlighted.
name: pr-tests on: pull_request: types: - "opened" - "reopened" - "synchronize" paths: - "src/**" - "build.mjs" - "tests/**" - "package.json" - "package-lock.json" - ".github/workflows/scripts/**" - ".github/workflows/pr-tests.yml" push: branches: - "master" paths: - "src/**" - "build.mjs" - "tests/**" - "package.json" - "package-lock.json" - ".github/workflows/scripts/**" - ".github/workflows/pr-tests.yml" concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: tests: timeout-minutes: 30 runs-on: latchkey-small permissions: contents: read steps: - uses: actions/checkout@v7 - uses: actions/setup-node@v6 with: cache: 'npm' node-version: 22 - run: npm ci - run: npm run test:coverage - run: npm run lint - run: npm run build update_coverage_badge: timeout-minutes: 30 if: github.event_name == 'push' && github.ref == 'refs/heads/master' needs: tests runs-on: latchkey-small permissions: contents: write steps: - uses: actions/checkout@v7 with: fetch-depth: 0 - uses: actions/setup-node@v6 with: cache: 'npm' node-version: 22 - run: npm ci - run: npm run test:coverage - run: node .github/workflows/scripts/update-coverage-badge.mjs - name: Commit coverage badge run: | branch="${GITHUB_REF#refs/heads/}" max_retries=3 if git diff --quiet -- badges/coverage.json; then echo "Coverage badge unchanged" exit 0 fi git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" git add badges/coverage.json git commit -m "Update coverage badge [skip ci]" for attempt in $(seq 1 "${max_retries}"); do echo "Attempt ${attempt}/${max_retries}: pushing coverage badge update to ${branch}" if git push origin "HEAD:${branch}"; then echo "Coverage badge push succeeded" exit 0 fi if [ "${attempt}" -eq "${max_retries}" ]; then echo "::warning::Failed to push coverage badge after ${max_retries} attempts due to concurrent updates. Skipping without failing CI." exit 0 fi echo "Push rejected. Fetching latest origin/${branch} and retrying with rebase..." if ! git fetch origin "${branch}"; then echo "::warning::Failed to fetch origin/${branch} while retrying coverage badge push. Skipping without failing CI." exit 0 fi if ! git rebase "origin/${branch}"; then git rebase --abort || true echo "::warning::Rebase conflict while retrying coverage badge push. Skipping without failing CI." exit 0 fi sleep $((attempt * 2)) done
What changed
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Cancel superseded runs when a branch or PR gets a newer push.
- Cache dependency installs on the setup step so they are served from cache.
- Add a job timeout so a hung step cannot burn hours of runner time.
What Latchkey heals here
This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:
- Dependency installs
This workflow runs 2 jobs per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.