Skip to content
Latchkey

Upload coverage to Codecov workflow (sanic-org/sanic)

The Upload coverage to Codecov workflow from sanic-org/sanic, explained and optimized by Latchkey.

B

CI health: B - good

Point runs-on at Latchkey and get job timeouts, SHA-pinned actions, self-healing for flaky steps, and up to 58% lower cost, applied automatically.

Grade your own workflow free or run it on Latchkey →
Source: sanic-org/sanic.github/workflows/coverage-upload.ymlLicense MITView source

What it does

This is the Upload coverage to Codecov workflow from the sanic-org/sanic 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

workflow (.yml)
name: Upload coverage to Codecov
on:
  workflow_run:
    workflows: ["Coverage check"]
    types:
      - completed

jobs:
  upload:
    name: Upload coverage
    runs-on: ubuntu-latest
    if: github.event.workflow_run.conclusion == 'success'

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.workflow_run.head_sha }}

      - name: Download coverage artifact
        uses: dawidd6/action-download-artifact@v6
        with:
          name: coverage-report
          run_id: ${{ github.event.workflow_run.id }}
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Get PR number
        id: pr
        run: |
          if [ "${{ github.event.workflow_run.event }}" == "pull_request" ]; then
            PR_NUMBERS=$(gh pr list --search "${{ github.event.workflow_run.head_sha }}" --state open --json number --jq '.[].number')
            PR_COUNT=$(printf "%s\n" "$PR_NUMBERS" | sed '/^$/d' | wc -l | tr -d ' ')
            if [ "$PR_COUNT" -eq 0 ]; then
              echo "Error: No open pull request found for head SHA '${{ github.event.workflow_run.head_sha }}'." >&2
              exit 1
            elif [ "$PR_COUNT" -gt 1 ]; then
              echo "Error: Multiple open pull requests ($PR_COUNT) found for head SHA '${{ github.event.workflow_run.head_sha }}':" >&2
              printf "%s\n" "$PR_NUMBERS" >&2
              exit 1
            fi
            PR_NUMBER="$PR_NUMBERS"
            echo "number=${PR_NUMBER}" >> "$GITHUB_OUTPUT"
          fi
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload to Codecov
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./coverage.xml
          fail_ci_if_error: true
          override_commit: ${{ github.event.workflow_run.head_sha }}
          override_branch: ${{ github.event.workflow_run.head_branch }}
          override_pr: ${{ steps.pr.outputs.number }}

The same workflow, on Latchkey

Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.

name: Upload coverage to Codecov
on:
  workflow_run:
    workflows: ["Coverage check"]
    types:
      - completed
 
jobs:
  upload:
    timeout-minutes: 30
    name: Upload coverage
    runs-on: latchkey-small
    if: github.event.workflow_run.conclusion == 'success'
 
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.workflow_run.head_sha }}
 
      - name: Download coverage artifact
        uses: dawidd6/action-download-artifact@v6
        with:
          name: coverage-report
          run_id: ${{ github.event.workflow_run.id }}
          github_token: ${{ secrets.GITHUB_TOKEN }}
 
      - name: Get PR number
        id: pr
        run: |
          if [ "${{ github.event.workflow_run.event }}" == "pull_request" ]; then
            PR_NUMBERS=$(gh pr list --search "${{ github.event.workflow_run.head_sha }}" --state open --json number --jq '.[].number')
            PR_COUNT=$(printf "%s\n" "$PR_NUMBERS" | sed '/^$/d' | wc -l | tr -d ' ')
            if [ "$PR_COUNT" -eq 0 ]; then
              echo "Error: No open pull request found for head SHA '${{ github.event.workflow_run.head_sha }}'." >&2
              exit 1
            elif [ "$PR_COUNT" -gt 1 ]; then
              echo "Error: Multiple open pull requests ($PR_COUNT) found for head SHA '${{ github.event.workflow_run.head_sha }}':" >&2
              printf "%s\n" "$PR_NUMBERS" >&2
              exit 1
            fi
            PR_NUMBER="$PR_NUMBERS"
            echo "number=${PR_NUMBER}" >> "$GITHUB_OUTPUT"
          fi
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 
      - name: Upload to Codecov
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./coverage.xml
          fail_ci_if_error: true
          override_commit: ${{ github.event.workflow_run.head_sha }}
          override_branch: ${{ github.event.workflow_run.head_branch }}
          override_pr: ${{ steps.pr.outputs.number }}
 

What changed

2 third-party actions are referenced by a movable tag. Pin them to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.

This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.

Actions used in this workflow