sonarcloud-pr.yml 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. name: SonarCloud Scan (PRs triggered by maven.yml)
  2. on:
  3. workflow_run:
  4. workflows:
  5. - Build
  6. types:
  7. - completed
  8. # Allows you to run this workflow manually from the Actions tab
  9. workflow_dispatch:
  10. jobs:
  11. get-info:
  12. name: "Get information about the source run"
  13. runs-on: ubuntu-latest
  14. if: ${{ github.actor != 'dependabot[bot]' && github.event.workflow_run.conclusion == 'success' }}
  15. outputs:
  16. sourceHeadRepo: ${{ steps.source-run-info.outputs.sourceHeadRepo }}
  17. sourceHeadBranch: ${{ steps.source-run-info.outputs.sourceHeadBranch }}
  18. sourceHeadSha: ${{ steps.source-run-info.outputs.sourceHeadSha }}
  19. mergeCommitSha: ${{ steps.source-run-info.outputs.mergeCommitSha }}
  20. targetCommitSha: ${{ steps.source-run-info.outputs.targetCommitSha }}
  21. pullRequestNumber: ${{ steps.source-run-info.outputs.pullRequestNumber }}
  22. pullRequestLabels: ${{ steps.source-run-info.outputs.pullRequestLabels }}
  23. targetBranch: ${{ steps.source-run-info.outputs.targetBranch }}
  24. sourceEvent: ${{ steps.source-run-info.outputs.sourceEvent }}
  25. steps:
  26. - name: "Get information about the origin 'CI' run"
  27. uses: potiuk/get-workflow-origin@v1_3
  28. id: source-run-info
  29. with:
  30. token: ${{ secrets.GITHUB_TOKEN }}
  31. sourceRunId: ${{ github.event.workflow_run.id }}
  32. sonar:
  33. name: SonarCloud Scan
  34. runs-on: ubuntu-latest
  35. if: ${{ needs.get-info.outputs.sourceEvent == 'pull_request' && github.actor != 'dependabot[bot]' && github.event.workflow_run.conclusion == 'success' }}
  36. needs: get-info
  37. steps:
  38. - uses: actions/checkout@v3
  39. with:
  40. repository: ${{ github.event.workflow_run.head_repository.full_name }}
  41. ref: ${{ github.event.workflow_run.head_branch }}
  42. fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
  43. - name: Set up JDK
  44. uses: actions/setup-java@v3
  45. with:
  46. distribution: 'temurin'
  47. java-version: 17
  48. - name: Cache SonarCloud packages
  49. uses: actions/cache@v3
  50. with:
  51. path: ~/.sonar/cache
  52. key: ${{ runner.os }}-sonar
  53. restore-keys: ${{ runner.os }}-sonar
  54. - name: Cache Maven packages
  55. uses: actions/cache@v3
  56. with:
  57. path: ~/.m2
  58. key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
  59. restore-keys: ${{ runner.os }}-m2
  60. - name: Build and analyze (PR)
  61. env:
  62. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  63. SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  64. run: >
  65. mvn
  66. -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
  67. -Dsonar.pullrequest.key=${{ needs.get-info.outputs.pullRequestNumber }}
  68. -Dsonar.pullrequest.branch=${{ needs.get-info.outputs.sourceHeadBranch }}
  69. -Dsonar.pullrequest.base=${{ needs.get-info.outputs.pullRequestNumber }}
  70. -U -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar