Skip to content

Publish

Publish #34

Workflow file for this run

name: Publish
on:
workflow_dispatch:
inputs:
version_type:
description: '版本发布类型'
required: true
default: 'Patch'
type: choice
options:
- Patch
- Minor
- Major
changelog:
description: '更新日志(子插件壹:更新内容|子插件贰:更新内容)'
required: true
type: string
permissions:
contents: write
pull-requests: write
jobs:
publish:
runs-on: ubuntu-latest
name: 🚀 自动化构建
environment:
name: Publish
url: ${{ github.server_url }}/${{ github.repository }}/releases/tag/v${{ steps.calculate_version.outputs.new_version }}
steps:
- name: Validate inputs
run: |
echo "📋 原始输入参数:"
echo " - 版本发布类型: ${{ github.event.inputs.version_type }}"
echo " - 更新日志: ${{ github.event.inputs.changelog }}"
echo ""
CHANGELOG="${{ github.event.inputs.changelog }}"
# Trim whitespace and check if empty
TRIMMED=$(echo "$CHANGELOG" | xargs)
if [ -z "$TRIMMED" ]; then
echo "::error::更新日志不能为空或仅包含空格字符"
exit 1
fi
echo "✅ 输入验证通过"
- name: Checkout full repo
uses: actions/checkout@v6
with:
fetch-depth: 0
token: ${{ secrets.PAT_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.10'
- name: Install pip dependencies
run: |
pip3 install semver luadata
- name: Install apt dependencies
run: |
sudo rm -f /var/lib/man-db/auto-update
sudo apt install --no-install-recommends lua5.1 p7zip-full
- name: Set up Git user
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Switch and hard reset to master
run: |
git fetch origin
git checkout master
git reset --hard origin/master
- name: Detect project info
id: project_info
run: |
# 从文件系统检测项目命名空间(英文)
BASE_DIR=$(ls -d *_\!Base 2>/dev/null | head -1)
if [ -z "$BASE_DIR" ]; then
echo "::error::未找到 *_!Base 目录"
exit 1
fi
PROJECT_NS="${BASE_DIR%_\!Base}"
echo "📁 检测到项目命名空间: $PROJECT_NS"
# 从 package.ini 读取项目名称(中文)
if [ -f "package.ini" ]; then
# package.ini 是 GBK 编码,转换为 UTF-8 后提取 name
PROJECT_NAME=$(iconv -f GBK -t UTF-8 package.ini | grep -E "^name=" | sed 's/name=//' | sed 's/插件集$//')
echo "📦 检测到项目名称: $PROJECT_NAME"
else
PROJECT_NAME="$PROJECT_NS"
echo "⚠️ 未找到 package.ini,使用命名空间作为项目名称"
fi
echo "namespace=$PROJECT_NS" >> $GITHUB_OUTPUT
echo "name=$PROJECT_NAME" >> $GITHUB_OUTPUT
- name: Update version, changelog and AssertVersion
id: update_changelog
run: |
VERSION_TYPE="${{ github.event.inputs.version_type }}"
CHANGELOG="${{ github.event.inputs.changelog }}"
PROJECT_NS="${{ steps.project_info.outputs.namespace }}"
PROJECT_NAME="${{ steps.project_info.outputs.name }}"
echo "🔄 开始更新版本信息..."
# 使用 changelog.py 脚本更新版本号和更新日志
python3 \!src-dist/changelog.py --version-type "$VERSION_TYPE" --changelog "$CHANGELOG"
# 从 Base.lua 读取更新后的版本号
NEW_VERSION=$(grep "_VERSION_.*'.*'" "${PROJECT_NS}_!Base/src/lib/Base.lua" | sed -E "s/.*'(.*)'.*/\1/")
echo "📦 新版本号: $NEW_VERSION"
# 更新 AssertVersion 调用
echo "🔍 Updating AssertVersion calls to version: $NEW_VERSION"
python3 \!src-dist/update_assert_version.py "$NEW_VERSION" --force-changed
# 检查是否有文件被修改
if git diff --quiet; then
echo "✅ No AssertVersion calls needed to be updated"
else
echo "📝 Some AssertVersion calls have been updated:"
git diff --name-only
fi
# 设置输出变量
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
# 读取生成的更新日志内容(从 CHANGELOG.md 中提取最新条目)
PROCESSED_CHANGELOG=$(sed -n '/^## '"${PROJECT_NAME}"'插件集 v'"$NEW_VERSION"'/,/^## '"${PROJECT_NAME}"'插件集 v[0-9]/{ /^## '"${PROJECT_NAME}"'插件集 v[0-9]/!p; /^## '"${PROJECT_NAME}"'插件集 v'"$NEW_VERSION"'/p }' CHANGELOG.md | sed '/^$/d')
# 设置步骤输出
{
echo "changelog<<EOF"
echo "$PROCESSED_CHANGELOG"
echo "EOF"
} >> $GITHUB_OUTPUT
- name: Calculate version for outputs
id: calculate_version
run: |
echo "new_version=${{ steps.update_changelog.outputs.new_version }}" >> $GITHUB_OUTPUT
- name: Write secret to file
run: |
cat > secret.jx3dat << 'EOF'
${{ secrets.SECRET_JX3DAT }}
EOF
- name: Commit release changes
run: |
NEW_VERSION="${{ steps.calculate_version.outputs.new_version }}"
git add -A
git commit -m "release: ${NEW_VERSION}" || echo "No changes to commit"
- name: Run Build Command
run: |
python3 \!src-dist/ci.py
- name: Create Release Branch
run: |
NEW_VERSION="${{ steps.calculate_version.outputs.new_version }}"
BRANCH="release/v${NEW_VERSION}"
git push -f origin master:stable
git push -f origin master:$BRANCH
- name: Create Pull Request
id: create_pr
uses: peter-evans/create-pull-request@v8
with:
token: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
title: "release: v${{ steps.calculate_version.outputs.new_version }}"
body: "Automated release PR for version ${{ steps.calculate_version.outputs.new_version }}"
base: master
branch: "release/v${{ steps.calculate_version.outputs.new_version }}"
- name: Upload Artifacts
id: upload_artifacts
uses: actions/upload-artifact@v7
with:
name: dist-archives-${{ github.run_number }}-${{ github.sha }}
path: '\!src-dist/dist/*.7z'
- name: Output Summary
if: steps.create_pr.outputs.pull-request-url
run: |
echo "# 🎉 构建成功!" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# 输出更新日志内容
echo "## 📝 更新日志内容" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "${{ steps.update_changelog.outputs.changelog }}" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# 输出 Pull Request 信息
echo "## 🔗 Pull Request 信息" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "📝 **Pull Request 已创建**: ${{ steps.create_pr.outputs.pull-request-url }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "🎯 **版本**: v${{ steps.calculate_version.outputs.new_version }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# 输出 Artifacts 信息
echo "## 📦 构建产物" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "📁 **Artifacts 名称**: \`dist-archives-${{ github.run_number }}-${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "🔗 **下载链接**: [${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "💡 点击上面的链接可以在 Actions 页面中找到并下载构建产物" >> $GITHUB_STEP_SUMMARY
# 同时输出 notice
echo "::notice title=📝 Pull Request 已生成::Pull Request created: ${{ steps.create_pr.outputs.pull-request-url }}"