使用GitHub Actions 实现自动化部署


注意:

1、本次操作的都是源码仓库,你首先需要一个仓库来存放你的源码

2、部署到github pages和服务器选择需要的就行,不需要的可以删除掉

3、部署完成之后,github pages有个生效时间,多刷新几次

4、如果使用了coding双线部署和又拍云的CDN加速,发现部署了之后页面不刷新,请检查serverless是否生效,是否正常运行。

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
# 当有改动推送到master分支时,启动Action
name: 自动部署

on:
push:
branches:
- master #2020年10月后github新建仓库默认分支改为main,注意更改

release:
types:
- published

jobs:
checkout:
runs-on: ubuntu-latest
steps:
- name: 检查分支
uses: actions/checkout@v2
with:
ref: master #2020年10月后github新建仓库默认分支改为main,注意更改

- name: 安装 Node
uses: actions/setup-node@v1
with:
node-version: "12.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 --save

- name: 生成静态文件
run: |
hexo clean
hexo generate

- name: 部署到github pages
run: |
git config --global user.name "ConstOwn"
git config --global user.email "xxxxxx@foxmail.com"
# git clone https://github.com/xxxxx/xxxx.github.io.git .deploy_git
# 此处务必用HTTPS链接。SSH链接可能有权限报错的隐患
# =====注意.deploy_git前面有个空格=====
# 这行指令的目的是clone博客静态文件仓库,防止Hexo推送时覆盖整个静态文件仓库,而是只推送有更改的文件
# 我注释掉了是为了刷新整个仓库,也可以选择不注释掉,但是可能出现没有识别到的情况
hexo deploy

- name: 部署到云服务器
uses: cross-the-world/scp-pipeline@master
with:
host: ${{ secrets.USER_HOST }}
user: ${{ secrets.USER_NAME }}
pass: ${{ secrets.USER_PASS }}
connect_timeout: 10s
local: './.deploy_git/*'
remote: xxxxxx