使用Github Actions 动态更新Github主页

总体思路

借鉴思路地址使用Github Actions 动态更新Github主页 - 方圆小站 (fangyuanxiaozhan.com)

原文讲的东西不够全,我这会补充一点

项目参考地址leonyan18/leonyan18 (github.com)

  1. python脚本爬取博客更新内容,并写入更新到指定位置
  2. github action 每天定时启动这个任务

python脚本

第一步是选择你要插入的位置,并修改脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
这是我的ReadME.md
## Recent Blog Posts(update time:2022-12-29 10:15:20)

[Alist+RaiDrive+阿里云盘=本地网盘+文件实时同步+备份](http://dawnchannel.tech/2022/12/28/local_sync/)

[Hexo博客搭建以及文件同步](http://dawnchannel.tech/2022/12/27/blog/)

[牛客模拟缓存](http://dawnchannel.tech/2022/12/26/NowcoderEmulateCache/)


<br/>

## 💻:keyboard: Languages and Tools

我是在## Recent Blog Posts和## 💻:keyboard: Languages and Tools 之间进行插入的,根据实际情况替换值,我这边是多了几个回车,需要注意一下,在本地调试通过之后在进行上传。

在main.py对应位置进行修改,在两个insert_info = 的位置

由于本人是通过hexo+icarus主题实现的,博客内容可以通过http://dawnchannel.tech/content.json直接获得,可以将其替换成自己的地址。

其他地址需要个人自定义了,还要注意一点我这个是按照时间顺序下来的,所以我还进行了一个逆序操作。

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
40
41
import time
import os
import re
import pytz
import requests
import json
from datetime import datetime

def get_link_info(feed_url, num):
result = ""
response = requests.get(feed_url).text
response=json.loads(response)
feed_entries = response["posts"]
feed_entries.reverse()
feed_entries_length = len(feed_entries)
all_number = 0
if(num > feed_entries_length):
all_number = feed_entries_length
else:
all_number = num

for entrie in feed_entries[0: all_number]:
title = entrie["title"]
link = "http://dawnchannel.tech"+entrie["link"]
result = result + "\n" + "[" + title + "](" + link + ")" + "\n"
return result

def main():
insert_info = get_link_info("http://dawnchannel.tech/content.json", 6)
# 替换 ---start--- 到 ---end--- 之间的内容
# pytz.timezone('Asia/Shanghai')).strftime('%Y年%m月%d日%H时M分')
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
insert_info = "## Recent Blog Posts(" + "update time:"+ datetime.fromtimestamp(int(time.time()),pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S') +")\n" + insert_info + "\n\n<br/>\n\n## 💻:keyboard: Languages and Tools "
# 获取README.md内容
print(insert_info)
with open (os.path.join(os.getcwd(), "README.md"), 'r', encoding='utf-8') as f:
readme_md_content = f.read()
new_readme_md_content = re.sub(r'## Recent Blog Posts(.|\n)*## 💻:keyboard: Languages and Tools ', insert_info, readme_md_content)
with open (os.path.join(os.getcwd(), "README.md"), 'w', encoding='utf-8') as f:
f.write(new_readme_md_content)
main()

第三步 本地调试

主要测试一下,本地的ReadME文件是否正常,在没有最近博客的时候和有最近博客的时候都测试一下。

github actions

前期准备

先解释下action的流程

  1. 创建所需要的环境
  2. 安装对应依赖
  3. git操作

然后看下我们的目录结构以及所需要的东西,缺一样都不可以

  1. ReadMe.md ,主要需要修改的对象
  2. main.py,python脚本用于修改内容
  3. Pipfile,创建环境安装对应依赖
  4. .gitignore,忽略生成的其他无关文件

.gitignore和Pipfile文件直接使用参考仓库里,如果用到其他库或者新增其他文件了,请自行添加。

启动actions

点击仓库下面的actions按钮,创建对应main.yml

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
name: 'leonyan18'

on:
push:
schedule:
- cron: '0 1 * * *'

jobs:
stale:
runs-on: ubuntu-latest
strategy:
matrix:
# in this example, there is a newer version already installed, 3.7.7, so the older version will be downloaded
python-version: ['3.7.4']
steps:
- uses: actions/checkout@v2 # Checking out the repo
- name: Install dependecies
uses: VaultVulp/action-pipenv@v2.0.1
with:
command: install -d # Install all dependencies, including development ones
- name: Build
uses: VaultVulp/action-pipenv@v2.0.1
with:
command: run build
- name: Commit and push if changed # 更新README.md
run: |
git diff
git config --global user.email "1183503933@qq.com"
git config --global user.name "leonyan"
git add README.md
git commit -m "Github Action Auto Updated"
git push

提交之后,去Enable这个job就可以每天更新了。

注意

当你重新启动失败的job的时候,一定要手动触发,重新运行之前的已经不行了,因为readme已经被修改过了,可以通过重新git提交会自动触发

或者改为手动触发

1
2
3
on:
# 手动触发事件
workflow_dispatch:

使用Github Actions 动态更新Github主页

http://dawnchannel.tech/2022/12/29/GetRecentBlog/

作者

leon Yan

发布于

2022-12-29

更新于

2022-12-30

许可协议


评论