背景

最近在 Github Marketplace 上发现一个有趣的 Github Actions Workflow,名为 Blog Post Workflow

主要功能是:使用 RSS 提要,可以在个人 GitHub profile/Project 的 readme.md 上自动显示来自任何来源的最新博客文章或 StackOverflow activity 或 Youtube Videos。

例如最新博客的显示效果如下图所示

image-20211201151404357

恰巧个人时常在博客中写点水文,因此想折腾下同步至个人 Github Profile 上。

本文记录下 Blog Post Workflow 的使用过程,同时感兴趣的话可以折腾下其它有趣的 Workflows:top 20 most used/starred Github Actions

流程

实现以上效果的流程如下主要包括:

  1. 创建 Github Profile;
  2. 创建 Blog Post Workflow ;
  3. 优化 Github Actions 触发方式;

Github Profile

首先在 Github 中新建同名个人仓库,例如笔者账户名 dreamhomes 新建仓库为 dreamhomes。 Github Profile 会显示此仓库下 README.md 中的内容。笔者仓库为 https://github.com/dreamhomes/dreamhomes,然后在 README.md 中定制下个人偏好格式,当前显示效果如下:

Github Profile

当然各种大佬造出了各种花里胡哨的 profile 格式,可以参考 Awesome GitHub Profile READMEs

Blog Post Workflow

在配置 profile 的基础上,然后就需要通过 Blog Post Workflow 定时获取最新博客文章,并更新至仓库下的 README.md 中。

此部分参考官方说明 How to use

  1. 首先在 README.md 中以下添加字段,workflow 会替代以下字段然后填充最新博客文章。

    1
    2
    3
    # Latest Blog Posts
    <!-- BLOG-POST-LIST:START -->
    <!-- BLOG-POST-LIST:END -->
  2. 在同名仓库下新建 Actions 并自定义 workflow 配置文件;模板如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    name: Latest blog post workflow
    on:
    schedule: # Run workflow automatically
    - cron: '0 * * * *' # Runs every hour, on the hour
    workflow_dispatch: # Run workflow manually (without waiting for the cron to be called), through the Github Actions Workflow page directly

    jobs:
    update-readme-with-blog:
    name: Update this repo's README with latest blog posts
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
    uses: actions/checkout@v2
    - name: Pull in dev.to posts
    uses: gautamkrishnar/blog-post-workflow@master
    with:
    feed_list: "https://dev.to/feed/gautamkrishnar,https://www.gautamkrishnar.com/feed/"

    需要替换博客 RSS 链接,同时设定写参数。个人配置如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    # This is a basic workflow to help you get started with Actions

    name: Latest blog post workflow

    # Controls when the workflow will run
    on:
    # Triggers the workflow on push or pull request events but only for the main branch
    # push:
    # branches: [ main ]
    # pull_request:
    # branches: [ main ]
    schedule: # Run workflow automatically
    - cron: '0 3 * * *' # Runs at 3:00

    # Allows you to run this workflow manually from the Actions tab
    workflow_dispatch:

    # A workflow run is made up of one or more jobs that can run sequentially or in parallel
    jobs:
    # This workflow contains a single job called "build"
    update-readme-with-blog:
    name: Update README with latest blog posts
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - name: Checkout
    uses: actions/checkout@v2

    # Runs a single command using the runners shell
    - name: Update posts
    uses: gautamkrishnar/blog-post-workflow@master
    with:
    feed_list: "https://dreamhomes.top/atom.xml"
    max_post_count: 7

    配置完成后手动触发一次 workflow 观察效果是否达到预期。笔者显示效果如下,达到预期 ↓ 🐶

    显示效果

至此,整个 workflow 流程即可结束,但存在一定的问题,即每天定时触发 workflow 拉取最新博客文章有点浪费资源,毕竟吾等佛系的人并不是每天都更新博客 🙃 最好的方案是新博客发布时触发 workflow 更新文章!因此需要优化触发任务。

优化触发任务

更改定时的 Actions 任务,主要目的是当博客更新文章时再触发 Blog Post Workflow 更新 README.md 文章,所以需要监控是否有文章更新事件,当有文章更新时发送事件再触发 workflow 更新。

回想笔者将博客文件存储在 github 上利用 Actions 来自动部署,详情见另一篇博客利用 Github Actions 自动化部署 Hexo 博客 。 那么是否可以在部署结束后再发送一个事件 来触发 Blog Post Workflow 执行?

Github 提供 repository_dispatch 来触发 workflow。

You can use the GitHub API to trigger a webhook event called repository_dispatch when you want to trigger a workflow for activity that happens outside of GitHub.

主要作用:在 Github 仓库外通过 Github API 来触发称为 repository_dispatch 的 webhook event 来触发 workflow。

所以可以在博客部署完后再创建一个 webhook event 来触发 Blog Post Workflow,如何创建 repository dispatch event 可以参考 Github Docs

首先更改 Blog Post Workflow 的触发方式为 repository_dispatch,对应的配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# This is a basic workflow to help you get started with Actions

name: Latest blog post workflow

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the main branch
# push:
# branches: [ main ]
# pull_request:
# branches: [ main ]
# schedule: # Run workflow automatically
# - cron: '0 3 * * *'
repository_dispatch:
types: [update_profile]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
update-readme-with-blog:
name: Update README with latest blog posts
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout
uses: actions/checkout@v2

# Runs a single command using the runners shell
- name: Update posts
uses: gautamkrishnar/blog-post-workflow@master
with:
feed_list: "https://dreamhomes.top/atom.xml"
max_post_count: 7

设置完成后可以通过 repository_dispatch 的 type 为 update_profile 的事件来触发 Blog Post Workflow。

本地利用 curl 命令进行测试,其中 ********** 为 Github token,可以自己生成。

1
2
3
4
5
6
$ curl \
-X POST \
-u "dreamhomes:*****************"\
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" https://api.github.com/repos/dreamhomes/dreamhomes/dispatches \
-d '{"event_type":"update_profile"}'

执行完成后即可看到 Github 上 Blog post workflow 执行。👍

Actions

最后,为了保证 Actions 部署完博客后再更新 Profile,可以在部署博客的 workflow 后面再发送类型为 update_profile 类型的事件,添加 Step 如下:

1
2
3
4
5
6
7
8
9
- name: Trigger update profile dispatch
run: |
sleep 120
curl \
-X POST \
-u "${{ secrets.USER }}:${{ secrets.UPDATE_PROFILE }}" \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" https://api.github.com/repos/dreamhomes/dreamhomes/dispatches \
-d '{"event_type":"update_profile"}'

以上即可通过 Push 博客源文件后,自动部署到 Github 而且更新 Profile! 🎉