Code coverage workflow (sigma67/ytmusicapi)
The Code coverage workflow from sigma67/ytmusicapi, 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 Code coverage workflow from the sigma67/ytmusicapi 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: Code coverage
on:
push:
branches:
- main
paths:
- ytmusicapi/**
- tests/**
pull_request_target:
paths:
- ytmusicapi/**
- tests/**
permissions: {}
jobs:
build:
runs-on: ubuntu-latest
# For pull_request_target events, requires maintainer approval via the
# 'coverage' environment before the job runs and secrets are exposed.
# Configure in Settings → Environments → coverage → Required reviewers.
# Enable "Prevent administrators from bypassing configured protection rules"
# to enforce this for admins too.
environment: ${{ github.event_name != 'push' && 'coverage' || '' }}
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}
persist-credentials: false
fetch-depth: 1
# v7 blocks fork PR checkouts on pull_request_target by default.
# Safe here because the 'coverage' environment gate requires maintainer approval
# before this job runs and secrets are exposed (see environment: field above).
allow-unsafe-pr-checkout: true
- name: Setup Python
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
with:
python-version: "3.10"
- name: Setup PDM
uses: pdm-project/setup-pdm@94a823180e06fcde4ad29308721954a521c96ed0 # v4.4
- name: Write OAuth credentials
env:
OAUTH_JSON: ${{ secrets.OAUTH_JSON }}
run: printf '%s\n' "$OAUTH_JSON" > tests/oauth.json
- name: Install dependencies
run: pdm install
- name: Generate coverage report
env:
HEADERS_AUTH: ${{ secrets.HEADERS_AUTH }}
TEST_CFG: ${{ secrets.TEST_CFG }}
run: |
curl -fsSL -o tests/test.mp3 https://www.kozco.com/tech/piano2-CoolEdit.mp3
cat <<< "$HEADERS_AUTH" > tests/browser.json
cat <<< "$TEST_CFG" > tests/test.cfg
pdm run pytest --junitxml=test-results.xml
pdm run coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
if: ${{ !cancelled() }}
with:
flags: unittests
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@0fa95f0e1eeaafde2c782583b36b28ad0d8c77d3 # v1.2.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: test-results.xml
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: Code coverage on: push: branches: - main paths: - ytmusicapi/** - tests/** pull_request_target: paths: - ytmusicapi/** - tests/** permissions: {} concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: build: timeout-minutes: 30 runs-on: latchkey-small # For pull_request_target events, requires maintainer approval via the # 'coverage' environment before the job runs and secrets are exposed. # Configure in Settings → Environments → coverage → Required reviewers. # Enable "Prevent administrators from bypassing configured protection rules" # to enforce this for admins too. environment: ${{ github.event_name != 'push' && 'coverage' || '' }} permissions: contents: read steps: - name: Checkout repository uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 with: ref: ${{ github.event.pull_request.head.sha || github.sha }} persist-credentials: false fetch-depth: 1 # v7 blocks fork PR checkouts on pull_request_target by default. # Safe here because the 'coverage' environment gate requires maintainer approval # before this job runs and secrets are exposed (see environment: field above). allow-unsafe-pr-checkout: true - name: Setup Python uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 with: cache: 'pip' python-version: "3.10" - name: Setup PDM uses: pdm-project/setup-pdm@94a823180e06fcde4ad29308721954a521c96ed0 # v4.4 - name: Write OAuth credentials env: OAUTH_JSON: ${{ secrets.OAUTH_JSON }} run: printf '%s\n' "$OAUTH_JSON" > tests/oauth.json - name: Install dependencies run: pdm install - name: Generate coverage report env: HEADERS_AUTH: ${{ secrets.HEADERS_AUTH }} TEST_CFG: ${{ secrets.TEST_CFG }} run: | curl -fsSL -o tests/test.mp3 https://www.kozco.com/tech/piano2-CoolEdit.mp3 cat <<< "$HEADERS_AUTH" > tests/browser.json cat <<< "$TEST_CFG" > tests/test.cfg pdm run pytest --junitxml=test-results.xml pdm run coverage xml - name: Upload coverage to Codecov uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0 if: ${{ !cancelled() }} with: flags: unittests fail_ci_if_error: true token: ${{ secrets.CODECOV_TOKEN }} - name: Upload test results to Codecov if: ${{ !cancelled() }} uses: codecov/test-results-action@0fa95f0e1eeaafde2c782583b36b28ad0d8c77d3 # v1.2.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: test-results.xml
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:
- Network fetches
This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.