Skip to content

workflow: 修复构建步骤中的问题 #21

workflow: 修复构建步骤中的问题

workflow: 修复构建步骤中的问题 #21

Workflow file for this run

name: tauri-build-windows
on:
workflow_dispatch:
inputs:
env_name:
description: "Frontend build mode (development/production/test)"
required: false
default: "production"
release:
types: [published]
push:
tags:
- "v*"
jobs:
build:
# Windows 专用构建 Job:
# - 仅在带有 Windows 标签的 self-hosted Runner 上运行
# - 不适用于 macOS / Linux 等其他平台
# 如需支持多平台,请为不同平台创建独立的 workflow 或 job
runs-on: [self-hosted, Windows]
permissions:
contents: write
env:
# 前端构建参数
# build.cjs reads ENV_NAME; default production
ENV_NAME: ${{ inputs.env_name || 'production' }}
# Release 相关上下文(同时支持 release 和 push tag 触发)
# - RELEASE_TAG: 优先使用 release 事件中的 tag_name,否则回退到 ref_name(如 v0.1.3)
# - RELEASE_NOTES: 在 release 事件中存在,push tag 时为空字符串
RELEASE_TAG: ${{ github.event.release.tag_name || github.ref_name }}
RELEASE_NOTES: ${{ github.event.release.body || '' }}
# R2 相关配置(若未配置对应 Secret,则相关功能会被跳过或报错)
R2_ACCOUNT_ID: ${{ secrets.R2_ACCOUNT_ID }}
R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
R2_BUCKET: ${{ secrets.R2_BUCKET }}
R2_PUBLIC_BASE_URL: ${{ secrets.R2_PUBLIC_BASE_URL }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install dependencies
run: pnpm install --frozen-lockfile
# 在 CI 构建期间使用 fixed 版本的 Tauri 配置
# 先将 src-tauri/tauri.conf.fixed.json 覆盖为 src-tauri/tauri.conf.json,
# 然后再由 prepare-version.mjs 统一写入最终版本号,确保参与打包的配置版本正确。
- name: Use fixed Tauri config
shell: powershell
run: |
Copy-Item "src-tauri/tauri.conf.fixed.json" "src-tauri/tauri.conf.json" -Force
- name: Prepare version from tag
# For release events, GITHUB_REF_NAME is usually "vX.Y.Z" as well.
run: node deploy/prepare-version.mjs
# Build and bundle the Tauri app.
# It will run `beforeBuildCommand` from `src-tauri/tauri.conf.json` (node build.cjs -> pnpm build:<env>)
- name: Build Tauri app
uses: tauri-apps/tauri-action@v0
env:
# 使用统一的 GT_TOKEN 作为凭证来源
GITHUB_TOKEN: ${{ secrets.GT_TOKEN }}
# Optional: enable updater signing / release signing if you configure them later
TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
with:
projectPath: .
# 与本地一致,使用 production feature 进行构建
args: --features production
# If you create a GitHub Release (tag v*), this will attach artifacts.
releaseId: ${{ github.event.release.id }}
- name: Generate latest.json
run: node deploy/generate-latest-json.mjs
- name: Upload installer to R2 storage
# 同时支持 release 事件和 push tag 事件
if: github.event_name == 'release' || github.event_name == 'push'
shell: powershell
run: |
$ErrorActionPreference = "Stop"
$TAG = "${{ env.RELEASE_TAG }}"
# 去掉前缀 v,得到纯版本号,如 0.1.0
$VERSION = $TAG.TrimStart('v')
# 找到 NSIS 安装包
$installer = Get-ChildItem "src-tauri/target/release/bundle/nsis" -Filter *.exe | Select-Object -First 1
if (-not $installer) {
Write-Error "NSIS installer not found under src-tauri/target/release/bundle/nsis"
}
# 使用 AWS CLI 上传到 R2(唯一方式)
if (-not (Get-Command aws -ErrorAction SilentlyContinue)) {
Write-Error "aws CLI not found. Please install AWS CLI v2 and ensure 'aws' is in PATH."
}
$accountId = "${{ env.R2_ACCOUNT_ID }}"
$bucket = "${{ env.R2_BUCKET }}"
$endpoint = "https://${accountId}.r2.cloudflarestorage.com"
$dest = "s3://${bucket}/${VERSION}/simprint_setup.exe"
$env:AWS_ACCESS_KEY_ID = "${{ env.R2_ACCESS_KEY_ID }}"
$env:AWS_SECRET_ACCESS_KEY = "${{ env.R2_SECRET_ACCESS_KEY }}"
$env:AWS_EC2_METADATA_DISABLED = "true"
Write-Host "Uploading $($installer.FullName) to R2: $dest"
aws s3 cp $installer.FullName $dest --endpoint-url $endpoint --region auto
- name: Upload latest.json to R2 root
# 同时支持 release 事件和 push tag 事件
if: github.event_name == 'release' || github.event_name == 'push'
shell: powershell
run: |
$ErrorActionPreference = "Stop"
if (-not (Get-Command aws -ErrorAction SilentlyContinue)) {
Write-Error "aws CLI not found. Please install AWS CLI v2 and ensure 'aws' is in PATH."
}
$accountId = "${{ env.R2_ACCOUNT_ID }}"
$bucket = "${{ env.R2_BUCKET }}"
$endpoint = "https://${accountId}.r2.cloudflarestorage.com"
$dest = "s3://${bucket}/latest.json"
$env:AWS_ACCESS_KEY_ID = "${{ env.R2_ACCESS_KEY_ID }}"
$env:AWS_SECRET_ACCESS_KEY = "${{ env.R2_SECRET_ACCESS_KEY }}"
$env:AWS_EC2_METADATA_DISABLED = "true"
Write-Host "Uploading latest.json to R2 root: $dest"
aws s3 cp "latest.json" $dest --endpoint-url $endpoint --region auto
- name: Upload latest.json artifact (manual runs)
if: github.event_name == 'workflow_dispatch'
uses: actions/upload-artifact@v4
with:
name: latest-json
path: latest.json
- name: Publish version metadata
if: github.event_name == 'release' || github.event_name == 'push'
shell: powershell
env:
VERSION_API_URL: ${{ secrets.VERSION_API_URL }}
VERSION_API_KEY: ${{ secrets.VERSION_API_KEY }}
RELEASE_TAG: ${{ env.RELEASE_TAG }}
RELEASE_NOTES: ${{ env.RELEASE_NOTES }}
run: |
$ErrorActionPreference = 'Stop'
if (-not $env:VERSION_API_URL) { throw 'VERSION_API_URL is not set' }
if (-not $env:VERSION_API_KEY) { throw 'VERSION_API_KEY is not set' }
$version = $env:RELEASE_TAG.TrimStart('v')
$timestamp = Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz'
$notes = if ([string]::IsNullOrWhiteSpace($env:RELEASE_NOTES)) { 'Automated release' } else { $env:RELEASE_NOTES }
$exePath = 'src-tauri/target/release/simprint.exe'
if (-not (Test-Path $exePath)) { throw \"Executable not found at $exePath\" }
$curlArgs = @(
'-s',
'-X', 'POST',
"$env:VERSION_API_URL/api/v1/versions/create",
'-H', "X-API-KEY: $env:VERSION_API_KEY",
'-F', 'type_id=1',
'-F', 'resource_name=simprint.exe',
'-F', "version=$version",
'-F', "name=simprint-$version.exe",
'-F', "notes=$notes",
'-F', 'platform=windows',
'-F', "pub_date=$timestamp",
'-F', "file=@$exePath"
)
$curlResponse = & curl.exe @$curlArgs
if ($LASTEXITCODE -ne 0) {
throw ("versions/create curl failed with exit code {0}: {1}" -f $LASTEXITCODE, $curlResponse)
}