ci-cd.yml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
  2. # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
  3. # 权限声明,确保 workflow 有权限写 checks 和 security-events
  4. permissions:
  5. contents: read
  6. checks: write
  7. security-events: write
  8. name: Java CI with Maven
  9. on:
  10. push:
  11. branches: [ "main" ]
  12. paths-ignore:
  13. - 'README.md'
  14. - 'LICENSE'
  15. - '.gitignore'
  16. - '.gitattributes'
  17. - 'picture'
  18. pull_request:
  19. branches: [ "main" ]
  20. workflow_dispatch:
  21. jobs:
  22. build:
  23. runs-on: ubuntu-latest
  24. timeout-minutes: 30
  25. strategy:
  26. matrix:
  27. java-version: ['8', '17', '21']
  28. fail-fast: false
  29. name: Build with Java ${{ matrix.java-version }}
  30. steps:
  31. - uses: actions/checkout@v3
  32. with:
  33. fetch-depth: 0
  34. - name: Set up JDK ${{ matrix.java-version }}
  35. uses: actions/setup-java@v3
  36. with:
  37. java-version: ${{ matrix.java-version }}
  38. distribution: 'temurin'
  39. cache: 'maven'
  40. # 优化Maven本地仓库缓存策略
  41. - name: Cache Maven packages
  42. uses: actions/cache@v3
  43. with:
  44. path: ~/.m2
  45. key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}-${{ matrix.java-version }}
  46. restore-keys: |
  47. ${{ runner.os }}-m2-
  48. # 编译和测试:去掉failOnWarning,避免因为警告导致失败
  49. - name: Build and Test with Maven
  50. run: |
  51. mvn -B verify --file pom.xml -Dmaven.test.failure.ignore=false -Dgpg.skip -Dmaven.javadoc.skip=false
  52. env:
  53. MAVEN_OPTS: -Xmx4g -XX:MaxMetaspaceSize=1g
  54. MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version"
  55. - name: Publish Test Report
  56. uses: mikepenz/action-junit-report@v4
  57. if: success() || failure()
  58. with:
  59. report_paths: '**/target/surefire-reports/TEST-*.xml'
  60. detailed_summary: true
  61. include_passed: true
  62. fail_on_failure: true
  63. - name: Run SonarQube Analysis
  64. if: matrix.java-version == '17' && github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
  65. continue-on-error: true
  66. run: |
  67. if [[ ! -z "${{ secrets.SONAR_TOKEN }}" ]]; then
  68. mvn sonar:sonar \
  69. -Dsonar.projectKey=agileboot \
  70. -Dsonar.organization=${{ secrets.SONAR_ORGANIZATION || 'default' }} \
  71. -Dsonar.host.url=${{ secrets.SONAR_HOST_URL || 'https://sonarcloud.io' }} \
  72. -Dsonar.login=${{ secrets.SONAR_TOKEN }} \
  73. -Dsonar.java.source=${{ matrix.java-version }}
  74. else
  75. echo "Skipping SonarQube analysis - SONAR_TOKEN not configured"
  76. fi
  77. # 上传构建产物,if-no-files-found 改为 warn
  78. - name: Upload Build Artifacts
  79. uses: actions/upload-artifact@v4
  80. with:
  81. name: agileboot-artifacts-java-${{ matrix.java-version }}
  82. path: |
  83. **/target/*.jar
  84. !**/target/original-*.jar
  85. retention-days: 5
  86. if-no-files-found: warn
  87. # # 只在 Java 17 版本上更新依赖图(权限和token已修复)
  88. # - name: Update dependency graph
  89. # uses: advanced-security/maven-dependency-submission-action@v4
  90. # if: matrix.java-version == '17' && success()
  91. # with:
  92. # token: ${{ secrets.GITHUB_TOKEN }}
  93. # # 发送构建状态通知
  94. # - name: Notify Build Status
  95. # if: always()
  96. # uses: rtCamp/action-slack-notify@v2.2.1
  97. # env:
  98. # SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK || '' }}
  99. # SLACK_CHANNEL: build-notifications
  100. # SLACK_COLOR: ${{ job.status }}
  101. # SLACK_TITLE: Build Status for Java ${{ matrix.java-version }}
  102. # SLACK_MESSAGE: 'Build ${{ job.status }} on Java ${{ matrix.java-version }}'