-
Notifications
You must be signed in to change notification settings - Fork 89
Add SonarCloud analysis workflow #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # 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 helps you trigger a SonarCloud analysis of your code and populates | ||
| # GitHub Code Scanning alerts with the vulnerabilities found. | ||
| # Free for open source project. | ||
|
|
||
| # 1. Login to SonarCloud.io using your GitHub account | ||
|
|
||
| # 2. Import your project on SonarCloud | ||
| # * Add your GitHub organization first, then add your repository as a new project. | ||
| # * Please note that many languages are eligible for automatic analysis, | ||
| # which means that the analysis will start automatically without the need to set up GitHub Actions. | ||
| # * This behavior can be changed in Administration > Analysis Method. | ||
| # | ||
| # 3. Follow the SonarCloud in-product tutorial | ||
| # * a. Copy/paste the Project Key and the Organization Key into the args parameter below | ||
| # (You'll find this information in SonarCloud. Click on "Information" at the bottom left) | ||
| # | ||
| # * b. Generate a new token and add it to your Github repository's secrets using the name SONAR_TOKEN | ||
| # (On SonarCloud, click on your avatar on top-right > My account > Security | ||
| # or go directly to https://sonarcloud.io/account/security/) | ||
|
|
||
| # Feel free to take a look at our documentation (https://docs.sonarcloud.io/getting-started/github/) | ||
| # or reach out to our community forum if you need some help (https://community.sonarsource.com/c/help/sc/9) | ||
|
|
||
| name: SonarCloud analysis | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ "develop" ] | ||
| pull_request: | ||
| branches: [ "develop" ] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| pull-requests: read # allows SonarCloud to decorate PRs with analysis results | ||
|
|
||
| jobs: | ||
| Analysis: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Analyze with SonarCloud | ||
|
|
||
| # You can pin the exact commit or the version. | ||
| # uses: SonarSource/sonarcloud-github-action@v2.2.0 | ||
| uses: SonarSource/sonarcloud-github-action@4006f663ecaf1f8093e8e4abb9227f6041f52216 | ||
|
Comment on lines
+46
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- .github/workflows/sonarcloud.yml ---'
cat -n .github/workflows/sonarcloud.yml
printf '%s\n' '--- pom.xml ---'
cat -n pom.xml
printf '%s\n' '--- sonar-project.properties ---'
if [ -f sonar-project.properties ]; then
cat -n sonar-project.properties
else
printf '%s\n' 'missing'
fi
printf '%s\n' '--- Sonar-related workflow and Maven references ---'
rg -n --glob '.github/workflows/**' --glob 'pom.xml' --glob '**/pom.xml' \
'sonar|checkout|permissions:|pull-requests:|maven' .Repository: opentiny/tiny-engine-backend-java Length of output: 22012 🌐 Web query:
💡 Result: The Citations:
Use the Maven scanner for this Maven reactor.
🧰 Tools🪛 zizmor (1.29.0)[warning] 50-50: action or reusable workflow from archived repository (archived-uses): repository is archived (archived-uses) 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||
| env: | ||
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} # Generate a token on Sonarcloud.io, add it to the secrets of this repo with the name SONAR_TOKEN (Settings > Secrets > Actions > add new repository secret) | ||
| with: | ||
| # Additional arguments for the SonarScanner CLI | ||
| args: | ||
| # Unique keys of your project and organization. You can find them in SonarCloud > Information (bottom-left menu) | ||
| # mandatory | ||
| -Dsonar.projectKey= | ||
| -Dsonar.organization= | ||
|
Comment on lines
+53
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '1,90p' .github/workflows/sonarcloud.yml
printf '\n--- project properties ---\n'
if [ -f sonar-project.properties ]; then cat -n sonar-project.properties; fi
printf '\n--- Maven configuration ---\n'
sed -n '1,180p' pom.xmlRepository: opentiny/tiny-engine-backend-java Length of output: 10402 🌐 Web query:
💡 Result: Yes, sonar.projectKey and sonar.organization are mandatory parameters for identifying your project and organization when using the SonarCloud GitHub Action and the SonarScanner CLI [1][2][3][4]. These properties must be defined to ensure the scanner can correctly map your analysis results to the appropriate project in SonarQube Cloud [5][3]. You can provide these mandatory parameters in two primary ways: 1. sonar-project.properties file: It is standard practice to declare these properties in a sonar-project.properties file located in the base directory of your project [1][2][3]. Example format: sonar.organization=your-organization-key sonar.projectKey=your-project-key 2. Command line arguments or Action configuration: - For the SonarCloud GitHub Action, if you prefer not to use the properties file, you can pass these parameters via the args input in your workflow YAML [1][2]. Example: - uses: sonarsource/sonarcloud-github-action@ with: args: > -Dsonar.organization=your-organization-key -Dsonar.projectKey=your-project-key - For the SonarScanner CLI, you can define them directly in your command execution using the -D flag (e.g., -Dsonar.projectKey=my-project) [6][3][4]. Note that environment variables (e.g., SONAR_PROJECT_KEY) are often ineffective for these specific properties because the SonarScanner expects them as configuration properties rather than generic environment variables [7]. Always ensure your projectBaseDir is correctly set if your properties file is not in the root directory, as the scanner resolves file paths relative to the execution directory [6][3][4]. Citations:
Set the SonarCloud project identity. The workflow passes empty 🤖 Prompt for AI Agents |
||
| # Comma-separated paths to directories containing main source files. | ||
| #-Dsonar.sources= # optional, default is project base directory | ||
| # Comma-separated paths to directories containing test source files. | ||
| #-Dsonar.tests= # optional. For more info about Code Coverage, please refer to https://docs.sonarcloud.io/enriching/test-coverage/overview/ | ||
| # Adds more detail to both client and server-side analysis logs, activating DEBUG mode for the scanner, and adding client-side environment variables and system properties to the server-side log of analysis report processing. | ||
| #-Dsonar.verbose= # optional, default is false | ||
| # When you need the analysis to take place in a directory other than the one from which it was launched, default is . | ||
| projectBaseDir: . | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: opentiny/tiny-engine-backend-java
Length of output: 21107
🌐 Web query:
SonarSource sonarcloud-github-action README checkout projectBaseDir permissions contents read💡 Result:
To use the SonarCloud GitHub Action effectively, here is the synthesis regarding projectBaseDir and GITHUB_TOKEN permissions: projectBaseDir The projectBaseDir input is an optional parameter used to specify the base directory for analysis [1][2]. By default, the action looks for the sonar-project.properties file in the root of the repository [1][3]. If your project metadata or source code resides in a subdirectory (e.g., in a monorepo setup), you can override this by providing the path to that directory [1][3]. Example configuration: - uses: sonarsource/sonarcloud-github-action@ with: projectBaseDir: app/src Note that SonarSource has transitioned to using sonarqube-scan-action for most modern use cases [3][4]. If you encounter a "Container action is only supported on Linux" error, you should migrate from the older sonarcloud-github-action to the newer sonarqube-scan-action [3]. GITHUB_TOKEN Permissions While the SonarCloud scan itself requires a SONAR_TOKEN, the GITHUB_TOKEN is used by the action to interact with GitHub, specifically to retrieve pull request information and decorate PRs with analysis results [5]. To allow SonarCloud to perform PR decoration, the GITHUB_TOKEN requires read access to pull requests [5]. In your GitHub Actions workflow, you should explicitly define these permissions: permissions: pull-requests: read contents: read # Often required by actions/checkout to read repository code [3][5] If your workflow requires broader access for other steps, using permissions: read-all is a convenient alternative [5]. Ensure these permissions are set at either the job level or the workflow level, depending on your requirements [5].
Citations:
Check out the repository before the scan.
The job has no
actions/checkoutstep, so the SonarCloud action cannot analyze the repository source files orsonar-project.properties. Add a pinnedactions/checkoutstep before the SonarCloud action. Also grantcontents: read, because the explicitpermissionsblock otherwise prevents checkout from reading the repository.🧰 Tools
🪛 zizmor (1.29.0)
[warning] 50-50: action or reusable workflow from archived repository (archived-uses): repository is archived
(archived-uses)
🤖 Prompt for AI Agents