Publish #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# .github/workflows/publish.yml | |
name: Publish | |
on: | |
release: | |
types: [released, prereleased] | |
workflow_dispatch: | |
# 手动触发工作流 | |
inputs: | |
version: | |
description: '发布版本号 (例如: v1.0.0)' | |
required: true | |
type: string | |
skip_readme_update: | |
description: '跳过 README 更新' | |
required: false | |
type: boolean | |
default: false | |
jobs: | |
publish: | |
name: Release build and publish | |
runs-on: macOS-latest | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v4 | |
with: | |
# 使用 PAT 或默认 token 来确保可以推送更改 | |
token: ${{ secrets.GITHUB_TOKEN }} | |
fetch-depth: 0 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'zulu' | |
java-version: 21 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' | |
- name: Set version variables | |
id: version | |
run: | | |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | |
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT | |
echo "🔧 手动运行,使用版本: ${{ github.event.inputs.version }}" | |
else | |
echo "version=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT | |
echo "🚀 Release 触发,使用版本: ${{ github.event.release.tag_name }}" | |
fi | |
- name: Update README versions | |
if: github.event.inputs.skip_readme_update != 'true' | |
run: | | |
echo "🚀 开始更新 README 版本号..." | |
python scripts/update_readme_version.py | |
- name: Check for README changes | |
if: github.event.inputs.skip_readme_update != 'true' | |
id: check_changes | |
run: | | |
echo "🔍 检查 README 文件是否有变化..." | |
if git diff --quiet HEAD -- README.md README_CN.md; then | |
echo "changed=false" >> $GITHUB_OUTPUT | |
echo "ℹ️ README 文件无变化" | |
else | |
echo "changed=true" >> $GITHUB_OUTPUT | |
echo "✅ README 文件已更新" | |
echo "变更内容:" | |
git diff HEAD -- README.md README_CN.md | |
fi | |
- name: Commit and push README changes | |
if: steps.check_changes.outputs.changed == 'true' && github.event.inputs.skip_readme_update != 'true' | |
run: | | |
echo "📝 提交 README 更改..." | |
git config --local user.email "action@github.com" | |
git config --local user.name "GitHub Action" | |
git add README.md README_CN.md | |
git commit -m "chore: update README versions for release ${{ steps.version.outputs.version }}" | |
git push origin HEAD:${{ github.event.repository.default_branch }} | |
echo "✅ README 更改已推送" | |
- name: Validate Gradle wrapper | |
uses: gradle/wrapper-validation-action@v1 | |
- name: Setup Gradle | |
uses: gradle/gradle-build-action@v2 | |
with: | |
gradle-home-cache-cleanup: true | |
- name: Publish to MavenCentral | |
run: | | |
echo "🚀 开始发布到 Maven Central..." | |
echo "📦 发布版本: ${{ steps.version.outputs.version }}" | |
chmod +x ./gradlew && ./gradlew publishAllPublicationsToSonatypeRepository --no-configuration-cache --stacktrace | |
echo "✅ 发布完成" | |
env: | |
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }} | |
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.MAVEN_CENTRAL_PASSWORD }} | |
ORG_GRADLE_PROJECT_signingInMemoryKeyId: ${{ secrets.SIGNING_KEY_ID }} | |
ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_PASSWORD }} | |
ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.GPG_KEY_CONTENTS }} | |
- name: Summary | |
run: | | |
echo "🎉 发布流程完成!" | |
echo "📦 版本: ${{ steps.version.outputs.version }}" | |
if [ "${{ github.event_name }}" = "release" ]; then | |
echo "🔗 Release: ${{ github.event.release.html_url }}" | |
else | |
echo "🔧 手动运行完成" | |
fi |