To generate a SonarQube token (required for authentication when running analyses from the command line or CI/CD pipelines), follow these steps:
Steps to Generate a SonarQube Token
- Log in to your SonarQube server (e.g.,
http://localhost:9000
for local setups). - Click your profile icon (top-right corner) β “My Account”.
- Go to the “Security” tab.
- Under “Tokens”, enter a name for your token (e.g.,
vs-console-token
). - Click “Generate”.
- Copy the token immediately (it wonβt be shown again!).
Example token format:sqp_1234567890abcdef
How to Use the Token
- In
dotnet-sonarscanner
commands, pass the token via:shCopyDownloaddotnet sonarscanner begin /k:”your-project-key” /d:sonar.host.url=”http://localhost:9000″ /d:sonar.login=”sqp_1234567890abcdef” - For security, never hardcode the token in scripts. Use:
- Environment variables (e.g.,
SONAR_TOKEN
). - Secret management tools (e.g., Azure Key Vault, GitHub Secrets).
- Environment variables (e.g.,
Important Notes
- π Treat tokens like passwords (they grant access to your SonarQube projects).
- π Regenerate tokens periodically or revoke old ones (under “Security”).
- π« No token? Youβll get errors like
Not authorized
orAuthentication failed
.
Example Workflow
# Set token as an environment variable (optional) set SONAR_TOKEN=sqp_1234567890abcdef # Run analysis (Windows CMD) dotnet sonarscanner begin /k:"my-project" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="%SONAR_TOKEN%" dotnet build dotnet sonarscanner end /d:sonar.login="%SONAR_TOKEN%"
Get the SonarQube Project URL
The project URL is the web address of your project in SonarQube. It typically follows this format:
http://<sonarqube-server-url>/dashboard?id=<project-key>
<sonarqube-server-url>
: The host where SonarQube is running (e.g.,http://localhost:9000
if running locally).<project-key>
: The unique key assigned to your project in SonarQube.
How to Find the Project Key?
- Log in to your SonarQube server.
- Navigate to your project.
- Check the URL in the browser’s address bar (e.g.,
http://localhost:9000/dashboard?id=my-project-key
). - Alternatively, go to Project Settings β General Settings β Key.
2. Run SonarQube Analysis in Visual Studio Console
To analyze a .NET project in Visual Studio Developer Command Prompt (or terminal), use the SonarScanner for .NET (dotnet-sonarscanner
).
Prerequisites
- Install Java (required for SonarQube scanner).
- Install SonarScanner for .NET:shCopyDownloaddotnet tool install –global dotnet-sonarscanner
Steps to Run Analysis
- Start the SonarQube Analysis:shCopyDownloaddotnet sonarscanner begin /k:”
” /d:sonar.host.url=” ” /d:sonar.login=” “ - Replace:
<project-key>
with your SonarQube project key.<sonarqube-server-url>
with your SonarQube server URL (e.g.,http://localhost:9000
).<token>
with a SonarQube user token.
- Replace:
- Build Your Project:shCopyDownloaddotnet build
- Complete & Publish Results to SonarQube:shCopyDownloaddotnet sonarscanner end /d:sonar.login=”<token>”
- Check Results:
- Open the SonarQube project URL (e.g.,
http://localhost:9000/dashboard?id=my-project-key
) in a browser.
- Open the SonarQube project URL (e.g.,
Example
# Start analysis dotnet sonarscanner begin /k:"my-dotnet-app" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="sqp_1234567890abcdef" # Build the project dotnet build # End analysis & upload results dotnet sonarscanner end /d:sonar.login="sqp_1234567890abcdef"
After running these commands, your analysis results will appear in the SonarQube dashboard.