SonarCloud Integration with AWS CodeBuild (& Docker)

  • ALM used: Bitbucket Cloud
  • CI system used: AWS CodeBuild with Docker
  • Languages of the repository: Javascript (Angular)
  • Error observed:
When running: docker run --rm -e SONAR_HOST_URL=$SONAR_URL -e SONAR_LOGIN=$SONAR_TOKEN -v $CODEBUILD_SRC_DIR:/usr/src sonarsource/sonar-scanner-cli  I get this error: ERROR: Error during SonarScanner execution
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.organization```

And when I try to add -D sonar.projectKey=myProjKey -D sonar.organization=myOrg(to the docker command, it doesn't work, I get this error: unknown shorthand flag: 'D' in -D)

How do I inject the missing parameters? (There is no info I can find about this in the documentation

Hi Magnus,

Welcome to the community support!

The space between the -D and the property key definition must be removed if I’m not mistaken:

-Dsonar.projectKey=myProjKey -Dsonar.organization=myOrg

Best.

Hi Olivier,

Thanks for the welcome! :slight_smile:

I get this error when change to your proposed solution:
unknown shorthand flag: ‘D’ in -Dsonar.projectKey=myProjectKey

from Docker I believe the error message is

BR
//Magnus

Hi,

Could try to run this command line (I mean the whole docker command) outside of the CodeBuild context?

Try to run the scanner CLI without Docker, then add Docker, then add CodeBuild.

When I have such problems I try to remove all layers on top of what I try to run to undersand wether the issue is in the command or in the layers which require some extra steps to make it work (char escaping, …).

I use a personal Pyhton CLI tool I built to wrap the Sonar Scanner CLI and I can confirm the -D syntax.
Here is an extract of the code :

 def __run(self):

    java_args = ''
    for key, value in self.__java_options.items():
        java_option = f'{key}={value} '
        java_args = java_args + java_option

    args = [
        'sonar-scanner',
        f'-Dsonar.organization={self.__organization}',
        f'-Dsonar.projectKey={self.__project_key}',
        '-Dsonar.sources=.',
        '-Dsonar.exclusions=**/tests/*,**/test/*',
        '-Dsonar.c.file.suffixes=-',
        '-Dsonar.cpp.file.suffixes=-',
        '-Dsonar.objc.file.suffixes=-',
        '-Dsonar.java.file.suffixes=-',
        f'-Dsonar.host.url={self.__sonarcloud_host}',
        f'-DjavaOpts={java_args}'
    ]

    if self.__debug:
        args.append('-X')

    if not self.__scm_blame:
        args.append('-Dsonar.scm.disabled=true')

the args String is then executed by the OS.

Best.

Hi,

I managed to solve it be generating the sonar-project.properties file using printf as the command prior to the docker command :slight_smile: i.e:
printf “sonar.projectKey=%s\nsonar.organization=myOrganization” “myProjectKey” > sonar-project.properties

Thanks for the help!

BR
//Magnus