请演示用 GitHub Actions 在 PR 时自动运行 svg-lint,检查未转曲文字。

解读

面试官想验证三件事:

  1. 你是否真正理解 SVG 源文件里“文字未转曲”带来的生产风险——激光切割/印刷厂直接拒单、字体缺失导致跑版。
  2. 你是否能把设计规范落地到工程化流程,而不是口头提醒同事“记得转曲”。
  3. 你对国内开源治理现状是否熟悉:GitHub 访问不稳定、私有仓库 Runner 成本、svg-lint 国内镜像源等现实痛点。

因此,答案必须给出“能跑起来的最小闭环”,同时把国内网络、私有仓库、安全合规、失败提示四个场景都照顾到。

知识点

  1. SVG 内部两种文字对象
    • <text> 元素:可编辑、可缺字体。
    • 转曲后变成 <path d="…"/>,不再依赖字体。
  2. svg-lint 原理
    遍历 DOM,一旦遇到非空 <text> 即报错,可配置白名单。
  3. GitHub Actions 触发事件
    pull_request 类型:opened、synchronize、reopened。
  4. 国内加速
    使用 npmmirror 镜像、actions 缓存、self-hosted Runner(阿里云 2 vCPU 包年 300 元即可)。
  5. 失败提示
    利用 actions/comment-pull-request 在 PR 里 @ 设计者,并给出一键转曲命令,降低修复门槛。

答案

仓库根目录放置以下三份文件即可通过面试“现场跑通”演示。

  1. .github/workflows/svg-lint.yml
name: SVG 文字转曲检查
on:
  pull_request:
    paths: ['**.svg']          # 仅 SVG 文件变动才触发,节省 Runner 时长
jobs:
  lint:
    runs-on: ubuntu-22.04
    steps:
      - name: 检出代码
        uses: actions/checkout@v4

      - name: 安装 Node 环境与国内镜像
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'

      - name: 安装 svg-lint
        run: |
          npm config set registry https://registry.npmmirror.com
          npm install -g svg-lint@2

      - name: 运行检查
        id: lint
        run: |
          # 找到所有新增或修改的 SVG
          git fetch origin ${{ github.base_ref }} --depth=1
          mapfile -t files < <(git diff --name-only --diff-filter=AM origin/${{ github.base_ref }} | grep '\.svg$')
          if [ ${#files[@]} -eq 0 ]; then
            echo "未检测到 SVG 变更,跳过"
            exit 0
          fi
          # 逐文件检查
          for f in "${files[@]}"; do
            echo "Checking $f"
            svg-lint --rule=no-text --error-on-warn "$f"
          done

      - name: 失败评论
        if: failure()
        uses: actions/github-script@v7
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `🚨 **未转曲文字 detected**\n\n请在该 SVG 文件内执行 **文字 → 对象变成路径**(Shift+Ctrl+C)后重新提交。\n\n设计师 @${{ github.event.pull_request.user.login }} 辛苦啦~`
            })
  1. .svglintignore(可选,白名单)
# 版权页保留可编辑文字
assets/copyright.svg
  1. 本地一键修复脚本(供设计师复制粘贴)
#!/usr/bin/env bash
# save as fix-text.sh
for svg in "$@"; do
  inkscape --batch-process --actions="select-all:text;object-to-path;export-filename:$svg;export-plain-svg" "$svg"
done

演示流程

  1. 提交 PR,内含带 <text> 的 logo.svg。
  2. Actions 自动触发,日志里清晰打印
    Checking logo.svg
    Error: <text> element found
  3. PR 被标记失败,并自动评论 @ 设计者,给出修复命令。
  4. 设计者本地运行 ./fix-text.sh logo.svg → commit → push,PR 状态自动转绿。

拓展思考

  1. 多仓库复用
    把 workflow 做成公开 reusable workflow,设计组所有项目一行 uses: your-org/svg-lint-workflow@v1 即可接入,符合国内集团化“中台”治理思路。
  2. 质量门禁
    在 GitHub 设置里打开“Require status checks to pass”,未转曲文字直接阻塞合并,彻底杜绝“忘记转曲”导致的生产事故。
  3. 大文件性能
    单文件 50 MB 以上的切割级 SVG,可先用 inkscape --shell 模式批量转曲,再提交;CI 侧加 timeout-minutes: 5 防止卡死。
  4. 私有仓库 Runner 成本
    若公司代码托管在 Gitee 私有库,可用 Gitee Go 语法等价迁移,或把 Runner 注册到阿里云轻量服务器,月流量 500 GB 足够 20 人团队。
  5. 合规审计
    在 yml 里加 if: github.event.pull_request.head.repo.fork == false 可防止外部贡献者恶意消耗 Runner 时长,满足国内信安审计要求。