|
1 |
| -# Analyze your code for free with SonarCloud |
| 1 | +# Scan your code with SonarQube Cloud [](https://github.com/SonarSource/sonarcloud-github-action/actions/workflows/qa.yml) |
2 | 2 |
|
3 |
| -This SonarSource project, available as a GitHub Action, scans your projects with SonarCloud, and helps developers produce |
4 |
| -[Clean Code](https://www.sonarsource.com/solutions/clean-code/?utm_medium=referral&utm_source=github&utm_campaign=clean-code&utm_content=sonarqube-scan-action). |
| 3 | +> [!WARNING] |
| 4 | +> This action is deprecated and will be removed in a future release. |
| 5 | +> Please use the `sonarqube-scan-action` action instead. |
| 6 | +> The `sonarqube-scan-action` is a drop-in replacement for this action. |
5 | 7 |
|
6 |
| -<img src="./images/SonarCloud-72px.png"> |
| 8 | +This SonarSource project, available as a GitHub Action, scans your projects with SonarQube [Cloud](https://www.sonarsource.com/products/sonarcloud/). |
7 | 9 |
|
8 |
| -[SonarCloud](https://www.sonarsource.com/products/sonarcloud/) is a widely used static analysis solution for continuous code quality and security inspection. |
9 |
| -It helps developers identify and fix issues in their code that could lead to bugs, vulnerabilities, or decreased development velocity. |
10 |
| -SonarCloud supports the most popular programming languages, including Java, JavaScript, TypeScript, C#, Python, C, C++, and [many more](https://www.sonarsource.com/knowledge/languages/). |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +SonarQube [Cloud](https://www.sonarsource.com/products/sonarcloud/) (formerly SonarCloud) is a widely used static analysis solution for continuous code quality and security inspection. |
| 14 | + |
| 15 | +It helps developers detect coding issues in 30+ languages, frameworks, and IaC platforms, including Java, JavaScript, TypeScript, C#, Python, C, C++, and [many more](https://www.sonarsource.com/knowledge/languages/). |
| 16 | + |
| 17 | +The solution also provides fix recommendations leveraging AI with Sonar's AI CodeFix capability. |
11 | 18 |
|
12 | 19 | ## Requirements
|
13 | 20 |
|
14 |
| -* Create your account on SonarCloud. Sign up for free now if it's not already the case! [SonarCloud Sign up](https://www.sonarsource.com/products/sonarcloud/signup/?utm_medium=referral&utm_source=github&utm_campaign=sc-signup&utm_content=signup-sonarcloud-listing-x-x&utm_term=ww-psp-x) |
15 |
| -* The repository to analyze is set up on SonarCloud. [Set it up](https://sonarcloud.io/projects/create) in just one click. |
| 21 | +* Create your account on SonarQube Cloud. [Sign up for free](https://www.sonarsource.com/products/sonarcloud/signup/?utm_medium=referral&utm_source=github&utm_campaign=sc-signup&utm_content=signup-sonarcloud-listing-x-x&utm_term=ww-psp-x) now if it's not already the case! |
| 22 | +* The repository to analyze is set up on SonarQube Cloud. [Set it up](https://sonarcloud.io/projects/create) in just one click. |
16 | 23 |
|
17 | 24 | ## Usage
|
18 | 25 |
|
19 | 26 | Project metadata, including the location of the sources to be analyzed, must be declared in the file `sonar-project.properties` in the base directory:
|
20 | 27 |
|
21 | 28 | ```properties
|
22 |
| -sonar.organization=<replace with your SonarCloud organization key> |
23 |
| -sonar.projectKey=<replace with the key generated when setting up the project on SonarCloud> |
| 29 | +sonar.organization=<replace with your SonarQube Cloud organization key> |
| 30 | +sonar.projectKey=<replace with the key generated when setting up the project on SonarQube Cloud> |
24 | 31 |
|
25 | 32 | # relative paths to source directories. More details and properties are described
|
26 |
| -# in https://sonarcloud.io/documentation/project-administration/narrowing-the-focus/ |
| 33 | +# at https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/analysis-scope/ |
27 | 34 | sonar.sources=.
|
28 | 35 | ```
|
29 | 36 |
|
30 |
| -The workflow, usually declared in `.github/workflows/build.yml`, looks like: |
| 37 | +The workflow, usually declared under `.github/workflows`, looks like: |
31 | 38 |
|
32 | 39 | ```yaml
|
33 | 40 | on:
|
34 |
| - # Trigger analysis when pushing in master or pull requests, and when creating |
35 |
| - # a pull request. |
| 41 | + # Trigger analysis when pushing to your main branches, and when creating a pull request. |
36 | 42 | push:
|
37 | 43 | branches:
|
| 44 | + - main |
38 | 45 | - master
|
| 46 | + - develop |
| 47 | + - 'releases/**' |
39 | 48 | pull_request:
|
40 | 49 | types: [opened, synchronize, reopened]
|
| 50 | + |
41 | 51 | name: Main Workflow
|
42 | 52 | jobs:
|
43 |
| - sonarcloud: |
| 53 | + sonarqube: |
44 | 54 | runs-on: ubuntu-latest
|
45 | 55 | steps:
|
46 | 56 | - uses: actions/checkout@v4
|
47 | 57 | with:
|
48 |
| - # Disabling shallow clone is recommended for improving relevancy of reporting |
| 58 | + # Disabling shallow clones is recommended for improving the relevancy of reporting |
49 | 59 | fetch-depth: 0
|
50 |
| - - name: SonarCloud Scan |
51 |
| - uses: sonarsource/sonarcloud-github-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/sonarcloud-scan |
| 60 | + - name: SonarQube Scan |
| 61 | + uses: sonarsource/sonarcloud-github-action@<action version> # Ex: v4.0.0, See the latest version at https://github.com/marketplace/actions/sonarcloud-scan |
52 | 62 | env:
|
53 | 63 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
54 | 64 | ```
|
55 | 65 |
|
| 66 | +## Action parameters |
| 67 | +
|
56 | 68 | You can change the analysis base directory by using the optional input `projectBaseDir` like this:
|
57 | 69 |
|
58 | 70 | ```yaml
|
59 |
| -uses: sonarsource/sonarcloud-github-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/sonarcloud-scan |
60 |
| -with: |
61 |
| - projectBaseDir: my-custom-directory |
| 71 | +- uses: sonarsource/sonarcloud-github-action@<action version> |
| 72 | + with: |
| 73 | + projectBaseDir: app/src |
| 74 | +``` |
| 75 | + |
| 76 | +In case you need to specify the version of the Sonar Scanner, you can use the `scannerVersion` option: |
| 77 | + |
| 78 | +```yaml |
| 79 | +- uses: sonarsource/sonarcloud-github-action@<action version> |
| 80 | + with: |
| 81 | + scannerVersion: 6.2.0.4584 |
62 | 82 | ```
|
63 | 83 |
|
64 |
| -In case you need to add additional analysis parameters, you can use the `args` option: |
| 84 | +In case you need to add additional analysis parameters, and you do not wish to set them in the `sonar-project.properties` file, you can use the `args` option: |
65 | 85 |
|
66 | 86 | ```yaml
|
67 |
| -- name: Analyze with SonarCloud |
68 |
| - uses: sonarsource/sonarcloud-github-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/sonarcloud-scan |
| 87 | +- uses: sonarsource/sonarcloud-github-action@<action version> |
69 | 88 | with:
|
70 |
| - projectBaseDir: my-custom-directory |
| 89 | + projectBaseDir: app/src |
71 | 90 | args: >
|
72 | 91 | -Dsonar.organization=my-organization
|
73 | 92 | -Dsonar.projectKey=my-projectkey
|
74 | 93 | -Dsonar.python.coverage.reportPaths=coverage.xml
|
75 | 94 | -Dsonar.sources=lib/
|
76 |
| - -Dsonar.test.exclusions=tests/** |
77 | 95 | -Dsonar.tests=tests/
|
| 96 | + -Dsonar.test.exclusions=tests/** |
78 | 97 | -Dsonar.verbose=true
|
79 | 98 | ```
|
80 | 99 |
|
81 |
| -More information about possible analysis parameters is found in the documentation at: |
82 |
| -https://docs.sonarcloud.io/advanced-setup/analysis-parameters |
83 |
| - |
84 |
| -In case you need to specify the version of the Sonar Scanner, you can use the `scannerVersion` option: |
| 100 | +You can also specify the URL where to retrieve the SonarScanner CLI from. |
| 101 | +The specified URL overrides the default address: `https://binaries.sonarsource.com/Distribution/sonar-scanner-cli`. |
| 102 | +This can be useful when the runner executing the action is self-hosted and has regulated or no access to the Internet: |
85 | 103 |
|
86 | 104 | ```yaml
|
87 |
| -uses: sonarsource/sonarcloud-github-action@<action version> # Ex: v2.1.0, See the latest version at https://github.com/marketplace/actions/sonarcloud-scan |
88 |
| -with: |
89 |
| - scannerVersion: 6.2.0.4584 |
| 105 | +- uses: sonarsource/sonarcloud-github-action@<action version> |
| 106 | + with: |
| 107 | + scannerBinariesUrl: https://my.custom.binaries.url.com/Distribution/sonar-scanner-cli/ |
90 | 108 | ```
|
91 | 109 |
|
92 |
| -See also example configurations at: |
93 |
| -https://github.com/sonarsource/sonarcloud-github-action-samples/ |
| 110 | +More information about possible analysis parameters can be found in the [Analysis parameters page](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/analysis-parameters/) of the SonarQube Cloud documentation. |
94 | 111 |
|
95 |
| -### Secrets |
| 112 | +### Environment variables |
96 | 113 |
|
97 |
| -- `SONAR_TOKEN` – **Required** this is the token used to authenticate access to SonarCloud. You can generate a token on your [Security page in SonarCloud](https://sonarcloud.io/account/security/). You can set the `SONAR_TOKEN` environment variable in the "Secrets" settings page of your repository. |
| 114 | +- `SONAR_TOKEN` – **Required** this is the token used to authenticate access to SonarQube. You can read more about security tokens in the [documentation](https://docs.sonarsource.com/sonarqube-cloud/managing-your-account/managing-tokens/). You can set the `SONAR_TOKEN` environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended). |
98 | 115 | - *`GITHUB_TOKEN` – Provided by Github (see [Authenticating with the GITHUB_TOKEN](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token)).*
|
| 116 | +- `SONAR_ROOT_CERT` – Holds an additional certificate (in PEM format) that is used to validate the certificate of a secured proxy to SonarQube Cloud. You can set the `SONAR_ROOT_CERT` environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended). |
99 | 117 |
|
100 |
| -## Error cleaning up workspace |
| 118 | +Here is an example of how you can pass a certificate (in PEM format) to the Scanner truststore: |
101 | 119 |
|
102 |
| -In some cases, the checkout action may fail to clean up the workspace. This is a known problem for GitHub actions implemented as a docker container (such as `sonarcloud-github-actions`) when self-hosted runners are used. |
103 |
| -Example of the error message: `File was unable to be removed Error: EACCES: permission denied, unlink '/actions-runner/_work//project/.scannerwork/.sonar_lock'` |
104 |
| -To work around the problem, `sonarcloud-github-action` attempts to fix the permission of the temporary files that it creates. If that doesn't work, you can manually clean up the workspace by running the following action: |
105 |
| -``` |
106 |
| -- name: Clean the workspace |
107 |
| - uses: docker://alpine |
108 |
| - with: |
109 |
| - args: /bin/sh -c "find \"${GITHUB_WORKSPACE}\" -mindepth 1 ! -name . -prune -exec rm -rf {} +" |
| 120 | +```yaml |
| 121 | +- uses: sonarsource/sonarcloud-github-action@<action version> |
| 122 | + env: |
| 123 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 124 | + SONAR_ROOT_CERT: ${{ secrets.SONAR_ROOT_CERT }} |
110 | 125 | ```
|
111 |
| -You can find more info [here](https://github.com/actions/runner/issues/434). |
112 | 126 |
|
113 |
| -## Example of pull request analysis |
| 127 | +If your source code file names contain special characters that are not covered by the locale range of `en_US.UTF-8`, you can configure your desired locale like this: |
114 | 128 |
|
115 |
| -<img src="./images/SonarCloud-analysis-in-Checks.png"> |
| 129 | +```yaml |
| 130 | +- uses: sonarsource/sonarcloud-github-action@<action version> |
| 131 | + env: |
| 132 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 133 | + LC_ALL: "ru_RU.UTF-8" |
| 134 | +``` |
116 | 135 |
|
117 |
| -Want to see more examples of SonarCloud in action? You can [explore current Open Source projects in SonarCloud](https://sonarcloud.io/explore/projects?sort=-analysis_date?utm_medium=referral&utm_source=github&utm_campaign=sc-signup&utm_content=signup-sonarcloud-listing-x-x&utm_term=ww-psp-x) that are using the Clean as You Code methodology. |
| 136 | +## Alternatives for Java, .NET, and C/C++ projects |
118 | 137 |
|
119 |
| -## Do not use this GitHub action if you are in the following situations |
| 138 | +This GitHub Action will not work for all technologies. If you are in one of the following situations, you should use the following alternatives: |
120 | 139 |
|
121 |
| -* Your code is built with Maven: run 'org.sonarsource.scanner.maven:sonar' during the build |
122 |
| -* Your code is built with Gradle: use the SonarQube plugin for Gradle during the build |
123 |
| -* You want to analyze a .NET solution: Follow our interactive tutorial for GitHub Actions after importing your project directly into SonarCloud |
124 |
| -* You want to analyze C and C++ code: rely on our [SonarCloud Scan for C and C++](https://github.com/marketplace/actions/sonarcloud-scan-for-c-and-c) and look at [our sample C and C++ project](https://github.com/sonarsource-cfamily-examples?q=gh-actions-sc&type=all&language=&sort=) |
| 140 | +* Your code is built with Maven. Read the documentation about our [SonarScanner for Maven](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-for-maven/). |
| 141 | +* Your code is built with Gradle. Read the documentation about our [SonarScanner for Gradle](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-for-gradle/). |
| 142 | +* You want to analyze a .NET solution. Read the documentation about our [SonarScanner for .NET](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-for-dotnet/introduction/). |
| 143 | +* You want to analyze C or C++ code. Starting from SonarQube 10.6, this GitHub Action will scan C and C++ out of the box. If you want to have better control over the scan configuration/setup, you can switch to the [SonarQube Cloud Scan for C and C++](https://github.com/marketplace/actions/sonarcloud-scan-for-c-and-c) GitHub Action - look at [our sample C and C++ project](https://github.com/sonarsource-cfamily-examples?q=gh-actions-sc&type=all&language=&sort=). |
125 | 144 |
|
126 | 145 | ## Have questions or feedback?
|
127 | 146 |
|
128 |
| -To provide feedback (requesting a feature or reporting a bug) please post on the [SonarSource Community Forum](https://community.sonarsource.com/) with the tag `sonarcloud`. |
| 147 | +To provide feedback (requesting a feature or reporting a bug) please post on the [SonarSource Community Forum](https://community.sonarsource.com/tags/c/help/sc/9/github-actions). |
129 | 148 |
|
130 | 149 | ## License
|
131 | 150 |
|
|
0 commit comments