github 将整个文件夹推送到自己的仓库:https://blog.csdn.net/viafcccy/article/details/85527118

前提 

node.js 环境 和 git 都已正确安装 (其实能正常运行 hexo 就已经说明正确安装了)

 hexo 可以正常运行 可以正常部署(这里介绍部署到 github pages )

  • 配置好 hexo 的主题,博客名称等等

    ok,有了以上前提,可以继续了

_config.yml 文件中在前提情况下,已经配置好了:

1
2
3
4
deploy:
type: git
repo: https://github.com/用户名/仓库名.git
branch: master

此时我们需要将上面 repo 的配置改成 ssh 格式——即 git@github.com: 用户名 / 仓库名. git

避免在执行 actions 时 部署出错

随便在任何文件位置可以直接右键 git bash here

复制粘贴这个 ssh-keygen -t rsa -b 4096 -C “Hexo Deploy Key” -f github-deploy-key -N “”

会在当前目录生成两个文件

github-deploy-key —— 私钥

github-deploy-key.pub —— 公钥

自行新建一个私人仓库来存放 hexo 源文件

  1. thmblog
    • 然后访问私人代码仓库 Settings -> Secrets , New secret
    • 在 Value 填入 github-deploy-key(私钥)  中的内容
    • Name 填写 HEXO_DEPLOY_KEY 注意大小写,这个后面的 GitHub Actions Workflow 要用到,一定不能写错。
  2. github pages
    • 访问 github pages 对应的代码仓库 Settings -> Deploy keys,Add deploy key
    • Title:HEXO_DEPLOY_PUB 可自定义名字
    • 在 Key 填入 github-deploy-key.pub(公钥)中的内容
    • Allow write access 一定要勾上
  3. 在私人代码仓库里点 Actions

然后创建一个新文件 .github/workflows/deploy.yml

deploy 名字可以自取但是一定要放在.github/workflows 目录中

deploy.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

name: Hexo Deploy
# 触发 workflow 的事件
on:
push:
branches:
- master
# 一个workflow由执行的一项或多项job
jobs:
# 一个job任务,任务名为build
build:
#运行在最新版ubuntu系统中
runs-on: ubuntu-latest
env:
TZ: Asia/Shanghai
#步骤合集
steps:
#新建一个名为checkout source的步骤
- name: Checkout source
#使用checkout@v2这个action获取源码
uses: actions/checkout@v2
#使用建一个名为setup-node的步骤
- name: Setup Node.js
#使用setup-node@v1这个action
uses: actions/setup-node@v3
#指定某个action 可能需要输入的参数
with:
node-version: '16.x'

- name: Setup Hexo
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
#执行执行某个shell命令或脚本
run: |
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "thmbuddrist@gmail.com"
git config --global user.name "thmBuddrist"
npm install hexo-cli -g
npm install
- name: Deploy
run: |
hexo clean
hexo g
hexo deploy
- 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/thmblog/"

3.1.2 通过SSH方式

进入仓库-Settings-Secrets-New secret,分别新建三条名为SERVER_SSH_KEYSERVER_HOSTSERVER_USER,值为SSH私钥、SSH地址和SSH用户名的secret,如需更多配置例如端口号等可以参考ssh-deploy

将以下文件追加到仓库\.github\workflows\main.yml文件下后提交,注意缩进,- name要和上边的- name对齐。

1
2
3
4
5
6
7
8
9
10
- name: Deploy
- 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/thmblog/"
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
name: hello-github-actions
# 触发 workflow 的事件
on:
push:
# 分支随意
branches:
- master
# 一个workflow由执行的一项或多项job
jobs:
# 一个job任务,任务名为build
build:
#运行在最新版ubuntu系统中
runs-on: ubuntu-latest
#步骤合集
steps:
#新建一个名为checkout_actions的步骤
- name: checkout_actions
#使用checkout@v2这个action获取源码
uses: actions/checkout@v2
#使用建一个名为setup-node的步骤
- name: setup-node
#使用setup-node@v1这个action
uses: actions/setup-node@v1
#指定某个action 可能需要输入的参数
with:
node-version: '14'
- name: npm install and build
#执行执行某个shell命令或脚本
run: |
npm install
npm run build
- name: commit push
#执行执行某个shell命令或脚本
run: |
git config --global user.email xxx@163.com
git config --global user.name xxxx
git add .
git commit -m "update" -a
git push
# 环境变量
env:
email: xxx@163.com

ok,这样就完美搞定 github action 和 GitHub pages 的连接啦,并且可以自动触发 Workflow 执行动作

俺是一个纯小白,只能傻瓜式的推送部署到仓库了

  • 在任意位置 git bash here 然后 输入 git clone https://github.com / 用户名 / 仓库名. git 将私人仓库给克隆下来

  • 将所有的 hexo 文件都复制到刚刚克隆下来的文件夹里面

  • 然后  git init 将该克隆下的文件夹变成 Git 可以管理的仓库

  • git add . 通过 git add 将所有文件提交到暂存区

  • git commit -m ‘the initial edition’ 版本描述

  • git remote add origin https://github.com / 用户名 / 仓库名. git 与仓库关联

  • git pull 第一次推送需要

  • git push -u origin master 带有 - u 这个参数是指,将 master 分支的所有内容都提交, 第一次关联之后后边你再提交就可以不用这个参数了

  • git push origin master 之后你的每一次修改,你就可以只将你修改用这个 push 就好了

    此时,所有的 hexo 文件全都 git 到仓库了,之后就可以直接在私人仓库 source/_posts / 里面添加. md 文件啦,可以随时随地写文章发文章,不受设备和配置环境干扰啦。

俺是一个纯小白,都是一步一步按照别人的步骤踩坑摸索来的,不容易呜呜呜。