SonarScanner "You must define the following mandatory properties: sonar.projectKey" in CI
The scanner needs a sonar.projectKey to know which project to attach the analysis to. It found none on the command line, in sonar-project.properties, or in the action inputs, so it stops before analyzing.
What this error means
The scanner exits immediately with "ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey".
sonar-scanner
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.Common causes
No project key is set anywhere
Neither a sonar-project.properties file nor a -Dsonar.projectKey= argument nor an action input provides the key.
The properties file is not in the scan working directory
A sonar-project.properties exists but the scanner runs from a different directory and never reads it.
How to fix it
Provide the project key explicitly
- Get the project key from the project in SonarQube or SonarCloud.
- Set it in
sonar-project.propertiesat the scan root, or pass it as an argument. - Re-run so the scanner attaches the analysis to the right project.
sonar-project.properties
sonar.projectKey=my-org_my-repo
sonar.organization=my-orgPass the key through the action inputs
When using a scan action, set the arguments so the key is present even without a properties file.
.github/workflows/ci.yml
- uses: SonarSource/sonarqube-scan-action@v4
with:
args: >
-Dsonar.projectKey=my-org_my-repoHow to prevent it
- Commit
sonar-project.propertieswith the project key at the repo root. - Run the scanner from the directory that holds the properties file.
- Keep the key identical to the one shown in the project settings.
Related guides
SonarCloud "Project not found. Please check the 'sonar.projectKey'" in CIFix SonarCloud "Project not found. Please check the 'sonar.projectKey' and 'sonar.organization' properties" i…
SonarScanner "No files nor directories matching sonar.sources" in CIFix SonarScanner "No files nor directories matching ... in sonar.sources" in CI - the configured source path…
SonarScanner "Not authorized. Please check the properties sonar.login" in CIFix SonarScanner "Not authorized. Please check the properties sonar.login, sonar.password or sonar.token" in…