Skip to content

SonarCloud Analysis #2186

SonarCloud Analysis

SonarCloud Analysis #2186

# SPDX-FileCopyrightText: 2022 Alliander N.V.
#
# SPDX-License-Identifier: Apache-2.0
name: SonarCloud Analysis
on:
workflow_run:
workflows: [SonarCloud Build]
types: [completed]
jobs:
sonar-check:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
timeout-minutes: 15
permissions:
contents: read
actions: read
pull-requests: read
steps:
- name: echo event
run: cat $GITHUB_EVENT_PATH
- name: Download PR number artifact
if: github.event.workflow_run.event == 'pull_request'
uses: dawidd6/action-download-artifact@5c98f0b039f36ef966fdb7dfa9779262785ecb05 # v14
with:
workflow: SonarCloud 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@b549046febe0fe86f8cb4f93c24e284433f9ab58 # 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@05a2312de9f8207044c4c9e41fe19703986acc13 # 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 }}
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
fetch-depth: 0
- name: Download build artifacts
uses: dawidd6/action-download-artifact@5c98f0b039f36ef966fdb7dfa9779262785ecb05 # v14
with:
workflow: SonarCloud Build
run_id: ${{ github.event.workflow_run.id }}
name: build-artifacts
- name: Cache SonarCloud packages
uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Set up JDK 17
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5
with:
distribution: 'zulu'
java-version: '17'
cache: 'maven'
- name: Set Common Sonar Variables
id: sonar_env
# Use $GITHUB_OUTPUT; the legacy ##[set-output] syntax is deprecated and disabled.
run: |
echo "sonar_opts=-Dsonar.host.url=https://sonarcloud.io -Dsonar.projectKey=com-pas_compas-scl-auto-alignment -Dsonar.organization=com-pas" >> "$GITHUB_OUTPUT"
- name: Create custom Maven Settings.xml
uses: whelk-io/maven-settings-xml-action@9dc09b23833fa9aa7f27b63db287951856f3433d # v22
with:
output_file: custom_maven_settings.xml
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
- name: Build and analyze (Pull Request)
if: ${{ github.event.workflow_run.event == 'pull_request' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_PR_BRANCH: ${{ fromJson(steps.get_pr_data.outputs.data).head.ref }}
SONAR_PR_KEY: ${{ fromJson(steps.get_pr_data.outputs.data).number }}
SONAR_PR_BASE: ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }}
SONAR_SCM_REVISION: ${{ github.event.workflow_run.head_sha }}
run: |
mvn -B -s custom_maven_settings.xml -Psonar \
${{ steps.sonar_env.outputs.sonar_opts }} \
"-Dsonar.pullrequest.branch=$SONAR_PR_BRANCH" \
"-Dsonar.pullrequest.key=$SONAR_PR_KEY" \
"-Dsonar.pullrequest.base=$SONAR_PR_BASE" \
"-Dsonar.scm.revision=$SONAR_SCM_REVISION" \
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
- name: Build and analyze (Push)
if: ${{ github.event.workflow_run.event == 'push' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_SCM_REVISION: ${{ github.event.workflow_run.head_sha }}
SONAR_BRANCH_NAME: ${{ github.event.workflow_run.head_branch }}
run: |
mvn -B -s custom_maven_settings.xml -Psonar \
${{ steps.sonar_env.outputs.sonar_opts }} \
"-Dsonar.scm.revision=$SONAR_SCM_REVISION" \
"-Dsonar.branch.name=$SONAR_BRANCH_NAME" \
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar