Files
GameServerManager/.github/workflows/build.yml
2026-10-02 19:03:32 +08:00

316 lines
12 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Build Package
# 添加权限配置
permissions:
contents: write
packages: write
actions: read
on:
workflow_dispatch:
inputs:
build_linux:
description: "构建Linux版本"
required: false
type: boolean
default: true
build_windows:
description: "构建Windows版本"
required: false
type: boolean
default: true
build_docker:
description: "构建Docker镜像"
required: false
type: boolean
default: false
build_docker_arm:
description: "构建ARM64 Docker镜像"
required: false
type: boolean
default: false
# 仅订阅标签推送事件:在 GitHub 网页上“创建并直接发布 Release”时,
# 会同时产生 push(tag) 和 release(published) 两个事件;若两个触发器并存,
# 同一版本会启动两次构建,并发推送同一 Docker 标签/缓存时互相冲突导致失败。
# 发布 Release(无论是推送标签后由本工作流自动创建,还是网页上新建标签发布)
# 都会产生标签推送事件,因此只保留 push.tags 即可覆盖全部发布场景。
push:
tags:
- 'v*'
jobs:
resolve_version:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
is_stable: ${{ steps.version.outputs.is_stable }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: '22.16.0'
- name: Resolve build version
id: version
# 不再接收 release 事件参数(已移除 release 触发器),
# 脚本会按标签名(GITHUB_REF)自动解析版本号
run: node scripts/resolve-build-version.js
build_linux:
needs: resolve_version
if: github.event.inputs.build_linux == 'true' || github.event_name == 'push'
runs-on: ubuntu-latest
strategy:
matrix:
include:
- arch: x64
package_command: npm run package:linux:x64
artifact_name: gsm3-management-panel-linux-x64-build
- arch: arm64
package_command: npm run package:linux:arm64
artifact_name: gsm3-management-panel-linux-arm64-build
- arch: riscv64
package_command: npm run package:linux:riscv64
artifact_name: gsm3-management-panel-linux-riscv64-build
steps:
- uses: actions/checkout@v4
- name: Setup Node.js for build
uses: actions/setup-node@v4
with:
node-version: '22.16.0'
cache: 'npm'
cache-dependency-path: |
package-lock.json
client/package-lock.json
server/package-lock.json
- name: Install dependencies
run: |
npm ci
npm ci --prefix client
npm ci --prefix server
- name: Build Linux ${{ matrix.arch }} package
run: ${{ matrix.package_command }}
env:
CI: true
NODE_ENV: production
APP_VERSION: ${{ needs.resolve_version.outputs.version }}
VITE_APP_VERSION: ${{ needs.resolve_version.outputs.version }}
- name: Upload Linux ${{ matrix.arch }} artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.artifact_name }}
path: dist/package/
retention-days: 30
build_windows:
needs: resolve_version
if: github.event.inputs.build_windows == 'true' || github.event_name == 'push'
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22.16.0'
cache: 'npm'
cache-dependency-path: |
package-lock.json
client/package-lock.json
server/package-lock.json
- name: Install dependencies
run: |
npm ci
npm ci --prefix client
npm ci --prefix server
- name: Build package
run: npm run package:windows
env:
CI: true
NODE_ENV: production
APP_VERSION: ${{ needs.resolve_version.outputs.version }}
VITE_APP_VERSION: ${{ needs.resolve_version.outputs.version }}
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: gsm3-management-panel-windows-build
path: dist/package/
retention-days: 30
build_docker:
needs: resolve_version
if: github.event.inputs.build_docker == 'true' || github.event_name == 'push'
runs-on: ubuntu-latest
steps:
# 第一步:检出代码
- name: 拉取仓库代码
uses: actions/checkout@v4
# 第二步:设置构建环境
- name: 配置QEMU模拟器
uses: docker/setup-qemu-action@v2
- name: 配置Docker Buildx
uses: docker/setup-buildx-action@v2
with:
driver-opts: network=host
# 第三步:登录所有镜像仓库
- name: 登录Docker仓库
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# 第四步:构建并推送多架构镜像(带缓存功能)
- name: 缓存并构建推送DockerHub多架构镜像
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: |
xiaozhu674/gameservermanager:${{ needs.resolve_version.outputs.version }}
${{ needs.resolve_version.outputs.is_stable == 'true' && 'xiaozhu674/gameservermanager:latest' || '' }}
build-args: |
APP_VERSION=${{ needs.resolve_version.outputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max
build_docker_arm:
needs: resolve_version
if: github.event.inputs.build_docker_arm == 'true'
runs-on: ubuntu-latest
steps:
# 第一步:检出代码
- name: 拉取仓库代码
uses: actions/checkout@v4
# 第二步:设置构建环境
- name: 配置QEMU模拟器
uses: docker/setup-qemu-action@v2
- name: 配置Docker Buildx
uses: docker/setup-buildx-action@v2
with:
driver-opts: network=host
# 第三步:登录Docker仓库
- name: 登录Docker仓库
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# 第四步:构建并推送ARM64镜像
- name: 构建并推送ARM64镜像
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/arm64
push: true
tags: |
xiaozhu674/gameservermanager:${{ needs.resolve_version.outputs.version }}-arm64-beta
build-args: |
APP_VERSION=${{ needs.resolve_version.outputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max
# 创建发布版本(仅在推送标签时触发)
create_release:
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
needs: [resolve_version, build_linux, build_windows]
runs-on: ubuntu-latest
steps:
- name: Download Linux x64 artifact
uses: actions/download-artifact@v4
with:
name: gsm3-management-panel-linux-x64-build
path: linux-x64-build/
- name: Download Linux arm64 artifact
uses: actions/download-artifact@v4
with:
name: gsm3-management-panel-linux-arm64-build
path: linux-arm64-build/
- name: Download Linux riscv64 artifact
uses: actions/download-artifact@v4
with:
name: gsm3-management-panel-linux-riscv64-build
path: linux-riscv64-build/
- name: Download Windows artifact
uses: actions/download-artifact@v4
with:
name: gsm3-management-panel-windows-build
path: windows-build/
# 发布产物文件名固定、不再追加版本号:对外下载地址可以长期不变,
# 新版本发布时同名资产就地覆盖(同一 tag 重跑也会覆盖,天然幂等)。
# 版本信息仍由 tag 与产物内部元数据体现,不写进文件名。
- name: Create platform archives
run: |
tar -czf gsm3-management-panel-linux.tar.gz -C linux-x64-build .
tar -czf gsm3-management-panel-linux-arm64.tar.gz -C linux-arm64-build .
tar -czf gsm3-management-panel-linux-riscv64.tar.gz -C linux-riscv64-build .
cd windows-build
zip -r ../gsm3-management-panel-windows.zip .
- name: Create Release
uses: softprops/action-gh-release@v1
with:
files: |
gsm3-management-panel-linux.tar.gz
gsm3-management-panel-linux-arm64.tar.gz
gsm3-management-panel-linux-riscv64.tar.gz
gsm3-management-panel-windows.zip
generate_release_notes: true
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 上传中继的每个 job 只能传一个文件,这里把 x86_64 的两个产物交接给下面的中继 job。
# 交接的是已经打好包的文件本体,保证 Release 资产与中继分发的字节完全一致。
- name: Upload x86_64 archives for relay
uses: actions/upload-artifact@v4
with:
name: gsm3-management-panel-relay-x64
path: |
gsm3-management-panel-linux.tar.gz
gsm3-management-panel-windows.zip
retention-days: 7
# 上传中继:Release 发布成功后,把 x86_64 的 Windows / Linux 包再分发到中继,供下载站对外提供固定地址。
# 只上传 x86_64;ARM64 等其它架构只保留在 Release,不进中继。
# 统一路径固定、不带版本号:同一路径重复上传即为就地覆盖,这是「固定下载地址」用法的前提。
# 凭证模型(中继 CICD 是 public 仓库,不保存任何中继密钥,密钥一律由调用方持有):
# 1. 中继地址:这里不传 relay-base,由中继 workflow 读取调用方的变量 RELAY_BASE;
# 2. 中继密钥:中继侧 RELAY_ADMIN_KEY / RELAY_UPLOAD_KEY 为必填,必须由调用方传入,
# 建议在 GSManagerXZ 组织级配置 Organization secrets,一处维护、全组织复用;
# 3. 刻意不使用 secrets: inherit:GitHub 只允许「同一组织 / 同一企业」内使用 inherit,
# 中继仓库在个人账号 yxsj245 名下,属于跨容器调用,只能用显式映射;
# 将来中继仓库若迁入同一组织或企业,可改用 secrets: inherit。
# 前置条件:中继仓库 scripts/relay-upload.sh 顶部的调用方白名单需包含本仓库(当前已包含)。
relay_upload_linux:
name: 上传中继-Linux x64
needs: create_release
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
uses: yxsj245/Download_Station_CICD/.github/workflows/relay-upload.yml@v1
with:
artifact-name: gsm3-management-panel-relay-x64
file: gsm3-management-panel-linux.tar.gz
logical-path: '开源项目/GSManager/gsm3-management-panel-linux.tar.gz'
secrets:
RELAY_ADMIN_KEY: ${{ secrets.RELAY_ADMIN_KEY }}
RELAY_UPLOAD_KEY: ${{ secrets.RELAY_UPLOAD_KEY }}
relay_upload_windows:
name: 上传中继-Windows x64
needs: create_release
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
uses: yxsj245/Download_Station_CICD/.github/workflows/relay-upload.yml@v1
with:
artifact-name: gsm3-management-panel-relay-x64
file: gsm3-management-panel-windows.zip
logical-path: '开源项目/GSManager/gsm3-management-panel-windows.zip'
secrets:
RELAY_ADMIN_KEY: ${{ secrets.RELAY_ADMIN_KEY }}
RELAY_UPLOAD_KEY: ${{ secrets.RELAY_UPLOAD_KEY }}