Status Badges

A status badge is a small image that shows one fact about a repository, such as whether the checks of main pass, or how many pull requests are open:

Checks of the newest commit on main of ucsb-cs156/proj-dining Open pull requests of ucsb-cs156/proj-dining

These two are live, for the repository ucsb-cs156/proj-dining: when something changes there, they change here too, without anyone editing this page.

In CS156 you will see badges:

  • on the index page of each project repository’s GitHub Pages site, in the sections Status of main and Status of pull requests;
  • on the overview page of all the team repositories of a quarter, such as https://ucsb-cs156-f26.github.io/ for F26;
  • in the README files of many open source projects.

How a badge works

A badge is an ordinary image, an SVG file, whose URL names a repository and the fact to show. The server that draws it reads the current state of the repository each time it is asked. That has three consequences.

  • It is live. The reader’s browser fetches the badge each time the page is opened, so a page with badges stays current without being rebuilt or published again.
  • It lags a little. Servers and browsers keep a copy of a badge for a short while before asking again: 5 minutes for GitHub’s badges, and from 30 seconds to 2 minutes for those of shields.io, depending on the badge. After a run finishes, its badge can take that long to change.
  • It needs alt text. A badge is a picture of text, which a screen reader cannot read. Because the value changes, the alt text says what the badge shows, not its value: “Checks of main”, not “passing”.

To put a badge on a page, use it as an image, and usually as a link to the page that explains it. In Markdown:

[![Checks of main](https://img.shields.io/github/check-runs/ucsb-cs156/proj-dining/main?label=main)](https://github.com/ucsb-cs156/proj-dining/commits/main)

The same in HTML:

<a href="https://github.com/ucsb-cs156/proj-dining/commits/main"><img alt="Checks of main" src="https://img.shields.io/github/check-runs/ucsb-cs156/proj-dining/main?label=main"></a>

Badges from GitHub

GitHub draws one kind of badge: the result of the last run of one workflow.

https://github.com/{owner}/{repo}/actions/workflows/{file}/badge.svg

Here {file} is the workflow’s file name in .github/workflows, such as 34-frontend-main-mutation-testing.yml. Without parameters, the badge shows the last run on the default branch (or, if there is none, the last run on any branch). Two parameters narrow it down:

  • branch=main counts only the runs on that branch;
  • event=push counts only the runs started by that event; another common one is event=pull_request.

For example, badge.svg?branch=main&event=push shows the last run for a push to main.

  • The label is the workflow’s name:, and cannot be changed.
  • When no run matches, the badge says “no status”.
  • GitHub draws these badges itself, so they do not depend on another service. For a private repository they cannot be shown on any other site.

To get the Markdown for one, open the repository’s Actions tab, choose the workflow, open the ⋯ menu next to “Filter workflow runs”, and choose Create status badge. GitHub’s documentation: Adding a workflow status badge.

Badges from shields.io

shields.io is a free service that draws badges for hundreds of sources. For GitHub, it reads the GitHub API, so it can show facts that GitHub has no badge for: the checks of any commit, the open pull requests, the last commit, commits per week, the state of one pull request, and more.

https://img.shields.io/github/{kind}/{owner}/{repo}/...

The list of shields.io badges has every kind, each with a form that builds its URL. These parameters work on all of them:

Parameter What it does Example
label The text on the left. Empty (label=) shows the value alone. label=main
color The color of the value, in place of the one shields.io picks from the value. color=orange
style flat (the default), flat-square, plastic, for-the-badge or social. style=flat-square
logo An icon on the left, by its name on Simple Icons. logo=github

shields.io also draws badges whose value does not come from GitHub:

  • A static badge, https://img.shields.io/badge/{label}-{message}-{color}, shows fixed text, such as Example of a static badge: java 25. Write a space as %20 or _, and a dash as --.
  • A dynamic JSON badge, https://img.shields.io/badge/dynamic/json?url={url}&query={JSONPath}, shows one value from a public JSON file. Its color is the same for every value.
  • An endpoint badge, https://img.shields.io/endpoint?url={url}, reads a small JSON file that gives the label, the message and the color, so the file decides the color too.

shields.io is a third party: when it is slow or down, its badges show an error or nothing, while the rest of the page loads as usual.

Reference: badges for CS156 repositories

The badges below include those on the index page of each project repository’s GitHub Pages site and on the overview page of a quarter’s team repositories, and a few alternatives. In the URLs:

  • {owner}/{repo} is the repository, such as ucsb-cs156/proj-dining;
  • {file} is a workflow’s file name;
  • {branch}, {number} and {commit} are a pull request’s branch, its number and the id of its newest commit.

Each image is live, for ucsb-cs156/proj-dining, except the one for commits behind main: that badge needs a branch that exists, so its image is a static copy.

Checks of main

Checks of the newest commit on main of proj-dining

https://img.shields.io/github/check-runs/{owner}/{repo}/main?label=main
  • From: shields.io
  • Shows: The check runs of the newest commit on main: passing, failing or pending.
  • Good to know: Only the workflows that ran for that commit count. A workflow whose paths: filter skipped the commit is left out, even if its last run failed.

Last run of one workflow on main

Last run of workflow 34 on main of proj-dining

https://github.com/{owner}/{repo}/actions/workflows/{file}/badge.svg?branch=main&event=push
  • From: GitHub
  • Shows: The last run of one workflow on main.
  • Good to know: The label is the workflow’s full name.

Last run of one workflow, with a label of your choice

Last run of workflow 34 on main of proj-dining, with a shorter label

https://img.shields.io/github/actions/workflow/status/{owner}/{repo}/{file}?branch=main&event=push&label=...
  • From: shields.io
  • Shows: The same, with a label of your choice.

Whether a pull request may be merged

Last run of the All Checks Pass workflow for a pull request of proj-dining

https://github.com/{owner}/{repo}/actions/workflows/00-all-checks-pass.yml/badge.svg?branch={branch}&event=pull_request
  • From: GitHub
  • Shows: Whether a pull request may be merged: the same rule as branch protection.
  • Good to know: Says “no status” for main, because this workflow runs only for pull requests. The image here has no branch=, so it shows the last pull request of any branch.

Checks of one commit

Checks of one commit of proj-dining

https://img.shields.io/github/check-runs/{owner}/{repo}/{commit}
  • From: shields.io
  • Shows: Every check run of one commit, such as the newest commit of a pull request.
  • Good to know: A branch name works in place of {commit}, even one with a slash in it.

State of a pull request

State of pull request 217 of proj-dining

https://img.shields.io/github/pulls/detail/state/{owner}/{repo}/{number}
  • From: shields.io
  • Shows: draft, open, merged, or rejected (closed without merging).

Commits behind main

Example of the behind main badge, a static copy

https://img.shields.io/github/commits-difference/{owner}/{repo}?base={branch}&head=main&label=behind%20main
  • From: shields.io
  • Shows: How many commits main has that the branch does not.
  • Good to know: base can also be a commit id, but a branch name keeps the badge current as the branch gets new commits.

Last change to a pull request

When pull request 217 of proj-dining last changed

https://img.shields.io/github/pulls/detail/last-update/{owner}/{repo}/{number}
  • From: shields.io
  • Shows: When the pull request last changed.

Last commit to main

Last commit to main of proj-dining

https://img.shields.io/github/last-commit/{owner}/{repo}/main
  • From: shields.io
  • Shows: When main last changed.

Open pull requests

Open pull requests of proj-dining

https://img.shields.io/github/issues-pr/{owner}/{repo}
  • From: shields.io
  • Shows: How many pull requests are open.

Commits per week

Commits per week to the default branch of proj-dining

https://img.shields.io/github/commit-activity/w/{owner}/{repo}
  • From: shields.io
  • Shows: Commits to the default branch in the last week.
  • Good to know: m and y in place of w give the last month and year.

One value from a JSON file (not used yet)

Result of the last Stryker run on main of proj-dining

https://img.shields.io/badge/dynamic/json?url={url}&query=$.result
  • From: shields.io
  • Shows: One value from a JSON file: here the result of the last Stryker run on main, which the GitHub Pages site publishes in stryker/report-result.json.
  • Good to know: Not used yet. One color for every value, so “passed” and “failed” look alike; an endpoint badge would fix that. Encode the url value (https%3A%2F%2F...).

checks-status (not used)

Checks of main of proj-dining as checks-status sees them

https://img.shields.io/github/checks-status/{owner}/{repo}/main
  • From: shields.io
  • Shows: Commit statuses only.
  • Good to know: Not used: it does not see GitHub Actions. It said “passing” for proj-courses, proj-dining and proj-frontiers while check-runs said failing or pending. Compare it with the badge under Checks of main.

See also

  • GitHub Actions: the workflows of CS156 projects, which the workflow badges report on.
  • GitHub Pages: where the index pages with these badges are published.