Skip to content

Build Fonts

Build Fonts #88

Workflow file for this run

name: Build Fonts
on:
schedule:
- cron: "0 3 * * *"
workflow_dispatch:
inputs:
bump:
description: "Manual bump type"
required: false
type: choice
options: [major, minor, patch]
default: "patch"
permissions:
contents: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions"
git config user.email "github-actions@github.com"
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: pip install fonttools
# ===== 判断模式 =====
- name: Detect mode
id: mode
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "mode=manual" >> $GITHUB_OUTPUT
else
echo "mode=auto" >> $GITHUB_OUTPUT
fi
# ===== 获取上游最新版本 =====
- name: Get Nerd Fonts tag
id: nerd
run: |
TAG=$(git ls-remote --tags https://github.com/ryanoasis/nerd-fonts.git \
| awk -F/ '{print $3}' \
| grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' \
| sort -V \
| tail -n1)
echo "tag=$TAG" >> $GITHUB_OUTPUT
# ===== 获取上游 release notes(用于自动 bump 推导)=====
- name: Fetch release notes
id: notes
if: steps.mode.outputs.mode == 'auto'
run: |
TAG=${{ steps.nerd.outputs.tag }}
BODY=$(curl -s https://api.github.com/repos/ryanoasis/nerd-fonts/releases/tags/$TAG | jq -r '.body')
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "$BODY" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
# ===== 🔧 核心修复:检测是否需要构建 =====
- name: Check upstream update
id: check
if: steps.mode.outputs.mode == 'auto'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
CURRENT_UPSTREAM="${{ steps.nerd.outputs.tag }}"
LAST_USED=""
# ① 从最新标签注释中提取
LAST_TAG=$(git tag --list 'v*' | sort -V | tail -n1 || true)
if [ -n "$LAST_TAG" ]; then
INFO=$(git for-each-ref refs/tags/$LAST_TAG --format='%(contents)')
LAST_USED=$(echo "$INFO" | grep -o 'nerd-fonts: v[0-9.]*' | awk '{print $2}')
fi
# ② 若注释中无,回退到本仓库最新 Release 描述
if [ -z "$LAST_USED" ]; then
RELEASE_JSON=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/releases/latest")
# 只有成功获取到才提取,避免解析错误
if echo "$RELEASE_JSON" | jq -e '.body' >/dev/null 2>&1; then
RELEASE_BODY=$(echo "$RELEASE_JSON" | jq -r '.body')
LAST_USED=$(echo "$RELEASE_BODY" | grep -o 'nerd-fonts: v[0-9.]*' | awk '{print $2}')
fi
fi
# ③ 判断
if [ -z "$LAST_USED" ]; then
# 完全没有历史记录
if [ -z "$(git tag --list 'v*')" ]; then
echo "No previous tags found, proceeding as first build."
echo "build=true" >> $GITHUB_OUTPUT
else
echo "Could not determine last used upstream version. Skipping to avoid duplicate."
echo "build=false" >> $GITHUB_OUTPUT
fi
elif [ "$LAST_USED" != "$CURRENT_UPSTREAM" ]; then
echo "Upstream changed from $LAST_USED to $CURRENT_UPSTREAM."
echo "build=true" >> $GITHUB_OUTPUT
else
echo "Upstream unchanged ($CURRENT_UPSTREAM)."
echo "build=false" >> $GITHUB_OUTPUT
fi
# ===== 最终构建判定 =====
- name: Decide build
id: final
run: |
if [ "${{ steps.mode.outputs.mode }}" = "manual" ]; then
echo "build=true" >> $GITHUB_OUTPUT
else
echo "build=${{ steps.check.outputs.build }}" >> $GITHUB_OUTPUT
fi
- name: Stop if no build
if: steps.final.outputs.build == 'false'
run: echo "No changes, skipping"
# ===== 获取当前版本 =====
- name: Get current version
id: current
if: steps.final.outputs.build == 'true'
run: |
TAG=$(git tag --list 'v*' | sort -V | tail -n1 || true)
VERSION=$(echo $TAG | sed 's/^v//')
if [ -z "$VERSION" ]; then
VERSION="0.0.0"
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
# ===== 确定 bump 类型 =====
- name: Determine bump
id: bump_type
if: steps.final.outputs.build == 'true'
run: |
if [ "${{ steps.mode.outputs.mode }}" = "manual" ]; then
echo "bump=${{ github.event.inputs.bump }}" >> $GITHUB_OUTPUT
else
chmod +x .scripts/derive_bump.sh
BUMP=$(.scripts/derive_bump.sh "${{ steps.notes.outputs.body }}")
echo "bump=$BUMP" >> $GITHUB_OUTPUT
fi
# ===== 计算新版本 =====
- name: Compute version
id: bump
if: steps.final.outputs.build == 'true'
run: |
chmod +x .scripts/bump_version.sh
NEW_VERSION=$(.scripts/bump_version.sh \
${{ steps.current.outputs.version }} \
${{ steps.bump_type.outputs.bump }})
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "tag=v$NEW_VERSION" >> $GITHUB_OUTPUT
# ===== 构建字体 =====
- name: Patch fonts
if: steps.final.outputs.build == 'true'
run: |
mkdir out
docker run --rm --user $(id -u):$(id -g) -v $PWD/original:/in -v $PWD/out:/out nerdfonts/patcher -c
docker run --rm --user $(id -u):$(id -g) -v $PWD/original:/in -v $PWD/out:/out nerdfonts/patcher -c -s
docker run --rm --user $(id -u):$(id -g) -v $PWD/ligaturized:/in -v $PWD/out:/out nerdfonts/patcher --makegroups 1 -c
docker run --rm --user $(id -u):$(id -g) -v $PWD/ligaturized:/in -v $PWD/out:/out nerdfonts/patcher --makegroups 1 -c -s
# ===== 注入版本信息 =====
- name: Inject version
if: steps.final.outputs.build == 'true'
run: |
chmod -R u+w out
python .scripts/inject_version.py \
out \
"${{ steps.bump.outputs.version }}" \
"${{ steps.bump.outputs.tag }}" \
"${{ steps.nerd.outputs.tag }}"
# ===== 打包 =====
- name: Package
if: steps.final.outputs.build == 'true'
run: |
cd out
for prefix in MonacoLigaturizedNerdFont MonacoLigaturizedNerdFontMono MonacoNerdFont MonacoNerdFontMono; do
zip -r "../${prefix}.zip" "${prefix}-"*.ttf
done
# ===== 创建带注释的标签(从此可直接从注释读取)=====
- name: Create annotated tag
if: steps.final.outputs.build == 'true'
run: |
git tag -a "${{ steps.bump.outputs.tag }}" -m "nerd-fonts: ${{ steps.nerd.outputs.tag }}"
git push origin "${{ steps.bump.outputs.tag }}"
# ===== 发布 =====
- name: Release
if: steps.final.outputs.build == 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.bump.outputs.tag }}
name: Monaco Nerd Font ${{ steps.bump.outputs.tag }}
body: |
Source:
- nerd-fonts: ${{ steps.nerd.outputs.tag }}
- mode: ${{ steps.mode.outputs.mode }}
- bump: ${{ steps.bump_type.outputs.bump }}
files: |
MonacoLigaturizedNerdFont.zip
MonacoLigaturizedNerdFontMono.zip
MonacoNerdFont.zip
MonacoNerdFontMono.zip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}