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

推荐订阅源

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-操作记录 06_转载-自我提升的8个好习惯 05_转载-延寿指南-反向面试-文档写作规范 04_转载-提问的智慧 03_转载-中文文案排版指北 11_Hexo-GitHub Actions 自动部署博客 10_Hexo-GitHub Actions 自动刷新多吉云 CDN 缓存 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端环境记录
12_Hexo-GitHub Actions 发布博客静态资源至 NPM
Mycpen · 2023-05-05 · via Mycpen

前言

将博客静态文件上传至 github,通过 github action 自动发包至 npm,使用 npmmirror cdn

npm 命令

网址: https://www.npmjs.com/

切换源

1
2
3
4
5


npm config set registry http://registry.npmmirror.com

npm config set registry https://registry.npmjs.org/

添加本地 npm 用户

1
2
3
4

npm adduser

npm login

初始化

1
npm init
image-20240324233905199

发布

1
npm publish

生成 npm token

npm 官网 => 头像 => Access Tokens => Generate New Token => 勾选 Automation

image-20240324234307284

github 仓库新增 NPM_TOKEN 的 secrets

image-20240325002106180

示例

package.json

scripts/generate_package_json.js,生成 public/package.json

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



hexo.extend.generator.register('npmpackagejson', function(locals){

return {
path: '/package.json',
data: `{
"name": "mycpen-blog",
"version": "0.0.0",
"description": "mycpen blog static file",
"main": "index.js",
"directories": {
"lib": "lib"
},
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mycpen/blog.git"
},
"author": "mycpen",
"license": "ISC",
"bugs": {
"url": "https://github.com/mycpen/blog/issues"
},
"homepage": "https://github.com/mycpen/blog#readme"
}
`};
});

npm push

scripts/npmpublish.js,生成 public/.github/workflows/autopublish.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
34
35
36
37
38
39




hexo.extend.generator.register('npmpush', function(locals){

return {
path: '/.github/workflows/autopublish.yml',
data: `name: Node.js Package
# 监测分支,2020年10月后github新建仓库默认分支改为main,记得更改
on:
push:
branches:
- main

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: "18.x"
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
NODE_AUTH_TOKEN: `+ '${{secrets.npm_token}}' + `

- name: Wait for 3 minutes
run: sleep 180 # 等待 3 分钟,单位为秒

- name: Sync package from npm to npmmirror
run: |
npm install -g cnpm --registry=https://registry.npmmirror.com
cnpm sync mycpen-blog
`
};
});

.npmignore

scripts/npmignore.js,生成 public/.npmignore。目的 不提交 .html 这类文件至 npm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16



hexo.extend.generator.register('npmignore', function(locals){

return {
path: '/.npmignore',
data: `**/*.html
.github/
download/
ads.txt
CNAME
robots.txt
`};
});

change version

.github/replace-localfile-url.py,修改 public/package.json version

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76





import os
import re
from datetime import datetime
import requests
import random





def search(search_path, search_result, search_fileType_list):

all_file = os.listdir(search_path)

for each_file in all_file:

if os.path.isdir(search_path + each_file):

search(search_path + each_file + '/', search_result, search_fileType_list)

else:
for i in search_fileType_list:
if re.findall(f'.*\\{i}$', each_file) == [each_file]:
search_result.append(search_path + each_file)




def replace(replace_file_name, replace_old_str, replace_new_str):
with open(replace_file_name, "r", encoding = "UTF-8") as f1:
content = f1.read()
f1.close()
t = content.replace(replace_old_str, replace_new_str)
with open(replace_file_name, "w", encoding = "UTF-8") as f2:
f2.write(t)
f2.close()







npm_num_mapping = {
'0': ['a', 'b', 'u'], '1': ['c', 'd', 'v'], '2': ['e', 'f', 'w'], '3': ['g', 'h', 'x'],
'4': ['i', 'j', 'y'], '5': ['k', 'l', 'z'], '6': ['m', 'n'], '7': ['o', 'p'],
'8': ['q', 'r'], '9': ['s', 't']
}

npm_now = datetime.now()

npm_formatted_time = npm_now.strftime("%y%m%d%H%M")

npm_converted_time = ''
for char in npm_formatted_time:
if char.isdigit():
npm_converted_time += ''.join(random.choice(npm_num_mapping[char]) if char in npm_num_mapping else char)
else:
npm_converted_time += char


result_npmPackageJson = ['./public/package.json']
old_npmPackageJson_version = '0.0.0'
new_npmPackageJson_version = f'0.0.0-{npm_converted_time}'
count = 0

for file_name in result_npmPackageJson:
replace(file_name, old_npmPackageJson_version, new_npmPackageJson_version)
count += 1
print("{} done {}".format(file_name, count))

.github/workflows/autodeploy.yml,执行上方 py 脚本 - name: Replace localFile URL

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
name: Auto deploy

on:
workflow_dispatch:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 检查分支
uses: actions/checkout@v2
with:
ref: main

- uses: szenius/set-timezone@v1.0
with:
timezoneLinux: "Asia/Shanghai"

- name: 安装 Node
uses: actions/setup-node@v1
with:
node-version: "18.x"

- name: 安装 Hexo
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g

- name: 缓存 Hexo
uses: actions/cache@v4
id: cache
with:
path: node_modules
key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

- name: 安装依赖
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install gulp-cli -g #全局安装gulp
npm install --save

- name: Generate static file
run: |
hexo clean ; hexo generate ; gulp

- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests
- name: Replace imageBed URL
run: |
python .github/replace-imgbed-url-v2.py
- name: Replace localFile URL
run: |
python .github/replace-localfile-url.py

- name: Delete static file repository tags
run: |
mkdir tmp_mycpen
cd tmp_mycpen
git clone https://github.com/mycpen/blog.git
cd blog
git push --force --quiet "https://mycpen:${{ secrets.GH_TOKEN }}@github.com/mycpen/blog.git" --delete $(git tag -l)
cd ../..
rm -rf tmp_mycpen

- name: Deploy static file
run: |
cd ./public
git init
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git config --global user.name 'github-actions[bot]'
git add .
git commit -m "$(date +'%Y/%m/%d')"
tag_name=$(date +'%y.%m.%d')
git tag $tag_name
git push --force --quiet "https://mycpen:${{ secrets.GH_TOKEN }}@github.com/mycpen/blog.git" $tag_name
git push --force --quiet "https://mycpen:${{ secrets.GH_TOKEN }}@github.com/mycpen/blog.git" master:main

- name: Purge JSD cache
run: |
jsd_purge_urls=("https://purge.jsdelivr.net/gh/mycpen/blog/" "https://purge.jsdelivr.net/gh/mycpen/blog@latest/" "https://purge.jsdelivr.net/gh/mycpen/blog@main/")
for url in "${jsd_purge_urls[@]}"; do curl -s $url; done

- name: 推送百度 url
run: |
hexo deploy

- name: Delete workflow runs
uses: Mattraks/delete-workflow-runs@v2
with:
token: ${{ secrets.GH_TOKEN }}
repository: ${{ github.repository }}
retain_days: 30
keep_minimum_runs: 6

参考

  1. akilar | github-action推送博客部署仓库至NPM

  2. akilar | npm图床

  3. 安知鱼 | hexo博客工作流CI

  4. 安知鱼 | npm图床

  5. EtherDream | 文件一键上传到 NPM

  6. npmmirror | 同步模块 cnpm sync cnpmcore

  7. github | CyanBlog/pkgpublish.js

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


欢迎访问 MYCPEN BLOG

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