From bbf6e4520419e17606bb9df0f846696d82ad99b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B5=B5=E5=90=8C=E5=AD=A6?= <94612053+imzql@users.noreply.github.com> Date: Mon, 16 Sep 2024 18:19:30 +0800 Subject: [PATCH] 9.16 --- content/post/dogeyun-auto-refresh-cdn.md | 153 ++++++++++ public/about/index.html | 11 + public/archives/index.html | 22 ++ public/categories/index.html | 10 +- public/categories/page/2/index.html | 10 +- public/categories/page/3/index.html | 10 +- public/categories/page/4/index.html | 10 +- public/categories/page/5/index.html | 10 +- public/categories/page/6/index.html | 10 +- public/categories/page/7/index.html | 10 +- public/categories/page/8/index.html | 5 + public/categories/rss.xml | 24 +- public/categories/教程/index.html | 10 +- public/categories/教程/page/2/index.html | 10 +- public/categories/教程/page/3/index.html | 10 +- public/categories/教程/page/4/index.html | 10 +- public/categories/教程/page/5/index.html | 10 +- public/categories/教程/page/6/index.html | 10 +- public/categories/教程/page/7/index.html | 10 +- public/categories/教程/page/8/index.html | 5 + public/categories/教程/rss.xml | 133 ++++++++- public/categories/文案/index.html | 10 +- public/categories/文案/page/2/index.html | 10 +- public/categories/文案/page/3/index.html | 10 +- public/categories/文案/page/4/index.html | 10 +- public/categories/文案/page/5/index.html | 10 +- public/categories/文案/page/6/index.html | 10 +- public/categories/文案/page/7/index.html | 10 +- public/categories/文案/page/8/index.html | 5 + public/categories/生活/index.html | 10 +- public/categories/生活/page/2/index.html | 10 +- public/categories/生活/page/3/index.html | 10 +- public/categories/生活/page/4/index.html | 10 +- public/categories/生活/page/5/index.html | 10 +- public/categories/生活/page/6/index.html | 10 +- public/categories/生活/page/7/index.html | 10 +- public/categories/生活/page/8/index.html | 5 + public/categories/相册/index.html | 10 +- public/categories/相册/page/2/index.html | 10 +- public/categories/相册/page/3/index.html | 10 +- public/categories/相册/page/4/index.html | 10 +- public/categories/相册/page/5/index.html | 10 +- public/categories/相册/page/6/index.html | 10 +- public/categories/相册/page/7/index.html | 10 +- public/categories/相册/page/8/index.html | 5 + public/index.html | 24 +- public/page/2/index.html | 22 +- public/page/3/index.html | 22 +- public/page/4/index.html | 22 +- public/page/5/index.html | 22 +- public/page/6/index.html | 22 +- public/page/7/index.html | 22 +- public/page/8/index.html | 11 + .../48dad6112a4e2110c95b04111084309.png | Bin 0 -> 55958 bytes .../f2736183148b132aea6cb14908e8e86.png | Bin 0 -> 140997 bytes .../post/dogeyun-auto-refresh-cdn/index.html | 268 ++++++++++++++++++ public/post/index.html | 10 +- public/post/page/2/index.html | 10 +- public/post/page/3/index.html | 10 +- public/post/page/4/index.html | 10 +- public/post/page/5/index.html | 10 +- public/post/page/6/index.html | 10 +- public/post/page/7/index.html | 10 +- public/post/page/8/index.html | 5 + public/post/rss.xml | 133 ++++++++- public/rss.xml | 133 ++++++++- public/sitemap.xml | 15 +- public/tags/index.html | 10 +- public/tags/page/2/index.html | 10 +- public/tags/page/3/index.html | 10 +- public/tags/page/4/index.html | 10 +- public/tags/page/5/index.html | 10 +- public/tags/page/6/index.html | 10 +- public/tags/page/7/index.html | 10 +- public/tags/page/8/index.html | 5 + public/tags/美女/index.html | 10 +- public/tags/美女/page/2/index.html | 10 +- public/tags/美女/page/3/index.html | 10 +- public/tags/美女/page/4/index.html | 10 +- public/tags/美女/page/5/index.html | 10 +- public/tags/美女/page/6/index.html | 10 +- public/tags/美女/page/7/index.html | 10 +- public/tags/美女/page/8/index.html | 5 + .../48dad6112a4e2110c95b04111084309.png | Bin 0 -> 55958 bytes .../f2736183148b132aea6cb14908e8e86.png | Bin 0 -> 140997 bytes 85 files changed, 1281 insertions(+), 378 deletions(-) create mode 100644 content/post/dogeyun-auto-refresh-cdn.md create mode 100644 public/pics/hugo/dogeyun-auto-refresh-cdn/48dad6112a4e2110c95b04111084309.png create mode 100644 public/pics/hugo/dogeyun-auto-refresh-cdn/f2736183148b132aea6cb14908e8e86.png create mode 100644 public/post/dogeyun-auto-refresh-cdn/index.html create mode 100644 static/pics/hugo/dogeyun-auto-refresh-cdn/48dad6112a4e2110c95b04111084309.png create mode 100644 static/pics/hugo/dogeyun-auto-refresh-cdn/f2736183148b132aea6cb14908e8e86.png diff --git a/content/post/dogeyun-auto-refresh-cdn.md b/content/post/dogeyun-auto-refresh-cdn.md new file mode 100644 index 00000000..9b5e00b9 --- /dev/null +++ b/content/post/dogeyun-auto-refresh-cdn.md @@ -0,0 +1,153 @@ +--- +title: "Hugo使用GitHub Action自动刷新多吉云CDN缓存" +date: "2024-09-16T14:48:42+08:00" +draft: false +categories: [] +tags: [] +author: "小赵同学" +layout: "post" +status: "public" +categories: ["教程"] +tags: [] +--- + +博客目前使用的方案是多吉云全站cdn缓存,使用git将hugo文件推送到github + +大概一分钟GitHub Action自动部署完成,还需要去多吉云控制台手动刷新cdn缓存,长时间以来,感觉很不方便 + +这里特别感谢博友[永恒末匕](https://likaijian.com/)提供的方案,原方案地址:https://blog.cpen.top/posts/6f7759e0.html + +这个方案虽然可以实现自动刷新的效果,但是有个弊端,也就是多吉云key令牌会直接暴露出来,只能把仓库设置为私有,不太灵活,今天下午花了点时间完善了一下,理论支持所有部署在GitHub的静态网站 + +## 快速食用 + +在你的项目的根目录,新建一个`RefreshCDN.py`的文件,代码如下 + +```python +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。 + :param data: POST 的数据,字典格式。 + :param json_mode: 数据 data 是否以 JSON 格式请求,默认为 false 则使用表单形式。 + + :return dict: 返回的数据。 + """ + access_key = os.environ.get('DOGECLOUD_ACCESS_KEY') # 从环境变量获取 AccessKey + secret_key = os.environ.get('DOGECLOUD_SECRET_KEY') # 从环境变量获取 SecretKey + + if not access_key or not secret_key: + raise ValueError("Access Key or Secret Key is not set in environment variables.") + + 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 列表,使用逗号分隔 +url_list_env = os.environ.get('CDN_URL_LIST') +if not url_list_env: + raise ValueError("CDN_URL_LIST is not set in environment variables.") + +url_list = url_list_env.split(',') + +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']) +``` + +创建你的 GitHub Actions 脚本,在项目根目录创建 `.github/workflows` 目录,如果有该目录请忽略 + +接下来在`.github/workflows` 目录下新建 `RefreshCDN.yml`文件 + +yml文件配置如下 + +```yml +name: Refresh CDN + +on: + push: + branches: + - main + +jobs: + refresh-cdn: + runs-on: ubuntu-latest + steps: + - name: 检出代码 + uses: actions/checkout@v2 + + - name: 安装 Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + - name: 安装依赖 + run: pip install requests + + - name: 等待源站部署 + run: sleep 1m # 这里用了个笨办法,等待 1 分钟后进行刷新 + + - name: 刷新 CDN + run: python RefreshCDN.py + env: + DOGECLOUD_ACCESS_KEY: ${{ secrets.DOGECLOUD_ACCESS_KEY }} + DOGECLOUD_SECRET_KEY: ${{ secrets.DOGECLOUD_SECRET_KEY }} + CDN_URL_LIST: ${{ secrets.CDN_URL_LIST }} +``` + +然后在你的GitHub项目仓库页面 + +依次进入 `Settings` -> `Secrets and variables` -> `Actions` -> `New repository secret` + +创建三个密钥键值对,分别为 + +CDN_URL_LIST:值为 URL 列表,使用逗号分隔。 + +``` +https://usj.cc/,https://5b2.cn/ +``` + +`DOGECLOUD_ACCESS_KEY`:填入你的 AccessKey。 + +`DOGECLOUD_SECRET_KEY`:填入你的 SecretKey。 + +多吉云密钥获取请访问:https://console.dogecloud.com/user/keys + +按照如上步骤,设置完毕,你的仓库得有如下三个字段 + + + +执行git提交,脚本就可以自动刷新多吉云缓存了 + + + +去多吉云控制台看看刷新预热,这样就是成功了 diff --git a/public/about/index.html b/public/about/index.html index dc9e8467..93154fc6 100644 --- a/public/about/index.html +++ b/public/about/index.html @@ -79,6 +79,17 @@ var option; var dataMap = new Map(); + var key = "2024-09-16"; + var value = dataMap.get(key); + var link = "/post/dogeyun-auto-refresh-cdn/"; + var title = "Hugo使用GitHub Action自动刷新多吉云CDN缓存"; + + + if (value == null) { + dataMap.set(key, [{link, title}]); + } else { + value.push({link, title}); + } var key = "2024-08-11"; var value = dataMap.get(key); var link = "/post/migrate-to-hugo/"; diff --git a/public/archives/index.html b/public/archives/index.html index eeeebea2..da214425 100644 --- a/public/archives/index.html +++ b/public/archives/index.html @@ -83,6 +83,28 @@ +
博客目前使用的方案是多吉云全站cdn缓存,使用git将hugo文件推送到github
+大概一分钟GitHub Action自动部署完成,还需要去多吉云控制台手动刷新cdn缓存,长时间以来,感觉很不方便
+这里特别感谢博友 + +永恒末匕提供的方案,原方案地址:https://blog.cpen.top/posts/6f7759e0.html
+这个方案虽然可以实现自动刷新的效果,但是有个弊端,也就是多吉云key令牌会直接暴露出来,只能把仓库设置为私有,不太灵活,今天下午花了点时间完善了一下,理论支持所有部署在GitHub的静态网站
+在你的项目的根目录,新建一个RefreshCDN.py的文件,代码如下
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。
+ :param data: POST 的数据,字典格式。
+ :param json_mode: 数据 data 是否以 JSON 格式请求,默认为 false 则使用表单形式。
+
+ :return dict: 返回的数据。
+ """
+ access_key = os.environ.get('DOGECLOUD_ACCESS_KEY') # 从环境变量获取 AccessKey
+ secret_key = os.environ.get('DOGECLOUD_SECRET_KEY') # 从环境变量获取 SecretKey
+
+ if not access_key or not secret_key:
+ raise ValueError("Access Key or Secret Key is not set in environment variables.")
+
+ 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 列表,使用逗号分隔
+url_list_env = os.environ.get('CDN_URL_LIST')
+if not url_list_env:
+ raise ValueError("CDN_URL_LIST is not set in environment variables.")
+
+url_list = url_list_env.split(',')
+
+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'])
+创建你的 GitHub Actions 脚本,在项目根目录创建 .github/workflows 目录,如果有该目录请忽略
接下来在.github/workflows 目录下新建 RefreshCDN.yml文件
yml文件配置如下
+name: Refresh CDN
+
+on:
+ push:
+ branches:
+ - main
+
+jobs:
+ refresh-cdn:
+ runs-on: ubuntu-latest
+ steps:
+ - name: 检出代码
+ uses: actions/checkout@v2
+
+ - name: 安装 Python
+ uses: actions/setup-python@v2
+ with:
+ python-version: '3.x'
+
+ - name: 安装依赖
+ run: pip install requests
+
+ - name: 等待源站部署
+ run: sleep 1m # 这里用了个笨办法,等待 1 分钟后进行刷新
+
+ - name: 刷新 CDN
+ run: python RefreshCDN.py
+ env:
+ DOGECLOUD_ACCESS_KEY: ${{ secrets.DOGECLOUD_ACCESS_KEY }}
+ DOGECLOUD_SECRET_KEY: ${{ secrets.DOGECLOUD_SECRET_KEY }}
+ CDN_URL_LIST: ${{ secrets.CDN_URL_LIST }}
+然后在你的GitHub项目仓库页面
+依次进入 Settings -> Secrets and variables -> Actions -> New repository secret
创建三个密钥键值对,分别为
+CDN_URL_LIST:值为 URL 列表,使用逗号分隔。
+https://usj.cc/,https://5b2.cn/
+DOGECLOUD_ACCESS_KEY:填入你的 AccessKey。
DOGECLOUD_SECRET_KEY:填入你的 SecretKey。
多吉云密钥获取请访问:https://console.dogecloud.com/user/keys
+按照如上步骤,设置完毕,你的仓库得有如下三个字段
+
+
执行git提交,脚本就可以自动刷新多吉云缓存了
+
+
去多吉云控制台看看刷新预热,这样就是成功了
+]]>Lw%+ 2UK1vcO(nemLgL=K
z2_NJAUM2EAuurXa#GScBly@5(9%FzzuH_~od#k4dWrUE}Jns%X4JC^EuR1tPcwu@H
z=8LeiRK?eyOkH_j-BMQgs`84BvhAgzGG>W)HJ)L%pbe!bI0`DP*-d>n5{>kr4Pe
zJiTy23hBrs1kX(bW*~3gb<&PJQ3-m3q%iGhB@EhfZ!WtztAdpvhgYKYLf;Chx3LED
z8s0@@Gat`1tUsM&>*Q_-#I}`-L4`6AorNl_&Lqsp_NWh%AMAJURi
zBdhLZ*Kb;fE%CeF0)omMb&)ov7k3Ha7W$@m=yvPa%xK%(#Cx{M_FiM>4w<7Hff6G>
zddnFw(x#1e-EO+NA9At6q<_9u5ITB_eVF-%+qSIE-m2dQaEgGA7kr^S5~obn?och;
zXux}Ec(nk4vA5S#+Umnb+2O`s)1|ffji_l8$FGxHwh8MM7PbciHCXN2(f!^BrPpfZ
zx2G#vBKi{w@bi1{re0!G8R_doK;Rd)g4Y
2c*Np$JLjkMDJP*$h{0MvyqONW)xqma%f5r#80FkQv7Nl
~ixk