Node.js + GitHub Actions 自动刷新 CDN

注意
本文最后更新于 2023-12-08,文中内容可能已过时。
问题
腾讯云云函数从 2022 年 6 月 1 日开始收费了,差不多每个月 12 块,对于可能几个月都用不上一次云函数刷新缓存的人来说,有点太贵了。 使用 Node.js 和 GitHub Actions 刷新 CDN 的方式,可以节省不少钱。

1 安装依赖

1
npm install qcloud-cdn-node-sdk

2 编写脚本

1
vim .scripts/qcloudcdn.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * Refresh Qcloud CDN cache
 * @command `node qcloudcdn.js $SECRET_ID $SECRET_KEY`
 */
const qcloudSDK = require('qcloud-cdn-node-sdk');

// Get the config from https://console.qcloud.com/capi
qcloudSDK.config({
  secretId: process?.argv[2],
  secretKey: process?.argv[3]
});

qcloudSDK.request(
  'RefreshCdnDir',
  {
    // See https://cloud.tencent.com/document/api/228/3947
    'dirs.0': 'https://lruihao.cn/'
  },
  (res) => {
    res.code && console.log(res);
  }
);

3 增加快捷指令

打开 package.json 增加 scripts:

1
2
3
4
5
{
  "scripts": {
    "qcloudcdn": "node .scripts/qcloudcdn.js $SECRET_ID $SECRET_KEY"
  }
}

运行方式:

1
SECRET_ID=<secretId> SECRET_KEY=<secretKey> npm run qcloudcdn
Mac OS 环境变量配置(可选)
1
vim ~/.bash_profile
1
2
3
# Qcloud secret key-value
export SECRET_ID=<secretId>
export SECRET_KEY=<secretKey>
1
source ~/.bash_profile

然后,在本地可简化指令为 npm run qcloudcdn 或者 yarn qcloudcdn
等同于 SECRET_ID=$SECRET_ID SECRET_KEY=$SECRET_KEY npm run qcloudcdn

4 配置 GitHub Actions

在原有 GitHub Actions 中部署后增加一个步骤:

1
2
3
4
5
- name: Refresh Qcloud CDN cache
  env:
    SECRET_ID: ${{ secrets.SECRET_ID }}
    SECRET_KEY: ${{ secrets.SECRET_KEY }}
  run: npm run qcloudcdn

在仓库 Settings > Secrets > Actions 中增加两个 Secret: SECRET_IDSECRET_KEY,原来有配置过的就不用了,比如,我原来的叫 COS_SECRET_IDCOS_SECRET_ID,那修改上面配置 env 中的变量名即可。

最后上传代码,以后更新仓库时就会自动刷新 CDN 缓存了。

5 参考资料

本方案使用的旧的 API 请求方式,如果失效,可以参考新的 API,见 SDK 中心

相关内容

Buy me a coffee~
Lruihao 支付宝支付宝
Lruihao 微信微信
0%