Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 37 ยท Getting Started

Your First Analysis

Walk through a first SonarQube scan: create a project, generate a token, add sonar-project.properties, run the scanner and read the result. Includes a properties validator.

This lesson follows the path every new user takes: log in, create a project, give the scanner a token, run it and look at the result. The steps below describe the real product; the runnable parts are stand-ins that check your configuration the way a careful teammate would. Menu labels can differ slightly between versions.

The five steps

  1. Log in. On a fresh local server the address is http://localhost:9000, and the first login is admin / admin; you are asked to set a new password immediately.
  2. Create a project. Choose Create a project and pick Manually (or import from a code host in editions that support it). Give it a project key, a display name and the main branch name.
  3. Choose the analysis method. For a first test pick Locally, then generate a token. A token is a secret you pass to the scanner instead of your password. Copy it at once; the server shows it only one time.
  4. Describe the project. Place a file named sonar-project.properties in the repository root.
  5. Run the scanner. From the repository root run sonar-scanner. The scanner uploads a report, and after a short wait the project page shows the results.

The properties file

A minimal file for a Python project looks like this:

PROPERTIES
sonar.projectKey=my-shop
sonar.projectName=My Shop
sonar.sources=src
sonar.tests=tests
sonar.sourceEncoding=UTF-8
sonar.host.url=http://localhost:9000

Do not put the token in this file: it would be committed. Pass it as an environment variable (SONAR_TOKEN) or on the command line (-Dsonar.token=...). In older versions the property was sonar.login; current versions use sonar.token.

Terminal
export SONAR_TOKEN=your-token-here
sonar-scanner

Never commit tokens or passwords. In CI store the token as a secret and expose it as an environment variable only to the scan step.

Validating a properties file

Typos in this file cost minutes of waiting for a failed scan. This stand-in parses the text and checks the common mistakes: missing key, token placed in the file, unknown path, bad host URL.

Python Validate sonar-project.properties (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Building the command line

Every property can also be given with -D. This is how CI passes values that differ per run, such as the branch or version. The stand-in assembles the command from a dictionary and leaves the token to the environment.

Python Build the scanner command (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading the first result

When the scan finishes the project page opens on Overview. Look at it in this order:

  1. Quality Gate status, a big Passed or Failed. On a first scan everything counts as new code, so the gate may fail; that is normal.
  2. Measures: bugs, vulnerabilities, hotspots to review, code smells, coverage (shows no value until you import coverage reports, see Coverage and Technical Debt), duplications and size.
  3. Issues tab: click any issue to see the rule, the code and a Why is this an issue? explanation.

If the page shows nothing, the report may still be processing; scanners print a link to the Compute Engine task at the end of the run. The next lesson explains what the server does with the report.

Common first-scan problems

  • Not authorized: the token is wrong or lacks the Execute Analysis permission.
  • Project key mismatch: the key in the file differs from the one you created, so a second project may appear.
  • No sources found: sonar.sources points at a folder that does not exist relative to where you ran the command.
  • Connection refused: the server is not running or sonar.host.url is wrong.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should the authentication token be stored for a scan?

    Tokens are secrets; pass them through the environment or a CI secret, never commit them.
  2. 2. Which property names the project in SonarQube uniquely?

    sonar.projectKey identifies the project; the name is only a display label.
  3. 3. Why might a first scan fail the quality gate?

    On the first analysis there is nothing to compare with, so all code is treated as new.
  4. 4. What does the scanner upload to the server?

    The scanner analyzes locally and sends a report that the server processes.

Summary

  • Steps: log in, create a project, make a token, add sonar-project.properties, run sonar-scanner.
  • sonar.projectKey, sonar.sources and sonar.host.url are the core properties; the token goes in SONAR_TOKEN.
  • Read results in order: gate, measures, issues.
  • Most first-scan failures are wrong tokens, paths or URLs.

Next: Architecture and Components