115 lines
4.5 KiB
Go
115 lines
4.5 KiB
Go
name: Go Build & Release
|
||
|
||
on:
|
||
workflow_dispatch: # 仅支持手动触发
|
||
inputs:
|
||
servicePort: # 手动设置服务启动端口(必填)
|
||
description: '服务启动序号'
|
||
required: true
|
||
type: number
|
||
|
||
jobs:
|
||
# 生成随机版本号(8位字母数字,作为screen会话名核心标识)
|
||
prepare-version:
|
||
runs-on: ubuntu-latest
|
||
outputs:
|
||
build_version: ${{ steps.set-random-version.outputs.version }}
|
||
|
||
steps:
|
||
- name: Set random build version
|
||
id: set-random-version
|
||
run: |
|
||
# 生成8位随机字母数字(大小写混合),确保版本与会话名唯一性
|
||
RANDOM_SUFFIX=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
|
||
echo "version=v$RANDOM_SUFFIX" >> $GITHUB_OUTPUT
|
||
|
||
build:
|
||
needs: prepare-version
|
||
runs-on: ubuntu-latest
|
||
|
||
steps:
|
||
- uses: actions/checkout@v4
|
||
|
||
- name: 缓存Go依赖
|
||
id: go-cache
|
||
uses: actions/cache@v3
|
||
with:
|
||
path: ~/go/pkg/mod
|
||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.mod') }}
|
||
restore-keys: |
|
||
${{ runner.os }}-go-
|
||
|
||
- name: Set up Go
|
||
uses: actions/setup-go@v4
|
||
with:
|
||
go-version: '1.20'
|
||
|
||
- name: 编译logic服务
|
||
run: go build -o ./public/logic_${{ needs.prepare-version.outputs.build_version }} -v ./logic
|
||
|
||
- name: Upload Build Artifact
|
||
uses: actions/upload-artifact@v4
|
||
with:
|
||
name: logic_${{ needs.prepare-version.outputs.build_version }}
|
||
path: ./public/logic_${{ needs.prepare-version.outputs.build_version }}
|
||
|
||
- name: 推送到服务器并通过screen启动服务(会话名无端口)
|
||
uses: easingthemes/ssh-deploy@main
|
||
env:
|
||
SSH_PRIVATE_KEY: ${{ secrets.BLAZING }}
|
||
REMOTE_HOST: "122.10.117.123"
|
||
REMOTE_USER: "root"
|
||
SOURCE: "public/"
|
||
TARGET: "/home/"
|
||
ARGS: "-avz --chown=git:git"
|
||
# 核心调整:会话名仅用随机版本号(格式:logic-{版本号},如 logic-v7aB9x2zF)
|
||
SCRIPT_AFTER: |
|
||
# 定义screen会话名(仅含服务标识+随机版本号,无端口)
|
||
SCREEN_NAME="logic-${{ needs.prepare-version.outputs.build_version }}"
|
||
# 停止同名旧会话(仅清理当前版本,不影响其他版本)
|
||
screen -S $SCREEN_NAME -X quit 2>/dev/null || true
|
||
# 启动服务并传入-port参数
|
||
screen -dmS $SCREEN_NAME /home/logic_${{ needs.prepare-version.outputs.build_version }} -port=${{ github.event.inputs.servicePort }}
|
||
# 输出完整关联信息,方便管理(会话名、端口、版本一一对应)
|
||
echo "======================================"
|
||
echo "服务启动成功!"
|
||
echo "Screen会话名:$SCREEN_NAME"
|
||
echo "服务端口:${{ github.event.inputs.servicePort }}"
|
||
echo "对应版本:${{ needs.prepare-version.outputs.build_version }}"
|
||
echo "管理命令:"
|
||
echo " - 进入会话:screen -r $SCREEN_NAME"
|
||
echo " - 查看所有会话:screen -ls"
|
||
echo " - 停止服务:screen -S $SCREEN_NAME -X quit"
|
||
echo "======================================"
|
||
|
||
# 自动创建GitHub Release(同步会话名与端口关联关系)
|
||
create-release:
|
||
needs: [prepare-version, build]
|
||
permissions:
|
||
contents: write
|
||
runs-on: ubuntu-latest
|
||
|
||
steps:
|
||
- name: Download build artifact
|
||
uses: actions/download-artifact@v4
|
||
with:
|
||
name: logic_${{ needs.prepare-version.outputs.build_version }}
|
||
path: ./public
|
||
|
||
- name: Create GitHub Release
|
||
uses: softprops/action-gh-release@v1
|
||
with:
|
||
tag_name: ${{ needs.prepare-version.outputs.build_version }}
|
||
name: Release ${{ needs.prepare-version.outputs.build_version }}
|
||
body: |
|
||
## 自动构建发布
|
||
- 版本号: ${{ needs.prepare-version.outputs.build_version }}
|
||
- 触发方式: 手动触发
|
||
- 服务端口: ${{ github.event.inputs.servicePort }}
|
||
- Screen会话名: logic-${{ needs.prepare-version.outputs.build_version }}
|
||
- 可执行文件: logic_${{ needs.prepare-version.outputs.build_version }}
|
||
- 启动命令: ./logic_${{ needs.prepare-version.outputs.build_version }} -port=${{ github.event.inputs.servicePort }}
|
||
files: ./public/logic_${{ needs.prepare-version.outputs.build_version }}
|
||
draft: false
|
||
prerelease: false
|