Azure Devops Run SonarCloud Analysis for .Net Project

309 Views Asked by At

I have a pipeline for .net core project and i am trying to use SonarCloud for code analysis. My pipeline below is being run by a hosted agent. It complains about Java_Home. How can i get around this?

error

ERROR: JAVA_HOME not found in your environment, and no Java executable present in the PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation, or add "java.exe" to the PATH

yaml

pool:
  name: company-pool
variables:
  BuildConfiguration: 'Release'

trigger:
  branches:
    include:
    - main

steps:

- task: UseDotNet@2
  displayName: 'Use .NET 6.x'
  inputs:
    version: 6.x
    includePreviewVersions: true

- task: SonarCloudPrepare@1
  displayName: 'Prepare analysis on SonarCloud'
  inputs:
    SonarCloud: SonarCloud
    organization: myorg
    projectKey: 'mysummary'
    projectName: 'myproject'

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: |
     *src/Comapny.Web/Comapny.Web.csproj

- task: DotNetCoreCLI@2
  displayName: 'Build projects'
  inputs:
    projects: |
     *src/Comapny.Web/Comapny.Web.csproj

    arguments: '--configuration $(BuildConfiguration)'

- task: SonarCloudAnalyze@1
  displayName: 'Run Code Analysis'
  inputs:
    jdkversion: 'JAVA_HOME'
    
- task: SonarCloudPublish@1
  displayName: 'Publish Quality Gate Result'
1

There are 1 best solutions below

0
On

Based on the error message, the cause of the issue is that you haven't configured the JAVA environment in your Agent machine.

To solve this issue, you can JavaToolInstaller@0 to configure the Java Environment. But since you don’t have the Preinstalled java version on your local machine, you need to add steps to download it.

Here is an example:

Linux Machine:

steps:
- bash: 'wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz '
  displayName: 'Bash Script'

- task: JavaToolInstaller@0
  displayName: 'Use Java 21'
  inputs:
    versionSpec: 21
    jdkArchitectureOption: x64
    jdkSourceOption: LocalDirectory
    jdkFile: '$(build.sourcesdirectory)/jdk-21_linux-x64_bin.tar.gz'
    jdkDestinationDirectory: '$(agent.toolsDirectory)/jdk21'
- task: UseDotNet@2
  displayName: 'Use .NET 6.x'
  inputs:
    version: 6.x
    includePreviewVersions: true

- task: SonarCloudPrepare@1
  displayName: 'Prepare analysis on SonarCloud'
  inputs:
    SonarCloud: SonarCloud
    organization: myorg
    projectKey: 'mysummary'
    projectName: 'myproject'

Windows Machine:

steps:
- powershell: |
   $source = "https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.zip"
   $destination = "$(build.sourcesdirectory)\jdk-21_windows-x64_bin.zip"
   $client = new-object System.Net.WebClient 
   $cookie = "oraclelicense=accept-securebackup-cookie"
   $client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie) 
   $client.downloadFile($source, $destination)
  displayName: 'PowerShell Script'

- task: JavaToolInstaller@0
  displayName: 'Use Java 21'
  inputs:
    versionSpec: 21
    jdkArchitectureOption: x64
    jdkSourceOption: LocalDirectory
    jdkFile: '$(build.sourcesdirectory)\jdk-21_windows-x64_bin.zip'
    jdkDestinationDirectory: '$(agent.toolsDirectory)/jdk21'

- task: UseDotNet@2
  displayName: 'Use .NET 6.x'
  inputs:
    version: 6.x
    includePreviewVersions: true

- task: SonarCloudPrepare@1
  displayName: 'Prepare analysis on SonarCloud'
  inputs:
    SonarCloud: SonarCloud
    organization: myorg
    projectKey: 'mysummary'
    projectName: 'myproject'

Then the JAVA_HOME will be set on the self-hosted agent.