# Github-Action自动部署到阿里云
Github Action实现功能:每次代码push到仓库,自动打包发布到域名下
部署环境: (github/阿里云ECS服务器:ubuntu18.04)
# 私钥配置与yml编写
# 创建repo
# 连接服务器创建密钥
$ ssh root@47.56.13.69 // 连接远程服务器,以root用户登录,也可以adduser添加用户
- 输入服务器密码:******
$ cd ~ // 也可以省略,默认就在根目录
$ ssh-keygen -t rsa -C autodeployment -f deployment // 生成公钥与私钥
$ cat deployment.pub >> ~/.ssh/authorized_keys // 将公钥写入.ssh/authorized_keys
$ vim ~/.ssh/authorized_keys // vim查看,此时authorized_keys会多出密钥
$ vim deployment // 拷贝deployment内容到github
1
2
3
4
5
6
7
2
3
4
5
6
7
# github私钥
进入代码仓库 => Settigngs => 左侧Secrets => 复制私钥
# github action工作流创建
因为是Node.js项目,所以选中Node.js > Set up this workflow
# yml配置
# 可以按需求修改,比如push,pull_requesy某个分支时执行CI
# 因为我是yarn,如果npm就直接使用npm
# 某个步骤报错,action执行的时候也可能显示通过图标,小心踩坑
name: Node.js CI
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
# 如果写了测试用例,也可以自动测试
- run: yarn install
- run: yarn build
env:
CI: true
- name: Deploy
uses: easingthemes/ssh-deploy@v2.0.7
env:
# Private Key secrets.ALIYUN就是刚才配置的密钥名
SSH_PRIVATE_KEY: ${{ secrets.ALIYUN }}
# For any initial/required rsync flags
ARGS: "-rltgoDzvO --delete"
# Source directory dist/ 需要到远端服务器的文件目录 **填入自己的打包路径**
SOURCE: "dist/"
# Remote host **填入自己的域名**
REMOTE_HOST: "47.56.13.69"
# Remote user **填入自己的用户,一般都是root用户**
REMOTE_USER: "root"
# Target directory 此处填写的是我博客域名下的action pacth下 **填入自己的域名路径**
TARGET: "/www/wwwroot/sineava.top/action/"
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
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
# CI完成
# 服务器
此时进入域名下,就会发现多了自动打包的文件,不过此时直接域名访问会报错 => vue项目还缺vue.config.js配置
# vue.config.js
module.exports = {
publicPath: process.env.NODE_ENV == 'production' ? './' : './',
outputDir: 'dist',
indexPath: 'index.html',
filenameHashing: true,
lintOnSave: process.env.NODE_ENV === 'production',
runtimeCompiler: false,
productionSourceMap: true
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
然后push到github,会自动执行工作流 => 因为刚才创建了github工作流,所以别忘了先pull
# CI再次完成
此时域名就可以访问到了