Skip to content

Update Python version badge #284

Update Python version badge

Update Python version badge #284

name: Update Python version badge
on:
workflow_run:
workflows: ["CI"]
types: [completed]
workflow_dispatch:
jobs:
python-version-badge:
if: ${{ github.event.workflow_run.head_branch == 'main' && github.event.workflow_run.event == 'push' }}
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v6
with:
persist-credentials: false
- name: Prepare Python icon
id: python-icon
run: |
# Prefer explicit light/dark icons so CI can embed a specific file.
# Use the single `python.svg` icon file.
if [ -f .github/icons/python.svg ]; then
ICON_FILE=.github/icons/python.svg
else
ICON_FILE=""
fi
if [ -n "$ICON_FILE" ] && [ -f "$ICON_FILE" ]; then
ICON_BASE64=$(base64 -w0 "$ICON_FILE")
echo "icon=data:image/svg+xml;base64,$ICON_BASE64" >> $GITHUB_OUTPUT
echo "icon_path=$ICON_FILE" >> $GITHUB_OUTPUT
else
echo "icon=" >> $GITHUB_OUTPUT
echo "icon_path=" >> $GITHUB_OUTPUT
fi
- name: Generate Python version badge
uses: emibcn/badge-action@v2
with:
label: "Python"
status: "3.12 | 3.13 | 3.14"
color: "3776ab"
icon: ${{ steps.python-icon.outputs.icon }}
path: ".github/python-version.svg"
- name: Ensure icon is embedded in SVG
run: |
FILE=.github/python-version.svg
ICON_PATH=${{ steps.python-icon.outputs.icon_path }}
# If the workflow provided an icon path, embed its base64 into the badge SVG.
if [ -f "$FILE" ] && [ -n "$ICON_PATH" ] && [ -f "$ICON_PATH" ]; then
ICON_B64=$(base64 -w0 "$ICON_PATH")
perl -0777 -pe "s/xlink:href=\"[^\"]*\"/xlink:href=\"data:image\/svg\+xml;base64,$ICON_B64\"/s" -i "$FILE"
fi
- name: Commit Python version badge
env:
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
GIT_USER_NAME: ${{ secrets.GIT_USER_NAME || github.actor }}
GIT_USER_EMAIL: ${{ secrets.GIT_USER_EMAIL || format('%s@users.noreply.github.com', github.actor) }}
run: |
set -euo pipefail
if [ -z "${PERSONAL_ACCESS_TOKEN:-}" ]; then
echo "PERSONAL_ACCESS_TOKEN is not set. Skipping push to remote to avoid protected branch rejection."
exit 0
fi
git config user.name "$GIT_USER_NAME"
git config user.email "$GIT_USER_EMAIL"
remote_url=$(git remote get-url origin)
if [[ "$remote_url" == https://* ]]; then
auth_remote=$(echo "$remote_url" | sed -E "s#https://#https://${PERSONAL_ACCESS_TOKEN}@#")
git remote set-url origin "$auth_remote"
fi
# Stash any generated/untracked files (badge SVGs), pull latest
# changes, then restore. This avoids "cannot pull with rebase: You
# have unstaged changes" errors when multiple workflows race.
git stash push --include-untracked -m "badge-workflow-stash" || true
git pull --rebase origin main || true
git stash pop --index || true
git add .github/python-version.svg || true
if ! git diff --staged --quiet; then
if [ -n "${GPG_PRIVATE_KEY:-}" ]; then
echo "$GPG_PRIVATE_KEY" | gpg --batch --import || true
keyid=$(gpg --list-secret-keys --with-colons | awk -F: '/^sec/ {print $5; exit}') || true
if [ -n "$keyid" ]; then
git config user.signingkey "$keyid" || true
git config commit.gpgsign true || true
export GIT_COMMITTER_SIGNINGKEY="$keyid"
fi
fi
if [ -n "${GPG_PRIVATE_KEY:-}" ]; then
git commit -S -m "chore: update Python version badge" || true
else
git commit -m "chore: update Python version badge" || true
fi
git push origin HEAD:main || true
else
echo "No changes to Python version badge"
fi