使用github action自动化编译并部署至服务器

本文主要对github的action做一次备忘,方便以后查阅

进入github仓库 actions 页面

image-20220511190636223

选择 set up a workflow yourself

image-20220511190721414

名称随意,我这里设置为ci.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
name: Build app and deploy to tencent
on:
#监听master分支的push操作
push:
branches:
- master
jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Install Node.js
uses: actions/setup-node@v1
with:
node-version: '16.13.1'
- name: Install yarn #安装包管理器,我这里使用的是yarn
run: npm install -g yarn
- name: Install yarn dependencies
run: yarn install #安装依赖
- name: Run build task
run: yarn run build #编译
- name: Deploy to Server
uses: easingthemes/ssh-deploy@v2.1.5
env:
SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }} #使用action中的secret
ARGS: '-rltgoDzvO --delete'
SOURCE: public # 这是要复制到云静态服务器的文件夹名称
REMOTE_HOST: '124.220.xx.xx' # 你的服务器公网地址,如果仓库为公开需要写入secret中,注意隐藏
REMOTE_USER: root # 登录后默认为 root 用户,并且所在文件夹为 root
TARGET: /www/wwwroot/hexo/ # 打包后的 public 文件夹

ci.yml中的secrets进行设置。Name为 SERVER_SSH_KEY,Value为ssh私钥,同时注意将公钥上传至服务器。这里不再展开。

image-20220511191258602

至此,配置完成。workflow会自动工作,失败时会得到邮件通知。