change build.yml

This commit is contained in:
zi2ven
2026-02-04 19:53:26 +08:00
parent b4b6d409b5
commit b302cb2cc5

View File

@@ -29,16 +29,22 @@ jobs:
git clone https://git.fig-lang.cn/${{ github.repository }} . git clone https://git.fig-lang.cn/${{ github.repository }} .
git checkout ${{ github.ref }} git checkout ${{ github.ref }}
- name: 设置版本 - name: 设置版本和提交信息
run: | run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
VERSION="${{ inputs.version }}" VERSION="${{ inputs.version }}"
else else
VERSION="${GITHUB_REF#refs/tags/}" VERSION="${{ github.ref_name }}"
fi fi
echo "构建版本: $VERSION" echo "构建版本: $VERSION"
echo "VERSION=$VERSION" >> $GITHUB_ENV echo "VERSION=$VERSION" >> $GITHUB_ENV
# 拿提交消息
COMMIT_MSG=$(git log -1 --pretty=%B)
echo "COMMIT_MSG<<EOF" >> $GITHUB_ENV
echo "$COMMIT_MSG" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: 构建项目 (Linux) - name: 构建项目 (Linux)
run: | run: |
echo "开始构建Linux版本..." echo "开始构建Linux版本..."
@@ -83,21 +89,34 @@ jobs:
GITEA_TOKEN: ${{ secrets.CI_TOKEN }} GITEA_TOKEN: ${{ secrets.CI_TOKEN }}
run: | run: |
VERSION="${{ env.VERSION }}" VERSION="${{ env.VERSION }}"
COMMIT_MSG="${{ env.COMMIT_MSG }}"
API="https://git.fig-lang.cn/api/v1/repos/${{ github.repository }}" API="https://git.fig-lang.cn/api/v1/repos/${{ github.repository }}"
echo "正在为Linux版本创建/更新发布 $VERSION ..." echo "正在为Linux版本创建/更新发布 $VERSION ..."
# 确保正确转义
python3 -c "import json, os; print(json.dumps({'tag_name': os.environ['VERSION'], 'name': 'Fig ' + os.environ['VERSION'], 'body': os.environ.get('COMMIT_MSG', ''), 'draft': False, 'prerelease': False}))" > release_body.json
RESPONSE=$(curl -sS -X POST -H "Authorization: token $GITEA_TOKEN" \ RESPONSE=$(curl -sS -X POST -H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "{\"tag_name\":\"$VERSION\",\"name\":\"Fig $VERSION\",\"draft\":false,\"prerelease\":false}" \ -d @release_body.json \
"$API/releases" 2>/dev/null || echo '{"id":0}') "$API/releases" 2>/dev/null || echo '{"id":0}')
RELEASE_ID=$(echo "$RESPONSE" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2) RELEASE_ID=$(echo "$RESPONSE" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "0" ]; then if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "0" ]; then
echo "尝试通过标签获取已有发布的ID..." echo "尝试通过标签获取已有发布的ID..."
RELEASE_ID=$(curl -sS -H "Authorization: token $GITEA_TOKEN" \ RESPONSE_TAG=$(curl -sS -H "Authorization: token $GITEA_TOKEN" "$API/releases/tags/$VERSION")
"$API/releases/tags/$VERSION" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2) RELEASE_ID=$(echo "$RESPONSE_TAG" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
# 发布存在,更新它以包含最新的提交消息
if [ -n "$RELEASE_ID" ] && [ "$RELEASE_ID" != "0" ]; then
echo "更新已有发布的说明..."
curl -sS -X PATCH -H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d @release_body.json \
"$API/releases/$RELEASE_ID" > /dev/null
fi
fi fi
if [ -z "$RELEASE_ID" ]; then if [ -z "$RELEASE_ID" ]; then
@@ -151,9 +170,9 @@ jobs:
run: | run: |
$env:Path = "C:\Program Files\Git\cmd;$env:Path" $env:Path = "C:\Program Files\Git\cmd;$env:Path"
git clone https://git.fig-lang.cn/$env:GITHUB_REPOSITORY . git clone https://git.fig-lang.cn/$env:GITHUB_REPOSITORY .
git checkout $env:GITHUB_REF git checkout ${{ github.ref }}
- name: 设置版本 - name: 设置版本和提交信息
run: | run: |
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
if ($env:GITHUB_EVENT_NAME -eq 'workflow_dispatch') { if ($env:GITHUB_EVENT_NAME -eq 'workflow_dispatch') {
@@ -161,11 +180,17 @@ jobs:
if (-not $VERSION) { $VERSION = $env:VERSION_INPUT } if (-not $VERSION) { $VERSION = $env:VERSION_INPUT }
if (-not $VERSION) { $VERSION = "dev-build" } if (-not $VERSION) { $VERSION = "dev-build" }
} else { } else {
$VERSION = $env:GITHUB_REF_NAME $VERSION = "${{ github.ref_name }}"
} }
Write-Host "构建版本: $VERSION" Write-Host "构建版本: $VERSION"
"VERSION=$VERSION" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8 "VERSION=$VERSION" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
# 提交消息
$COMMIT_MSG = git log -1 --pretty=%B
"COMMIT_MSG<<EOF" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
$COMMIT_MSG | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
"EOF" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
- name: 构建项目 (Windows Native) - name: 构建项目 (Windows Native)
run: | run: |
xmake f -p windows -a x86_64 -m release -y xmake f -p windows -a x86_64 -m release -y
@@ -186,14 +211,7 @@ jobs:
run: | run: |
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# 🔧 修改:与发布步骤使用相同的版本号计算逻辑 $VERSION = $env:VERSION
if ($env:GITHUB_EVENT_NAME -eq 'workflow_dispatch') {
$VERSION = $env:INPUT_VERSION
if (-not $VERSION) { $VERSION = "dev-build" }
} else {
$VERSION = $env:GITHUB_REF_NAME
}
Write-Host "打包版本: $VERSION" Write-Host "打包版本: $VERSION"
$PACKAGE_NAME = "Fig-${VERSION}-windows-x86_64" $PACKAGE_NAME = "Fig-${VERSION}-windows-x86_64"
@@ -231,13 +249,8 @@ jobs:
run: | run: |
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# 重新计算版本号(不使用 $env:VERSION $VERSION = $env:VERSION
if ($env:GITHUB_EVENT_NAME -eq 'workflow_dispatch') { $COMMIT_MSG = $env:COMMIT_MSG
$VERSION = $env:INPUT_VERSION
if (-not $VERSION) { $VERSION = "dev-build" }
} else {
$VERSION = $env:GITHUB_REF_NAME
}
Write-Host "正在上传Windows版本到发布: $VERSION" Write-Host "正在上传Windows版本到发布: $VERSION"
@@ -262,6 +275,7 @@ jobs:
$CREATE_BODY = @{ $CREATE_BODY = @{
tag_name = $VERSION tag_name = $VERSION
name = "Fig $VERSION" name = "Fig $VERSION"
body = $COMMIT_MSG
draft = $false draft = $false
prerelease = $false prerelease = $false
} | ConvertTo-Json -Compress } | ConvertTo-Json -Compress
@@ -281,6 +295,16 @@ jobs:
$RESPONSE = Invoke-RestMethod -Uri "$API/releases/tags/$VERSION" ` $RESPONSE = Invoke-RestMethod -Uri "$API/releases/tags/$VERSION" `
-Headers @{Authorization = "token $TOKEN" } ` -Headers @{Authorization = "token $TOKEN" } `
-ErrorAction SilentlyContinue -ErrorAction SilentlyContinue
# 发布存在,更新它以包含最新的提交消息
if ($RESPONSE -and $RESPONSE.id) {
Write-Host "更新已有发布的说明..."
Invoke-RestMethod -Method Patch -Uri "$API/releases/$($RESPONSE.id)" `
-Headers @{
Authorization = "token $TOKEN"
'Content-Type' = 'application/json'
} -Body $CREATE_BODY -ErrorAction SilentlyContinue | Out-Null
}
} }
if ($RESPONSE -and $RESPONSE.id) { if ($RESPONSE -and $RESPONSE.id) {