notion实现⾃动发布到hugogithub博客
notion是⽤来记录笔记的,hugo是我⽤来作为github博客⾃动构建发布的
我⽬前设置了⼀个github action是:当我的博客仓库hugo分⽀有push事件时,⾃动构建⽂章发布到master分⽀,并且发布到博客园。
但是会有这样的不便:在notion中写了⼀篇笔记或⽂章,想要发布到github静态博客上,发现需要先将⽂章转化成markdown,图⽚需要上传到图床,然后贴⼊markdown,然后再推送到github,等待action⾃动构建静态博客
既然我使⽤notion记录笔记,何不继续All-in-one,将notion作为我的博客发布⼯具。
只需要在 notion 中建⽴⼀个⽤于博客发布的 database,然后写完笔记后填⼊这个 database,再使⽤⼀些⼿段触发 CI 即可完成博客⽂章的发布
⼯具介绍
说⼲就⼲,写了两个⼯具
notiontomd是⽤来notion中的某个page转化为markdown的库,当然,当前⽀持的block是有限的,详细信息可以查看该仓库
notion_to_github_blog则是⼀个github action模板,⽤来⾃动从指定格式的database中拉取需要更新发布的⽂章,然后利⽤notiontomd转化为markdown,然后推送到github仓库,再触发另外的github aciton进⾏博客静态⽂件构建
使⽤张婧仪周迅什么关系
怎么建仓怎么⾃动从某分⽀拉取推到github pages所在分⽀我就不展开说明了,感兴趣的可以去⽹上搜索相关资料,本⽂所关注的流程是从notion database到博客源⽂件
基础环境
hugo分⽀⽤来存放博客源⽂件,其中有⼀个github action的功能是push时触发,然后⾃动构建推送到master分⽀
master分⽀⽤来存放hugo构建之后⽣成的站点静态⽂件
hugo作为主分⽀,master设置为github pages分⽀(原因后⾯描述)
workflows编写
要使⽤该action,⾸先你需要在 notion 中创建⼀个 database,这个 database 需要有⼏个字段,字段名如下:
Name (title): ⽂章标题
Article (text): ⽂章链接
MDFilename (text): 创建的 markdown ⽂件名
Category (select): ⽂章分类精华液
Tags (multi_select): ⽂章标签
麦迪娜 买买提IsPublish (checkbox): ⽂章是否发布念奴娇中秋
NeedUpdate (checkbox): ⽂章是否有更新
CreateAt (Created time): 创建时间
UpdateAt (Last edited time): 更新时间
默认当IsPublish未勾选或NeedUpdate勾选的项⽬才会触发流程,即IsPublish=false || NeedUpdate=true时触发
样例如下
然后你需要在你存放博客源⽂件的仓库进⾏⼀些设置,放置上workflows
下⾯以我的github博客仓库为例进⾏说明
我们创建⼀个workflows:
name: Notion To Blog
on:
issues:
types: [opened]
jobs:
notion-to-blog:
if: ${{ github.event.issue.user.login == github.actor && contains(github.event.issue.title, 'notion-ci') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
# Workflows are only triggered when commits (and tags I think, but it would need to be tested) are created pushed using a Personal Access Token (PAT).
# ref: github/EndBug/add-and-commit/issues/311#issuecomment-948749635
token: ${{ secrets.CHECKOUT_TOKEN }}
- name: Markdown From Notion
uses: akkuman/notion_to_github_blog@master
with:
notion_token: ${{ secrets.NOTION_TOKEN }}
notion_database_id: ${{ secrets.NOTION_DATABASE_ID }}
img_store_type: github
img_store_path_prefix: notionimg
# img_store_url_path_prefix: ${{ secrets.IMG_STORE_URL_PATH_PREFIX }}
# Actions run as an user, but when they are running in a fork there are potential security problems, so they are degraded to "read-only"
# ref: github/actions/first-interaction/issues/10#issuecomment-546628432
# ref: docs.github/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
# so you should set another token
img_store_github_token: ${{ secrets.CHECKOUT_TOKEN }}
img_store_github_repo: akkuman/pic
img_store_github_branch: master
# md_store_path_prefix: ${{ secrets.MD_STORE_PATH_PREFIX }}
- name: push to github
uses: EndBug/add-and-commit@v7
with:
branch: hugo
message: 'Notion CI'
字段解释:
notion_token: notion申请的app的api token
img_store_type: notion中提取出来的图⽚保存到哪,可选 local 或者 github,local代表保存到源仓库,github代表保存到另⼀个github 仓库(图床)中去,默认为local
img_store_path_prefix: 图⽚保存的路径前缀,默认为static/notionimg
img_store_url_path_prefix:当img_store_type=local时需要,设置在markdown图⽚链接中的前缀,和上⾯的img_store_path_prefix不相同,⽐如img_store_path_prefix='static/notionimg' img_store_url_path_prefix:='/notionimg/'的情况下,则图⽚保存路径为
'./static/notionimg/{img_md5}{img_ext}', ⽽在markdown⽂件中的体现为![](/notionimg/{img_md5}{img_ext})
img_store_github_token: 当img_store_type=github时需要,设置保存图⽚到github图床所使⽤的token(secrets.GITHUB_TOKEN只有读权限,所以需要另外使⽤)
img_store_github_repo: 当img_store_type=github时需要,你把哪个仓库当作github图床
img_store_github_branch: 当img_store_type=github时需要,你把哪个github图床仓库的哪⼀个分⽀当作图床
女人节md_store_path_prefix: 最后⽣成的markdown⽂件保存在哪,默认是当前仓库⽬录的content/posts⽬录下
其中需要关注的是
1. token: ${{ secrets.CHECKOUT_TOKEN }}是为了后⾯的push to github推送后能够触发另外⼀个action流程,否则⽆法触发,其中
的CHECKOUT_TOKEN为你创建的 Personal Access Token,具体可以查看我上⾯的注释
2. on: issues: types: [opened]的主要作⽤是当打开或提交⼀个issue时触发该action
3. if: ${{ github.event.issue.user.login == github.actor && contains(github.event.issue.title, 'notion-ci') }}的主要作⽤是:当提交issue的⼈是你⾃⼰,并且
issue标题包含 notion-ci 时进⾏action流程
注意: 只有当workflows在主分⽀时,使⽤issues作为触发条件才会⽣效,所以我个⼈是将hugo作为主分⽀,将master作为Github Pages分⽀
测试
然后我们需要把指定的databse以及所需要发布的⽂章都集成我们申请的app
以及需要发布的⽂章
注意:database中的Article列,按下@号来搜索选择⽂章
github配置好相关的Secrets,
母公司与子公司我们在仓库中提交⼀个标题包含notion-ci的issue,即可触发workflows
全⾃动整个流程
平台调研
根据官⽅⽂章中所提到的,我们可以得到⼀些可以⽤于notion的⾃动化集成平台,对⽐了⼀下,应该是最实惠的平台,免费⽤户每个⽉可以触发300次,⼀般⽽⾔,对于博客来说够了
⾃动化集成
⾸先点选Link Notion,⼀路下⼀步,出现下⾯的页⾯时,点选我们的databse
然后确认后点选我们的database
然后继续Link Github授予github权限(注意,这个应⽤所需的权限较⼤)
然后配置⼀下相关属性
注意选好相关仓库,以及Title中需要包含notion-ci
确认就好了,当然,有⼀些缺陷,免费的是每五分钟检查⼀次,等不及的话,你还是可以⼿动提交issue触发
现在尝试在database中使⽤右上⾓的New新增⼀个条⽬,查看会有什么变化
注意:所有涉及到的⽂章,都需要invite我们先前创建的app,否则github action⽆法读取到