之前一直是使用 hexo 的静态网站,同时部署到腾讯云 cos,hexo 有上传到 cos 的插件,hugo 没有,于是利用了新出的 github action 功能,在网上抄到了代码就直接写上去了
文章前缀转换
这个地方其实还有点麻烦,我之前用的是 solo blog,它可以导出 hexo 格式的 markdown,头部格式和其他人的 hexo 有点不一样,在网上找到的转换脚本都不能用,于是只能改一下,最终找到一个 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
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
104
105
106
107
108
109
110
111
| // yaml_to_toml.js
const readline = require("readline");
const fs = require("fs");
const os = require("os");
const moment = require("moment-timezone"); // 需要通过 npm install moment-timezone 来安装
const timezone = "Asia/Shanghai"; // 时区
const src = "hexo-post"; // hexo .md 文件源目录
const target = "../content/posts"; // 目标文件目录
// 开始转换
readDir();
// 遍历目录
function readDir() {
// read all files in src
fs.readdir(src, function (err, files) {
files.map((filename) => {
// get the file extension
const extension = filename.substr(
filename.lastIndexOf(".", filename.length)
);
if (extension === ".md") {
readFile(`${filename}`);
}
});
});
}
function readFile(filename) {
fs.readFile(`${src}/${filename}`, { encoding: "utf8" }, function (err, data) {
if (err) {
return console.log("err: ", err);
}
const content = data.split("---");
const head = content[0].split("\n");
// console.log('head: ', head);
let newHead = head.map((item, index) => {
// console.log('slpitHead: ', slpitHead(item, index, head));
return slpitHead(item, index, head);
});
newHead = newHead.filter((item) => {
return item;
});
// console.log('newHead: ', newHead);
const newContent = `+++${os.EOL}${newHead.join(os.EOL)}${os.EOL}${
os.EOL
}+++${os.EOL}${content[1]}`;
fs.writeFile(
`${target}/${filename}`,
newContent,
{
encoding: "utf8",
},
function (err) {
if (err) {
throw err;
}
console.log(`${filename} 生成成功!`);
}
);
});
}
function slpitHead(item, index, head) {
// title
if (item.indexOf("title:") !== -1) {
return `title = "${item.split("title:")[1].trim()}"`;
}
// date
if (item.indexOf("date:") !== -1) {
// console.log((moment.tz(item.split('date:')[1], timezone)).format());
let hexo_date = item.split("date:")[1];
hexo_date = hexo_date.replace(" ", "").replace("'", "").replace("'", "");
return `date = "${moment.tz(hexo_date, timezone).format()}"`;
}
// tags
if (item.indexOf("tags:") !== -1) {
const tags = [];
let tags0 = item
.split("tags:")[1]
.replace(" ", "")
.replace("[", "")
.replace("]", "")
.split(", ");
for (let i = 0; i < tags0.length; i++) {
tags.push(tags0[i]);
}
return `tags = ${JSON.stringify(tags)}`;
}
// categories
if (item.indexOf("categories:") !== -1) {
const categories = [];
for (let i = index + 1; i < head.length; i++) {
if (head[i].indexOf("-") !== -1) {
categories.push(head[i].split("-")[1].trim());
} else {
break;
}
}
// console.log('categories: ', categories);
return `categories = ${JSON.stringify(categories)}`;
}
return false;
}
|
使用 github action 部署到 cos
这个地方就是原封不动抄别人的代码了。出处,由于代码里面会包含自己的部署密钥 appkey,我就把托管在 github 上的 hugo 博客项目改成私有的了。
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
| name: Deloy to COS
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Go
uses: actions/setup-go@v1.0.0
with:
version: 1.13
- name: Install Hugo
run: |
export GOPATH=$HOME
mkdir $HOME/src
cd $HOME/src
git clone https://github.com/gohugoio/hugo.git
cd hugo
go install
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.6
- name: Install dependencies
run: |
python -m pip install --upgrade pip
- name: Install coscmd
run: |
pip install coscmd
- name: deploy
run: |
$HOME/bin/hugo
coscmd config -a **腾讯云AccessId** -s **腾讯云SecretKey** -b **桶名称** -r **桶地区** -m 10
coscmd upload -r -s public/ /
|