前言
将博客静态文件上传至 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 用户
初始化
发布
生成 npm token
npm 官网 => 头像 => Access Tokens => Generate New Token => 勾选 Automation
github 仓库新增 NPM_TOKEN
的 secrets
示例
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@v1 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
|
参考
akilar | github-action推送博客部署仓库至NPM
akilar | npm图床
安知鱼 | hexo博客工作流CI
安知鱼 | npm图床
EtherDream | 文件一键上传到 NPM
npmmirror | 同步模块 cnpm sync cnpmcore
github | CyanBlog/pkgpublish.js