MOXUY

Feb 20, 2023

使用GithubAction自动部署

1.在服务器生成ssh密钥对 教程

2.将密钥添加到仓库的Secrets中
代码仓库 - Settings - Secrets - Actions
添加完成后就可以在action中使用了

3.部署代码示例

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
name: Build and Deploy

on:
push:
branches:
- main

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@v3
# 缓存依赖
- name: Cache nodemodules
uses: actions/cache@v1
env:
cache-name: cache-node-modules
with:
# 需要缓存的文件的路径
path: ./node_modules
# 对缓存的文件指定的唯一标识
key: ${{ runner.os }}-dist-${{ env.cache-name }}-${{ hashFiles('./package.json') }}
# 用于没有再找目标key的缓存的backup选项
restore-keys: |
${{ runner.os }}-dist-${{ env.cache-name }}-
${{ runner.os }}-dist-
${{ runner.os }}-
# 装依赖
- name: Install
run: yarn
# 打包
- name: Build
run: yarn build
# 上传打包资源
- name: Deploy
uses: AEnterprise/rsync-deploy@v1.0
env:
DEPLOY_KEY: ${{ secrets.SERVER_PRIVATE_KEY }}
ARGS: '-avz --delete'
FOLDER: 'dist/'
SERVER_PORT: ${{ secrets.SERVER_PORT }}
SERVER_IP: ${{ secrets.SERVER_HOST }}
USERNAME: ${{ secrets.USER_NAME }}
SERVER_DESTINATION: ${{secrets.SERVER_DESTINATION}} # 服务器目标路径

tags: 工程 Github
OLDER > < NEWER