Hexo 博客配置 GitHub Actions 推送 indexnow
本文最后编辑于:2025年1月31日 晚上
Hexo 博客配置 GitHub Actions 推送 indexnow
什么是 IndexNow?
IndexNow 是一种开源协议,可以让网站所有者在网站内容出现变化(添加、更新或删除)后通知搜索引擎,让搜索引擎立即索引这些页面和内容。这使搜索引擎能够在其搜索结果中快速反映这种变化,从而提高整体抓取效率。
API Key
找到 Indexnow 官网生成 API Key(每点击一次 Generate 都会生成一个新的 Key):
验证网站所有权
你需要点击上图中 API Key 右边的下载按钮,将该 5617c6a058f84405b90e2a0c6e99519a.txt
文件保存好。
然后把它放到你的网站根目录下,让它可以通过网址链接 https://www.example.com/5617c6a058f84405b90e2a0c6e99519a.txt
的形式访问,并得到明文 API Key:5617c6a058f84405b90e2a0c6e99519a
比如,将 5617c6a058f84405b90e2a0c6e99519a.txt
放在 source
目录下,并在_config.yml
中配置 skip_render
如下:
1
2
3
skip_render:
- '*.py'
- '*.txt'
这样可以确保 Hexo 不会尝试渲染这些文件,而只是将它们直接复制到 public
目录。
这样就做好了第一和第二步。
GitHub Actions 配置
GitHub 的 Actions 允许你运行一些小文件,我们可以采用 Python 脚本的方式来提交 URLs
编写提交 URLs 的 Python 脚本
我的 Python 文件如下(index_now.py
):
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
42
43
44
45
46
47
48
49
50
51
52
import requests
import xml.etree.ElementTree as ET
HOST = "userName.github.io" # 替换为你的实际域名
KEY = "5617c6a058f84405b90e2a0c6e99519a" # 替换为你的 API Key
def get_latest_posts(sitemap_path, n=10):
# 解析 XML sitemap
tree = ET.parse(sitemap_path)
root = tree.getroot()
# 使用命名空间
namespaces = {'s': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
# 获取所有 URL
urls = [(url.find('s:loc', namespaces).text, url.find('s:lastmod', namespaces).text)
for url in root.findall('s:url', namespaces)]
# 根据最后修改时间排序
urls.sort(key=lambda x: x[1], reverse=True)
# 返回最近的 n 个 URL
return [url[0] for url in urls[:n]]
def ping_bing(url_list):
# url = 'https://www.bing.com/indexnow'
url = 'https://api.indexnow.org/IndexNow'
headers = {
'Content-Type': 'application/json; charset=utf-8',
}
# 准备数据
data = {
"host": HOST,
"key": KEY,
"keyLocation": f"https://{HOST}/{KEY}.txt",
"urlList": url_list
}
# 发送 POST 请求
response = requests.post(url, headers=headers, json=data)
return response
if __name__ == "__main__":
sitemap_path = "sitemap.xml" # 替换为你的 sitemap 文件路径
url_list = get_latest_posts(sitemap_path, 10) # 获取最近 10 篇文章的 URL
print("最近更新的文章 URL 列表:")
print(url_list) # 打印获取的 URL 列表
response = ping_bing(url_list) # 向 IndexNow 发送请求
print("响应状态码:", response.status_code) # 打印响应状态
print("响应内容:", response.text) # 打印响应内容
脚本值得注意的点在于:
HOST = "userName.github.io"
KEY = "5617c6a058f84405b90e2a0c6e99519a"
sitemap_path = "sitemap.xml"
前面两个主要是配置成你自己的就行。
主要是第三个 sitemap 路径问题,我建议同样存放在网站根目录下。
可以安装 hexo-generator-sitemap
插件,该插件在每次 hexo g
生成文件时重新生成新的 sitemap.xml
文件并放置在 public
目录下,这会使得它被推送时放置在仓库根目录下,符合我脚本的要求。
脚本编写好后,同样需要将其推送到仓库,我的是根目录。我将脚本同样放在 source
目录下,这样它会和 key.txt
一同被推送至仓库根目录。
本地创建 workflows
Hexo 配置
创建 GitHub workflow 一般是.github/workflows/xxx.yml
但是 Hexo 默认忽略以点(.example
)开头的文件和文件夹,所以我的做法是修改_config.yml
中的 include
项,将其添加进去:
1
2
include:
- ".github/**/**"
此时还需要修改下 skip_render 这个配置,否则 hexo 会自动把 yaml 文件给转成 json 格式的,到时候 GitHub 是不识别的(_config.yml
):
1
2
skip_render:
- ".github/**/**"
最后需要配置下 hexo 的 deploy 配置,不要忽略隐藏文件。否则在最后部署到 GitHub 时,隐藏文件会被忽略掉(_config.yml
):
1
2
3
4
5
deploy:
type: git
repository: git@github.com:userName/userName.github.io.git
branch: master
ignore_hidden: false # 取消忽略隐藏文件
.github
隐藏目录相关配置项参考链接为:Hexo 博客使用 GitHub Actions 来自动提交 Sitemap
这样,Hexo 会将 source/.github
文件夹中的内容也复制到 public
目录中,一旦执行 hexo d
命令便会部署到 GitHub。
不过你得确保你的 .github
目录直接放在 source
目录下,即结构如下:
1
2
3
4
5
your-hexo-site/
├── source/
│ ├── .github/
│ │ └── workflows/
│ │ └── your-action.yml
这样一来,当你推送代码至仓库时,GitHub 会自动触发 Actions 运行你的 your-action.yml
文件,所以需要在这个 yml
文件中调用我们写的 Python 脚本。
编写 action 触发文件
我的 indexnow.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
name: Submit IndexNow Request To Bing
on:
push:
branches: ["master"] # 根据需要替换为你的主分支
workflow_dispatch: # 允许手动触发工作流
jobs:
indexnow:
name: IndexNow
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11' # 可以根据需要调整 Python 版本
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests # 安装 requests 库
- name: Run IndexNow script
run: |
python index_now.py # 替换为你的 Python 脚本文件名
该文件值得注意的点在于:
branches: ["master"]
python index_now.py
注意修改成你的配置项就行。
至此准备工作已经完毕,你只需要推送新代码到仓库就会自动触发 actions 发请求给必应的 indexnow,最终你可以在必应网站管理员工具查看效果。
- 注:实际上你也可以单独在本地运行该 python 文件,效果是一致的。