惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
A
About on SuperTechFans
博客园 - Franky
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
G
Google Developers Blog

二丫讲梵

学习周刊-总第258期-2026年第15周 学习周刊-总第257期-2026年第14周 学习周刊-总第256期-2026年第13周 学习周刊-总第255期-2026年第12周 学习周刊-总第254期-2026年第11周 学习周刊-总第253期-2026年第10周 临时插播一条羊毛,免费领取450元大模型API代金券 学习周刊-总第252期-2026年第09周 学习周刊-总第251期-2026年第08周 学习周刊-总第250期-2026年第07周 学习周刊-总第249期-2026年第06周 诚邀评论,聊聊你所知欲知的我 学习周刊-总第248期-2026年第05周 我的QQ动态之2015年 我的QQ动态之2014年 我的QQ动态之2013年 我的QQ动态之2012年 我的QQ动态-2010-2011年 我的QQ动态-创栏小叙 学习周刊-总第247期-2026年第04周 Nexus社区版权益阉割--一文告诉你有哪些版本可以选择 学习周刊-总第246期-2026年第03周 学习周刊-总第245期-2026年第02周 学习周刊-总第244期-2026年第01周 学习周刊-总第243期-2025年第52周 学习周刊-总第242期-2025年第51周 开源项目ZenOps:带你领略禅意运维 学习周刊-总第241期-2025年第50周 用京东金融,享负债人生 学习周刊-总第240期-2025年第49周
利用Github Action自动检测项目中 yaml 或 json 的语法
二丫讲梵 · 2024-01-22 · via 二丫讲梵

# 前言

我最近开源的 awesome-ops (opens new window) 项目,其中的项目分类及表格数据的生成,依赖 yaml-readme (opens new window) 这个项目所实现的 action 来完成。因此,其中的项目都是基于大量 yaml 来收集,在于 yaml 打交道的过程中,不可避免的会遇到错漏的情况,这个时候,增加一个 yaml 语法检测的 action 就势在必行了。

当我有了这个念头之后,就针对这方面的 action 进行了一大波调研,发现市场上有许多对应的 action,真正好用的并不多,因此,在花了一两个小时之后,才发掘到了本文将要介绍的 action,如果你也有此类场景与需求,那么就往下读吧。

# 配置

所用 Actions。

使用配置其实非常简单,基本上阅读完官方介绍文档就可以上手使用了。

我这里就以自己项目中所用的配置 (opens new window)进行说明:

name: yaml-validate
on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

permissions:
  contents: read
  pull-requests: write # enable write permissions for pull request comments

jobs:
  json-yaml-validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: json-yaml-validate
        id: json-yaml-validate
        uses: GrantBirki/json-yaml-validate@v2.6.0 # replace with the latest version
        with:
          comment: "true" # enable comment mode
          base_dir: "items"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

其中所用到的参数也比较简单,这里做几点说明:

  • pull_request: 在触发条件中记得配置此项,从而实现项目再接收到 pr 时自动对项目内的文件进行语法检测。如果失败则无法进行正常 pr,从而实现协作者自行确保提交内容格式正确的义务。
  • comment: 当检测失败时,该 action 会自动将检测到的错误通过评论的形式呈现在对应 pr 的评论区,非常优雅。
  • base_dir: 指定将要扫描的路径,如果不配置,则默认扫描所有后缀符合 yaml 或 json 的文件。

只需如上简单配置即可实现想要的效果。

当然该 action 还有其他一些参数,你可以点此 (opens new window)进行了解。

# 效果

正常情况下,执行成功就会显示通过,如果扫描到语法有问题,则会通过评论的形式在 pr 中,呈现效果如下:

我这里所用到的是 yaml 的语法检测,它还支持 json 语法的检测,如果你也有类似场景,就快应用起来吧。