Sonar Analysis #881
This file contains 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
name: "Sonar Analysis" | |
on: | |
workflow_run: | |
workflows: [CI Build] | |
types: [completed] | |
jobs: | |
sonar-cloud-analysis: | |
name: "Sonar Cloud Build" | |
runs-on: ubuntu-latest | |
# dependabot should not have SONAR_TOKEN | |
if: github.actor != 'dependabot[bot]' && github.repository_owner == 'remsfal' && github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event != 'pull_request_target' | |
steps: | |
- name: Download PR number artifact | |
if: github.event.workflow_run.event == 'pull_request' | |
uses: dawidd6/action-download-artifact@v6 | |
with: | |
workflow: CI Build | |
run_id: ${{ github.event.workflow_run.id }} | |
name: PR_NUMBER | |
- name: Read PR_NUMBER.txt | |
if: github.event.workflow_run.event == 'pull_request' | |
id: pr_number | |
uses: juliangruber/read-file-action@v1 | |
with: | |
path: ./PR_NUMBER.txt | |
- name: Request GitHub API for PR data | |
if: github.event.workflow_run.event == 'pull_request' | |
uses: octokit/request-action@v2.x | |
id: get_pr_data | |
with: | |
route: GET /repos/{full_name}/pulls/{number} | |
number: ${{ steps.pr_number.outputs.content }} | |
full_name: ${{ github.event.repository.full_name }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Checkout push branch | |
if: github.event.workflow_run.event != 'pull_request' | |
uses: actions/checkout@v4 | |
with: | |
repository: ${{ github.event.workflow_run.head_repository.full_name }} | |
ref: ${{ github.event.workflow_run.head_branch }} | |
fetch-depth: 0 | |
- name: Checkout PR branch | |
if: github.event.workflow_run.event == 'pull_request' | |
uses: actions/checkout@v4 | |
with: | |
repository: ${{ github.event.workflow_run.head_repository.full_name }} | |
ref: ${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} | |
fetch-depth: 0 | |
- name: Checkout base branch | |
if: github.event.workflow_run.event == 'pull_request' | |
run: | | |
git remote add upstream ${{ github.event.repository.clone_url }} | |
git fetch upstream | |
git branch -u upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Cache SonarCloud packages | |
uses: actions/cache@v4 | |
with: | |
path: ~/.sonar/cache | |
key: ${{ runner.os }}-sonar | |
restore-keys: ${{ runner.os }}-sonar | |
- name: Cache Maven packages | |
uses: actions/cache@v4 | |
with: | |
path: ~/.m2/repository | |
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} | |
restore-keys: ${{ runner.os }}-maven | |
- name: Test with Maven (package) | |
run: mvn --batch-mode package | |
- name: SonarCloud Scan on PR | |
if: github.event.workflow_run.event == 'pull_request' | |
env: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
mvn -B verify sonar:sonar \ | |
-Dsonar.projectKey=remsfal_remsfal-backend \ | |
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \ | |
-Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }} \ | |
-Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} \ | |
-Dsonar.pullrequest.base=upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} \ | |
-Dsonar.newCode.referenceBranch=upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} | |
- name: SonarCloud Scan on push | |
if: github.event.workflow_run.event == 'push' && github.event.workflow_run.head_repository.full_name == github.event.repository.full_name | |
env: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
mvn -B verify sonar:sonar \ | |
-Dsonar.projectKey=remsfal_remsfal-backend \ | |
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \ | |
-Dsonar.branch.name=${{ github.event.workflow_run.head_branch }} |