-
Notifications
You must be signed in to change notification settings - Fork 2
65 lines (57 loc) · 2.1 KB
/
deploy_api.yaml
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
name: Deploy api service
on:
# Manually triggered event
workflow_dispatch:
jobs:
build:
name: Build and deploy
runs-on: ubuntu-latest
environment:
name: prod
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.20' # 设置为 1.x 以自动获取最新的 Go 1.x 系列版本
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Build Go application
run: |
BINARY_FILENAME=api-$(date +'%Y%m%d-%H%M%S')
echo "BINARY_FILENAME=$BINARY_FILENAME" >> $GITHUB_ENV
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o $BINARY_FILENAME internal/app/cmd/main.go
- name: Copy files to the server
uses: appleboy/[email protected]
with:
host: ${{ vars.SERVER_HOST }}
username: ${{ vars.SERVER_USERNAME }}
key: ${{ secrets.SERVER_SSH_KEY }}
passphrase: ${{ secrets.SSH_KEY_PASSPHRASE }}
port: 22
source: ${{ env.BINARY_FILENAME }} # 修改为你的应用二进制文件的路径
target: "${{ vars.APPS_DIRECTORY }}/api" # 更改为服务器上的目标目录
- name: Clean old releases and restart application with PM2
uses: appleboy/ssh-action@master
with:
host: ${{ vars.SERVER_HOST }}
username: ${{ vars.SERVER_USERNAME }}
key: ${{ secrets.SERVER_SSH_KEY }}
passphrase: ${{ secrets.SSH_KEY_PASSPHRASE }}
port: 22
script: |
# 删除超过5个版本的旧二进制文件
cd "${{ vars.APPS_DIRECTORY }}/api"
ls -t api-* | tail -n +6 | xargs -I {} rm -- {}
# copy to decert-api
cp -f ${{ env.BINARY_FILENAME }} decert-api
# 使用 pm2 重启应用
pm2 reload pm2.json