Files
Fig-TreeWalker/.gitea/workflows/build.yml
T
PuqiAR f67a43f159
Release Build / build-windows-x64 (push) Failing after 4s
Release Build / build-linux-x64 (push) Successful in 23s
[CI] Windows工具链验证修复
2026-01-03 22:03:24 +08:00

251 lines
9.7 KiB
YAML

name: Release Build
on:
push:
tags: ["*"]
workflow_dispatch:
inputs:
version:
description: "版本号 (例如: v1.0.0)"
required: true
default: "dev-build"
jobs:
build-linux-x64:
runs-on: ubuntu
container:
image: git.fig-lang.cn/puqiar/fig-ci:base-latest
options: --network=host
steps:
- name: 验证构建环境
run: |
echo "=== 环境验证开始 ==="
xmake --version
clang++ --version | head -1
echo "=== 环境验证通过 ==="
- name: 检出代码
run: |
git clone https://git.fig-lang.cn/${{ github.repository }} .
git checkout ${{ github.ref }}
- name: 设置版本
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
VERSION="${{ inputs.version }}"
else
VERSION="${GITHUB_REF#refs/tags/}"
fi
echo "构建版本: $VERSION"
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: 构建项目 (Linux)
run: |
echo "开始构建Linux版本..."
xmake f -p linux -a x86_64 -m release -y
xmake build -j$(nproc)
echo "Linux构建成功。"
- name: 打包Linux发布文件
run: |
PACKAGE_NAME="Fig-${{ env.VERSION }}-linux-x86_64"
mkdir -p "${PACKAGE_NAME}"
cp build/linux/x86_64/release/Fig "${PACKAGE_NAME}/"
if [ -d "src/Module/Library" ]; then
cp -r src/Module/Library "${PACKAGE_NAME}/"
echo "已包含Library目录。"
fi
tar -czf "${PACKAGE_NAME}.tar.gz" "${PACKAGE_NAME}"
sha256sum "${PACKAGE_NAME}.tar.gz" > "${PACKAGE_NAME}.sha256"
echo "Linux打包完成: ${PACKAGE_NAME}.tar.gz"
- name: 发布Linux版本到Gitea
env:
GITEA_TOKEN: ${{ secrets.CI_TOKEN }}
run: |
VERSION="${{ env.VERSION }}"
API="https://git.fig-lang.cn/api/v1/repos/${{ github.repository }}"
echo "正在为Linux版本创建/更新发布 $VERSION ..."
# 1. 创建发布并捕获响应,提取 Release ID
RESPONSE=$(curl -sS -X POST -H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"tag_name\":\"$VERSION\",\"name\":\"Fig $VERSION\",\"draft\":false,\"prerelease\":false}" \
"$API/releases" 2>/dev/null || echo '{"id":0}')
# 从响应中提取 Release ID
RELEASE_ID=$(echo "$RESPONSE" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "0" ]; then
# 如果创建失败或已存在,尝试通过标签获取 Release ID
echo "尝试通过标签获取已有发布的ID..."
RELEASE_ID=$(curl -sS -H "Authorization: token $GITEA_TOKEN" \
"$API/releases/tags/$VERSION" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
fi
if [ -z "$RELEASE_ID" ]; then
echo "错误:无法获取或创建发布 ID"
exit 1
fi
echo "使用发布 ID: $RELEASE_ID 进行上传"
# 2. 使用 Release ID 上传资产
curl -sS -X POST -H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @Fig-$VERSION-linux-x86_64.tar.gz \
"$API/releases/$RELEASE_ID/assets?name=Fig-$VERSION-linux-x86_64.tar.gz"
curl -sS -X POST -H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: text/plain" \
--data-binary @Fig-$VERSION-linux-x86_64.sha256 \
"$API/releases/$RELEASE_ID/assets?name=Fig-$VERSION-linux-x86_64.sha256"
echo "✅ Linux版本发布完成!"
build-windows-x64:
runs-on: windows
steps:
- name: 验证Windows工具链
run: |
# 设置当前 PowerShell 会话的执行策略
powershell -Command "Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force"
# 使用 PowerShell 语法检查工具链
powershell -Command "
# 检查 xmake
if (Get-Command xmake -ErrorAction SilentlyContinue) {
Write-Host '✅ xmake 已安装'
xmake --version
} else {
Write-Host '警告: xmake 未找到'
}
# 检查 clang++
if (Get-Command clang++ -ErrorAction SilentlyContinue) {
Write-Host '✅ clang++ 已安装'
clang++ --version
} else {
Write-Host '警告: clang++ 未找到'
}
# 检查 git(可选)
if (Get-Command git -ErrorAction SilentlyContinue) {
Write-Host '✅ git 已安装'
} else {
Write-Host '警告: git 未找到'
}
"
- name: 检出代码
run: |
git clone https://git.fig-lang.cn/${{ github.repository }} .
git checkout ${{ github.ref }}
- name: 设置版本
run: |
if ("${{ github.event_name }}" -eq "workflow_dispatch") {
$VERSION = "${{ inputs.version }}"
} else {
$VERSION = "${{ github.ref }}".Replace("refs/tags/", "")
}
echo "VERSION=$VERSION" | Out-File -FilePath $env:GITHUB_ENV -Append
echo "Windows构建版本: $VERSION"
- name: 安装 xmake (Windows,可选)
run: |
# 如果工具链验证步骤发现没有 xmake,再执行安装
if (-not (Get-Command xmake -ErrorAction SilentlyContinue)) {
echo "正在安装 xmake..."
curl -fsSL https://xmake.io/shget.text -o xmake.ps1
powershell -ExecutionPolicy Bypass -File xmake.ps1
} else {
echo "xmake 已安装,跳过安装步骤。"
}
- name: 构建项目 (Windows Native)
run: |
xmake f -p windows -a x86_64 -m release -y
xmake build -j${env:NUMBER_OF_PROCESSORS}
echo "Windows构建成功。"
- name: 打包Windows发布文件
run: |
$VERSION = $env:VERSION
$PACKAGE_NAME = "Fig-${VERSION}-windows-x86_64"
New-Item -ItemType Directory -Force -Path $PACKAGE_NAME
# 尝试查找可执行文件 (可能是 Fig 或 Fig.exe)
if (Test-Path "build\windows\x86_64\release\Fig.exe") {
Copy-Item build\windows\x86_64\release\Fig.exe $PACKAGE_NAME\
} elseif (Test-Path "build\windows\x86_64\release\Fig") {
Copy-Item build\windows\x86_64\release\Fig $PACKAGE_NAME\Fig.exe
} else {
echo "错误:未找到构建输出文件"
Get-ChildItem -Recurse -Path build -Include "Fig*" | Select-Object -First 5
exit 1
}
if (Test-Path "src\Module\Library") {
Copy-Item -Recurse src\Module\Library $PACKAGE_NAME\
}
Compress-Archive -Path $PACKAGE_NAME -DestinationPath "${PACKAGE_NAME}.zip"
Get-FileHash "${PACKAGE_NAME}.zip" -Algorithm SHA256 | Out-File "${PACKAGE_NAME}.sha256" -Encoding UTF8
echo "Windows打包完成: ${PACKAGE_NAME}.zip"
- name: 发布Windows版本到Gitea
env:
GITEA_TOKEN: ${{ secrets.CI_TOKEN }}
run: |
$VERSION = $env:VERSION
$API = "https://git.fig-lang.cn/api/v1/repos/${{ github.repository }}"
echo "正在上传Windows版本到发布 $VERSION ..."
# 1. 尝试通过标签获取 Release ID
$RELEASE_RESPONSE = Invoke-RestMethod -Uri "$API/releases/tags/$VERSION" -Headers @{
Authorization = "token $env:GITEA_TOKEN"
} -ErrorAction SilentlyContinue
if ($RELEASE_RESPONSE -and $RELEASE_RESPONSE.id) {
$RELEASE_ID = $RELEASE_RESPONSE.id
echo "找到已有发布 ID: $RELEASE_ID"
} else {
# 如果不存在,则创建新发布
echo "发布不存在,正在创建..."
$CREATE_BODY = @{
tag_name = $VERSION
name = "Fig $VERSION"
draft = $false
prerelease = $false
} | ConvertTo-Json
$CREATE_RESPONSE = Invoke-RestMethod -Method Post -Uri "$API/releases" -Headers @{
Authorization = "token $env:GITEA_TOKEN"
'Content-Type' = 'application/json'
} -Body $CREATE_BODY -ErrorAction SilentlyContinue
if ($CREATE_RESPONSE -and $CREATE_RESPONSE.id) {
$RELEASE_ID = $CREATE_RESPONSE.id
echo "创建新发布 ID: $RELEASE_ID"
} else {
echo "错误:无法获取或创建发布"
exit 1
}
}
# 2. 使用 Release ID 上传资产
echo "正在上传 ZIP 文件..."
Invoke-RestMethod -Method Post -Uri "$API/releases/$RELEASE_ID/assets?name=Fig-$VERSION-windows-x86_64.zip" -Headers @{
Authorization = "token $env:GITEA_TOKEN"
'Content-Type' = 'application/octet-stream'
} -InFile "Fig-$VERSION-windows-x86_64.zip" -ErrorAction SilentlyContinue
echo "正在上传校验文件..."
Invoke-RestMethod -Method Post -Uri "$API/releases/$RELEASE_ID/assets?name=Fig-$VERSION-windows-x86_64.sha256" -Headers @{
Authorization = "token $env:GITEA_TOKEN"
'Content-Type' = 'text/plain'
} -InFile "Fig-$VERSION-windows-x86_64.sha256" -ErrorAction SilentlyContinue
echo "✅ Windows版本发布完成!"