| 1 |
# SonarQube Cloud in amazon.aws |
| 2 |
|
| 3 |
SonarQube Cloud (SonarCloud) is a Software-as-a-Service (SaaS) code analysis tool that helps maintain code quality by identifying issues related to maintainability, reliability, and security. |
| 4 |
|
| 5 |
The amazon.aws collection uses SonarQube Cloud to analyze the `main` branch and pull requests, and to track unit test coverage and quality gate compliance. |
| 6 |
|
| 7 |
## Core concepts |
| 8 |
|
| 9 |
1. **Clean as You Code**: Development practice where new code must meet quality standards. |
| 10 |
|
| 11 |
2. **Clean Code Attributes**: Consistency, Intentionality, Adaptability, and Responsibility. See [Code analysis metrics](https://docs.sonarsource.com/sonarqube-cloud/digging-deeper/metric-definitions/). |
| 12 |
|
| 13 |
3. **Software Quality**: SonarQube Cloud evaluates quality by flagging issues that violate clean code principles. |
| 14 |
|
| 15 |
4. **Quality Standards**: Defined by a quality profile (rules) and a quality gate (conditions that must pass). The gate shows pass or fail based on whether all conditions are met. |
| 16 |
|
| 17 |
## Analysis method: CI-based analysis |
| 18 |
|
| 19 |
The collection uses **CI-based analysis** with GitHub Actions: |
| 20 |
|
| 21 |
- The SonarScanner runs inside the CI build. |
| 22 |
- Configuration is controlled in the repository (properties file and workflow). |
| 23 |
- **Code coverage** is produced by a dedicated coverage job (via `tox`) before the SonarCloud workflow runs; coverage reports are downloaded and passed to the scanner. |
| 24 |
|
| 25 |
Automatic analysis is not used because it does not support code coverage and has limited branch support. |
| 26 |
|
| 27 |
References: |
| 28 |
|
| 29 |
- [CI-Based Analysis overview](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/overview-of-integrated-cis/) |
| 30 |
- [Test coverage in SonarCloud](https://docs.sonarsource.com/sonarqube-cloud/enriching/test-coverage/overview/) |
| 31 |
|
| 32 |
## Configuration files |
| 33 |
|
| 34 |
### `sonar-project.properties` |
| 35 |
|
| 36 |
Project-level analysis settings live in `sonar-project.properties` at the repository root: |
| 37 |
|
| 38 |
| Parameter | Value | Purpose | |
| 39 |
|-----------|--------|---------| |
| 40 |
| `sonar.projectKey` | `ansible-collections_amazon.aws` | SonarCloud project identifier | |
| 41 |
| `sonar.organization` | `ansible-collections` | SonarCloud organization | |
| 42 |
| `sonar.projectName` | `amazon.aws` | Display name | |
| 43 |
| `sonar.sources` | `.` | Root of analyzed source tree | |
| 44 |
| `sonar.tests` | `tests/unit,tests/integration` | Test directories (for test-aware analysis) | |
| 45 |
| `sonar.exclusions` | `tests/**,.tox/**` | Paths excluded from analysis | |
| 46 |
| `sonar.python.coverage.reportPaths` | `coverage.xml` | Default coverage report path (overridden by CI when present) | |
| 47 |
| `sonar.python.version` | `3.13` | Python version for analysis | |
| 48 |
| `sonar.newCode.referenceBranch` | `main` | Branch used as baseline for "new code" | |
| 49 |
|
| 50 |
Full reference: [Analysis parameters](https://docs.sonarqube.org/latest/analysis/analysis-parameters/). |
| 51 |
|
| 52 |
### `tox.ini` (coverage generation) |
| 53 |
|
| 54 |
Unit test coverage is produced by **tox**. The default test environment runs `pytest` with `pytest-cov`: |
| 55 |
|
| 56 |
- **Coverage scope**: `plugins/callback`, `plugins/inventory`, `plugins/lookup`, `plugins/module_utils`, `plugins/modules`, `plugins/plugin_utils`, and `plugins`. |
| 57 |
- **Reports**: HTML (`--cov-report html`) and Cobertura XML (`--cov-report xml:coverage.xml`). |
| 58 |
- **Output**: `coverage.xml` is written under the `tox` environment directory (e.g. `.tox/ansible2.20-py314-with_constraints/...`). |
| 59 |
|
| 60 |
The CI coverage job uses the environment `ansible2.20-py314-with_constraints` so that a single, consistent coverage report is produced for SonarCloud. |
| 61 |
|
| 62 |
### `pyproject.toml` (coverage and `pytest`) |
| 63 |
|
| 64 |
`pyproject.toml` does not define SonarQube-specific settings. It configures tools used by tests and coverage: |
| 65 |
|
| 66 |
- **`[tool.pytest]`**: Pytest options (e.g. `xfail_strict = true`). |
| 67 |
- **`[tool.coverage.report]`**: Coverage report options, including `also_exclude` for lines that are not required to be covered (e.g. `raise NotImplementedError`). |
| 68 |
|
| 69 |
These affect how coverage is generated (and thus what appears in `coverage.xml`), but SonarCloud is configured only via `sonar-project.properties` and the workflow arguments. |
| 70 |
|
| 71 |
## GitHub Actions integration |
| 72 |
|
| 73 |
Two workflows implement the integration: |
| 74 |
|
| 75 |
1. **`all_green`** (`.github/workflows/all_green_check.yml`) runs on pull requests and pushes to `main` and `stable-*`. It runs linters, sanity, units, and a **coverage** job. When all required jobs succeed, it triggers the SonarCloud workflow via `workflow_run`. |
| 76 |
|
| 77 |
2. **SonarCloud** (`.github/workflows/sonarcloud.yml`) runs after **all_green** completes successfully. It checks out the repo, downloads the coverage artifact from that run, locates the coverage XML, gathers PR metadata when applicable, and runs the SonarScanner with the appropriate arguments (including coverage paths). |
| 78 |
|
| 79 |
### Why `workflow_run` |
| 80 |
|
| 81 |
- **Secrets**: Workflows triggered by `pull_request` from forks run in the fork context and do not have access to repository secrets (e.g. `SONAR_TOKEN`). By triggering SonarCloud from **all_green** via `workflow_run`, the SonarCloud job runs in the upstream repository context and can use the token. |
| 82 |
- **Ordering**: SonarCloud runs only after `all_green` (including the coverage job) has finished, so coverage data is available when the scanner runs. |
| 83 |
|
| 84 |
Flow: |
| 85 |
|
| 86 |
1. PR or `push` triggers **`all_green`** (linters, sanity, units, coverage). |
| 87 |
2. The **coverage** job runs `tox -e ansible2.20-py314-with_constraints`, finds `coverage.xml` under `.tox`, rewrites paths in the XML to be repo-relative, and uploads it as the `coverage` artifact. |
| 88 |
3. When **`all_green`** completes successfully, the **SonarCloud** workflow is triggered. |
| 89 |
4. The SonarCloud job checks out the repo at the run's commit, downloads the `coverage` artifact, sets `COVERAGE_PATHS`, and runs the scanner with `sonar.python.coverage.reportPaths` set so SonarCloud receives the coverage report. |
| 90 |
|
| 91 |
### `all_green_check.yml`: coverage job |
| 92 |
|
| 93 |
- **Trigger**: Same as `all_green` (`pull_request` and push to `main` and `stable-*`). The coverage job runs on every such run (linters/sanity only run on `pull_request`; units and coverage always run). |
| 94 |
- **Steps**: |
| 95 |
1. Checkout repository. |
| 96 |
2. Set up Python 3.14 and install `tox`. |
| 97 |
3. Run `tox -e ansible2.20-py314-with_constraints` to execute unit tests and generate `coverage.xml` under `.tox`. |
| 98 |
4. **Rewrite coverage paths**: Locate `coverage.xml` under `.tox`, then replace the absolute path prefix (the `tox` env collection path) with an empty string so paths in the XML are repo-relative. This allows SonarCloud to match coverage to the repository layout when it checks out the same commit. |
| 99 |
5. Upload the rewritten `coverage.xml` as the artifact named `coverage`. |
| 100 |
- **Gate**: The **`all_green`** job only succeeds when linters (on PRs), sanity (on PRs), units, and coverage all succeed (or are skipped where allowed). |
| 101 |
|
| 102 |
### `sonarcloud.yml`: finalize job |
| 103 |
|
| 104 |
- **Trigger**: `workflow_run` when the **`all_green`** workflow completes (success only). |
| 105 |
- **Permissions**: `contents: read`, `pull-requests: read`, `actions: read` (needed to download artifacts from the triggering run). |
| 106 |
- **Steps**: |
| 107 |
1. **Checkout** the repository at `workflow_run.head_sha`. |
| 108 |
2. **Download coverage artifacts** from the triggering all_green run using `dawidd6/action-download-artifact` (pinned by full commit SHA, v16 equivalent). Pattern: `coverage*`. |
| 109 |
3. **Set coverage report paths**: Run `find . -name "coverage*.xml"`, comma-separate the paths, and set `COVERAGE_PATHS` in the environment. |
| 110 |
4. **Get PR number and info** (when the triggering event was a pull_request): Use `gh pr list` and `gh api` to set `PR_NUMBER`, `PR_BASE`, and `PR_HEAD`. |
| 111 |
5. **Prepare SonarCloud args**: Set `sonar.scm.revision` to the commit SHA. For pull requests, add `sonar.pullrequest.key`, `sonar.pullrequest.branch`, and `sonar.pullrequest.base`. When `COVERAGE_PATHS` is set, add `sonar.python.coverage.reportPaths`. |
| 112 |
6. **SonarCloud Scan**: Run `SonarSource/sonarqube-scan-action@v7` with the prepared args and `SONAR_TOKEN` from repository secrets. |
| 113 |
|
| 114 |
Coverage is only passed to the scanner when the download step finds at least one `coverage*.xml` file; otherwise the scan runs without coverage (and may rely on the default path in `sonar-project.properties` if no args override it). |
| 115 |
|
| 116 |
## Summary |
| 117 |
|
| 118 |
| Workflow | File | Trigger | Purpose | |
| 119 |
|----------|------|---------|---------| |
| 120 |
| `all_green` | `.github/workflows/all_green_check.yml` | `pull_request`, push to `main` and `stable-*` | Run linters, sanity, units, and coverage; gate on success | |
| 121 |
| SonarCloud | `.github/workflows/sonarcloud.yml` | After `all_green` completes successfully | Download coverage from `all_green` run and run SonarScanner with PR or branch analysis | |
| 122 |
|
| 123 |
| Artifact / config | Source | Use | |
| 124 |
|-------------------|--------|-----| |
| 125 |
| Coverage XML | `tox` (coverage job) → path rewrite → artifact `coverage` | Downloaded by SonarCloud job and passed as `sonar.python.coverage.reportPaths` | |
| 126 |
| `sonar-project.properties` | Repository root | Project key, organization, sources, tests, exclusions, default coverage path, Python version, reference branch | |
| 127 |
| `tox.ini` | Repository root | Defines testenv that runs `pytest` with `pytest-cov` and writes `coverage.xml` | |
| 128 |
| `pyproject.toml` | Repository root | Pytest and coverage report options (`exclude_lines`, etc.); no SonarCloud-specific config | |
| 129 |
|
| 130 |
## Debugging SonarCloud issues |
| 131 |
|
| 132 |
If analysis fails or coverage is missing: |
| 133 |
|
| 134 |
1. **Check `all_green`**: Ensure the `all_green` workflow (including the coverage job) succeeded for the same commit. SonarCloud downloads artifacts from that run. |
| 135 |
2. **Check artifact**: In the SonarCloud run, confirm the "Download coverage artifacts" and "Set coverage report paths steps" found at least one `coverage*.xml` and set `COVERAGE_PATHS`. |
| 136 |
3. **Run SonarScanner locally**: Install [SonarScanner CLI](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-cli/), set `SONAR_TOKEN` from [SonarCloud Security](https://sonarcloud.io/account/security), and run from the repo root: |
| 137 |
```sh |
| 138 |
sonar-scanner -Dsonar.projectBaseDir=. -Dsonar.host.url=https://sonarcloud.io |
| 139 |
``` |
| 140 |
Fix any errors reported at the end of the output. |
| 141 |
|
| 142 |
*Note:* Changes that break `sonar-project.properties` can still leave the PR status green because the SonarCloud workflow may not block the check. Use the SonarCloud project page and local `sonar-scanner` runs to verify configuration. |
| 143 |
|
| 144 |
## References |
| 145 |
|
| 146 |
- [SonarCloud Documentation](https://docs.sonarsource.com/sonarqube-cloud/) |
| 147 |
- [GitHub Actions for SonarCloud](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/github-actions-for-sonarcloud/) |
| 148 |
- [Storing and sharing workflow data (artifacts)](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/storing-and-sharing-data-from-a-workflow) |
| 149 |
- [workflow_run event](https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#workflow_run) |