GitHub Actions 自动发布Hexo 并通过 FTP上传 到阿里云ECS

引言

​ 手动发布Hexo到阿里云ECS非常麻烦,hexo g当文章量多时,较为耗时,并且每次手动上传极为不便,本文通过GitHub Actions 完成自动部署Hexo到阿里云ECS,本文通过FTP的方式上传到阿里云ECS。

添加 Actions

在仓库文件夹新建 .github/workflows/deploy-hexo.yml,或者点击 GitHub -> Actions,新建 Action

deploy-hexo.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
on: push
name: 🚀 Deploy website on push
jobs:
web-deploy:
name: 🎉 Deploy
runs-on: ubuntu-latest
steps:
- name: 🚚 Get latest code
uses: actions/checkout@v2.3.2

- name: Use Node.js 12
uses: actions/setup-node@v2-beta
with:
node-version: '12'

- name: Install Pandoc
run: |
sudo apt-get install pandoc

- name: 🔨 Build Project
run: |
npm install hexo-cli -g
npm install
hexo clean
hexo generate

- name: 📂 Sync files
uses: SamKirkland/FTP-Deploy-Action@4.0.0
with:
server: ${{ secrets.FTP_SERVER }}
username: ${{ secrets.FTP_USERNAME }}
password: ${{ secrets.FTP_PASSWORD }}
local-dir: ./public/

注意:

local-dir 为本地Hexo生成的部署文件所在文件夹

若Web服务部署路径不是FTP所在根目录,则需指定 server-dir

文档: https://github.com/marketplace/actions/ftp-deploy#settings

添加 Actions secrets

在此仓库 -> Settings -> Secrets 中添加FTP信息

FTP_USERNAME 为FTP用户名

FTP_SERVER 为FTP地址

FTP_PASSWORD 为FTP密码

其 secret value 填相应值

PS:GitHub 会自动将secret名转为大写

git push 触发一次,查看Actions,完成

补充

FTP 连接失败

通过WinSCP 连接FTP失败

解决如下:

  1. 进入高级设置

    1. 取消勾选 Passive mode,点击OK
    1. 点击 Save,再次 Login 即可进入

注意:这方法治标不治本,由于所使用的GitHub Actions FTP包不支持主动模式,仅支持被动模式(passive mode),所以建议不要使用此方法,此方法仅针对本地开发机,如果被动模式无法连接,可尝试放行端口,下面放行被动模式所需要的随机端口。

首先必须在阿里云ECS - 安全组放行 21端口,这是FTP协议默认端口,必须放行,注意:出入方向都要放行

然后,查看 FileZilla Server 的被动模式端口

由上图,发现被动模式端口为 3000-4000,因此需要放行此范围的端口

补充:

3000/4000 代表 <开始端口>/<结束端口>,即 3000到4000,同上,出入方向都要放行

阿里云ECS-添加安全组规则:https://help.aliyun.com/document_detail/25471.html

放行完成后,再次以passive mode连接,连接成功。

GitHub Actions 报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
INFO  Validating config
INFO Validating config
INFO Start processing
FATAL {
err: Error:
Error: R][hexo-renderer-pandoc] On /home/runner/work/notebook/notebook/source/_posts/2021-miniapp-report.md
Error: R][hexo-renderer-pandoc] pandoc exited with code null.
at Hexo.pandocRenderer (/home/runner/work/notebook/notebook/node_modules/hexo-renderer-pandoc/index.js:114:11)
at Hexo.tryCatcher (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/util.js:16:23)
at Hexo.<anonymous> (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/method.js:15:34)
at /home/runner/work/notebook/notebook/node_modules/hexo/lib/hexo/render.js:75:22
at tryCatcher (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromiseCtx (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/promise.js:641:10)
at _drainQueueStep (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/async.js:97:12)
at _drainQueue (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/runner/work/notebook/notebook/node_modules/bluebird/js/release/async.js:15:14)
at processImmediate (internal/timers.js:461:21)
} Something's wrong. Maybe you can find the solution here: %s https://hexo.io/docs/troubleshooting.html
Error: Process completed with exit code 2.

原因:由于需要让Hexo支持 LateX 数学公式渲染,所以使用了 hexo-renderer-pandoc 替换了hexo默认的markdown渲染引擎,而我本地电脑已安装了 pandoc,而 GitHub Actions环境没有 pandoc,所以导致报错。

hexo-deploy.yml 中,此步骤即用于在 Ubuntu 中安装 pandoc

1
2
3
- name: Install Pandoc
run: |
sudo apt-get install pandoc

GitHub Actions FTP 上传失败

这是由于此GitHub Actions (FTP-Deploy-Action)使用了被动模式连接,参考上文配置好被动模式连接即可

使用 另一个 FTP GitHub Action

https://github.com/airvzxf/ftp-deployment-action

deploy-hexo.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
on: push
name: 🚀 Deploy website on push
jobs:
web-deploy:
name: 🎉 Deploy
runs-on: ubuntu-latest
steps:
- name: 🚚 Get latest code
uses: actions/checkout@v2.3.2

- name: Use Node.js 12
uses: actions/setup-node@v2-beta
with:
node-version: '12'

- name: Install Pandoc
run: |
sudo apt-get install pandoc

- name: 🔨 Build Project
run: |
npm install hexo-cli -g
npm install
hexo clean
hexo generate

- name: 📂 Sync files
uses: airvzxf/ftp-deployment-action@latest
with:
server: ${{ secrets.FTP_SERVER }}
user: ${{ secrets.FTP_USERNAME }}
password: ${{ secrets.FTP_PASSWORD }}
local_dir: "./public"
delete: "true"

使用 SSH 上传

参考:

main.yml
1
2
3
4
5
6
7
8
9
- name: 部署到云服务器
uses: easingthemes/ssh-deploy@main
env:
SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
ARGS: "-avzr --delete"
SOURCE: "./public/"
REMOTE_HOST: ${{ secrets.SERVER_HOST }}
REMOTE_USER: ${{ secrets.SERVER_USER }}
TARGET: "/www/wwwroot/note.moeci.com/"

GitHub Actions 中配置 Git

参考:

main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- name: 配置Git部署私钥
env:
HEXO_DEPLOY_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$HEXO_DEPLOY_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
ssh-keyscan gitee.com >> ~/.ssh/known_hosts

- name: 配置Git信息
run: |
git config --global user.name 'yiyungent'
git config --global user.email 'i@moeci.com'

参考

感谢帮助!