Publish to GitHub Packages #25
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
| # This workflow uses actions that are not certified by GitHub. | |
| # They are provided by a third-party and are governed by | |
| # separate terms of service, privacy policy, and support | |
| # documentation. | |
| # This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time | |
| # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle | |
| name: Publish to GitHub Packages | |
| on: | |
| # 1. 手动触发 | |
| workflow_dispatch: | |
| inputs: | |
| skip_tests: | |
| type: boolean | |
| description: 'Skip unit tests?' | |
| default: false | |
| # 2. 推送 | |
| push: | |
| # 2.1 tag(以 v 开头) | |
| tags: | |
| - 'v*' | |
| # # 2.2 特定分支 | |
| # branches: | |
| # # xxx-alpha.0, xxx-beta.1 | |
| # - pre-release | |
| # # xxx-rc.0, xxx-rc.1 | |
| # - release-candidate | |
| # # 3. PR 合并到目标分支 | |
| # pull_request: | |
| # types: [ closed ] | |
| # branches: [ pre-release, release-candidate ] | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write # 必须有写包权限 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| # 获取最新 commit message | |
| - name: Get commit message | |
| id: commit_message | |
| run: | | |
| COMMIT_MSG=$(git log -1 --pretty=%B) | |
| echo "commit_message=$COMMIT_MSG" >> $GITHUB_OUTPUT | |
| echo "🔍 Last commit message: $COMMIT_MSG" | |
| # 如果是 push 到分支,且 commit message 不含 [publish],则跳过 | |
| - name: Check for [publish] in commit message | |
| if: > | |
| github.event_name == 'push' && | |
| !contains(github.event.ref, 'refs/tags/') && | |
| !contains(steps.commit_message.outputs.commit_message, '[publish]') | |
| run: | | |
| echo "⚠️ Commit message does not contain [publish]. Skipping publish job." | |
| exit 0 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| server-id: github | |
| server-username: ${{ github.actor }} | |
| server-password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Node.js 22 | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '22.12.0' | |
| # cache: 'pnpm' | |
| - name: Install pnpm | |
| run: npm install -g pnpm@latest | |
| # Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies. | |
| # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md | |
| - name: Setup Gradle | |
| uses: gradle/actions/setup-gradle@017a9effdb900e5b5b2fddfb590a105619dca3c3 # v4.4.2 | |
| with: | |
| cache-read-only: false | |
| cache-write-only: false | |
| # 构建并发布到 GitHub Packages | |
| - name: Build and Publish to GitHub Packages | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| GRADLE_CMD="./gradlew clean build publishMavenPublicationToGitHubPackagesRepository publishMavenBomPublicationToGitHubPackagesRepository" | |
| GRADLE_CMD="$GRADLE_CMD -D gpr.user=${{ github.actor }}" | |
| GRADLE_CMD="$GRADLE_CMD -D gpr.key=${{ secrets.GITHUB_TOKEN }}" | |
| GRADLE_CMD="$GRADLE_CMD -P xtream.maven.publications.signing=off" | |
| GRADLE_CMD="$GRADLE_CMD -P xtream.maven.repo.github.enabled=true" | |
| GRADLE_CMD="$GRADLE_CMD -P xtream.frontend.build.jt-808-server-dashboard-ui.enabled=true" | |
| GRADLE_CMD="$GRADLE_CMD -P xtream.backend.build.checkstyle.enabled=true" | |
| # 处理跳过测试 | |
| if ${{ | |
| (github.event_name == 'workflow_dispatch' && github.event.inputs.skip_tests) || | |
| (github.event_name == 'push' && contains(steps.commit_message.outputs.commit_message, '[skip-tests]')) | |
| }}; then | |
| echo "⏭️ Skipping tests..." | |
| GRADLE_CMD="$GRADLE_CMD -x test -x check" | |
| fi | |
| # 执行发布 | |
| $GRADLE_CMD | |
| - name: On Build Failed | |
| if: ${{ failure() }} | |
| run: | | |
| echo "❌ The publish job failed. Check logs above." | |
| - name: On Build Succeeded | |
| if: ${{ success() }} | |
| run: | | |
| echo "✅ Successfully published to GitHub Packages!" | |
| echo "✅ Location: https://github.com/${{ github.repository }}/packages" | |
| - name: On Build Finished | |
| if: always() | |
| run: | | |
| echo "📦 Finished." |