前言
之前一直通过 多吉云控制台 手动刷新博客全站 CDN 缓存,CDN 源站为 Vercel 部署的网址
由 空梦:全自动博客部署方案 这篇文章萌生了自动化的念头,且这些 CDN 服务商都提供了相应的 API 文档和代码样例,开箱即用
仓库示例:https://github.com/mycpen/blog/tree/main/.github
个人示例
1. 新增 Workflow 文件
以我为例,新增 source/.github/workflows/refresh-dogecloud.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 33
|
name: Refresh dogecloud CDN
on: push: branches: - main workflow_dispatch:
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: szenius/set-timezone@v1.0 with: timezoneLinux: "Asia/Shanghai" - name: Set up Python 3.10 uses: actions/setup-python@v3 with: python-version: "3.10" - name: Wait for 3 minutes run: sleep 180 - name: Run refresh script env: ACCESS_KEY: ${{ secrets.ACCESS_KEY }} SECRET_KEY: ${{ secrets.SECRET_KEY }} run: | pip install requests python .github/refresh-dogecloud.py
|
2. 新增 PY 脚本:刷新缓存
以我为例,新增 source/.github/refresh-dogecloud.py 文件,内容如下:
脚本里的 url_list 为需要刷新的目录,需手动修改为自己的。代码来自多吉云 API 验证、刷新目录任务
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 53 54 55 56 57 58 59
| from hashlib import sha1 import hmac import requests import json import urllib import os
def dogecloud_api(api_path, data={}, json_mode=False): """ 调用多吉云API
:param api_path: 调用的 API 接口地址,包含 URL 请求参数 QueryString,例如:/console/vfetch/add.json?url=xxx&a=1&b=2 :param data: POST 的数据,字典,例如 {'a': 1, 'b': 2},传递此参数表示不是 GET 请求而是 POST 请求 :param json_mode: 数据 data 是否以 JSON 格式请求,默认为 false 则使用表单形式(a=1&b=2)
:type api_path: string :type data: dict :type json_mode bool
:return dict: 返回的数据 """
access_key = os.environ["ACCESS_KEY"] secret_key = os.environ["SECRET_KEY"]
body = '' mime = '' if json_mode: body = json.dumps(data) mime = 'application/json' else: body = urllib.parse.urlencode(data) mime = 'application/x-www-form-urlencoded' sign_str = api_path + "\n" + body signed_data = hmac.new(secret_key.encode('utf-8'), sign_str.encode('utf-8'), sha1) sign = signed_data.digest().hex() authorization = 'TOKEN ' + access_key + ':' + sign response = requests.post('https://api.dogecloud.com' + api_path, data=body, headers = { 'Authorization': authorization, 'Content-Type': mime }) return response.json()
url_list = [ "https://cpen.top/", ]
api = dogecloud_api('/cdn/refresh/add.json', { 'rtype': 'path', 'urls': json.dumps(url_list) }) if api['code'] == 200: print(api['data']['task_id']) else: print("api failed: " + api['msg'])
|
3. 新增 Secrets 变量
Actions 新增 2 个 Secrets 变量,ACCESS_KEY、SECRET_KEY,对应的值在 多吉云 用户中心 - 密钥管理 中查看。link

避免因权限问题导致工作流执行失败,也可以设置下 Actions 读写权限

4. 新增 JS 代码:复制 .github
前言:source/.github 这类 . 开头的隐藏文件默认是不会被 hexo generate 渲染生成到 public 目录下的。网上和 ChatGPT 给出的解决办法大多试了(如 skip_render),没有生效,打算直接用 JS 代码将 source/.github 目录复制到 public/.github 目录下。以下代码每次 hexo generate 之后能实现复制的效果
以我为例,新增 scripts/before_generate.js 文件,内容如下:
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
| const fs = require('fs-extra'); const path = require('path');
function copyFolder(source, target) { fs.ensureDirSync(target);
const files = fs.readdirSync(source);
files.forEach((file) => { const sourcePath = path.join(source, file); const targetPath = path.join(target, file);
if (fs.statSync(sourcePath).isDirectory()) { copyFolder(sourcePath, targetPath); } else { fs.copyFileSync(sourcePath, targetPath); } }); }
copyFolder('./source/.github', './public/.github');
|
5. 更新 _config.yml 配置
当 hexo deploy 将渲染后的静态仓库推送到 Github 时,默认会取消推送 . 开头的隐藏文件,具体如下:

在 Hexo 配置文件 _config.yml 进行修改,新增 git 部署器 ignore_hidden 配置项,样例如下:
1 2 3 4 5 6 7 8 9 10
| # Deployment ## Docs: https://hexo.io/docs/one-command-deployment deploy: - type: git repository: git@github.com:mycpen/blog.git branch: main commit: Site updated message: "{{ now('YYYY/MM/DD') }}" + ignore_hidden: false
|
参考链接
多吉云:API 验证机制(Python)
多吉云:SDK 文档 - 刷新预热
ChatGPT(deploy.ignore_hidden、js 代码 复制目录、sleep)