Skip to content

Deploy api service

Deploy api service #15

Workflow file for this run

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