惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
Know Your Adversary
Know Your Adversary
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
T
Tor Project blog
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
腾讯CDC
L
LangChain Blog
IT之家
IT之家
Recent Commits to openclaw:main
Recent Commits to openclaw:main
月光博客
月光博客
N
News and Events Feed by Topic
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Tailwind CSS Blog
Jina AI
Jina AI
S
Security Affairs
T
The Blog of Author Tim Ferriss
博客园 - Franky
H
Hacker News: Front Page
Martin Fowler
Martin Fowler
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
Webroot Blog
Webroot Blog
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
S
Schneier on Security
B
Blog
Schneier on Security
Schneier on Security
Latest news
Latest news
TaoSecurity Blog
TaoSecurity Blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
Recorded Future
Recorded Future
O
OpenAI News
雷峰网
雷峰网
H
Heimdal Security Blog

Mycpen

13_Hexo-操作记录 12_Hexo-GitHub Actions 发布博客静态资源至 NPM 06_转载-自我提升的8个好习惯 05_转载-延寿指南-反向面试-文档写作规范 04_转载-提问的智慧 03_转载-中文文案排版指北 11_Hexo-GitHub Actions 自动部署博客 08_Other-GitHub Actions 定时执行脚本,备份又拍云云存储 07_Other-文章记录-sshpass 03_Error-问题记录-winpty-.gitignore 02_转载-Replit 部署 Memos & Butterfly 基于 Memos 实现清单功能 09_Hexo-Replit 搭建 Uptime Kuma 监控服务 08_Hexo-博客引用 B站鸿蒙字体 07_Hexo-插件整理 06_Other-Windows 图片批量压缩工具 ultra-tinypng 01_转载-图床搭建 Backblaze B2 + Cloudflare CDN 05_Other-使用 Python open 函数批量替换文件内容 04_Other-使用 Python ftplib 模块同步又拍云云存储数据至本地 03_Other-PC端环境记录
10_Hexo-GitHub Actions 自动刷新多吉云 CDN 缓存
Mycpen · 2023-04-03 · via Mycpen

前言

之前一直通过 多吉云控制台 手动刷新博客全站 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_KEYSECRET_KEY,对应的值在 多吉云 用户中心 - 密钥管理 中查看。link

image-20230502225346425

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

image-20230502225900571

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 时,默认会取消推送 . 开头的隐藏文件,具体如下:

image-20230502224949276

在 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

参考链接

  1. 多吉云:API 验证机制(Python)

  2. 多吉云:SDK 文档 - 刷新预热

  3. ChatGPT(deploy.ignore_hidden、js 代码 复制目录、sleep)

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 Mycpen


欢迎访问 MYCPEN BLOG

建站初衷 记录学习历程,整理平时发现并解决的问题