SonarQube: No analysis found in this build after applying include pattern - azure-devops

I am trying to integrate SonarQube in a CI-pipeline in Azure DevOps. I used the exact configuration that I used in other pipelines where everything runs smoothly. However, this pipeline can't find the analysis in the SonarQube Publish step as shown in the logs below.
##[debug] /home/vsts/work/_temp/sonar (directory)
##[debug] /home/vsts/work/_temp/sonar/Company.Service-CI[sonarqube-test][121226][2022-01-19] (directory)
##[debug] /home/vsts/work/_temp/sonar/Company.Service-CI[sonarqube-test][121226][2022-01-19]/9731b11b-6af8-d211-752d-e8fe0770156c (directory)
##[debug] /home/vsts/work/_temp/sonar/Company.Service-CI[sonarqube-test][121226][2022-01-19]/9731b11b-6af8-d211-752d-e8fe0770156c/report-task.txt (file)
##[debug]4 results
##[debug]found 4 paths
##[debug]applying include pattern
##[debug]adjustedPattern: '/home/vsts/work/_temp/sonar/Company.Service-CI[sonarqube-test][121226][2022-01-19]/**/report-task.txt'
##[debug]0 matches
##[debug]0 final results
##[debug][SQ] Searching for sonar/Company.Service-CI[sonarqube-test][121226][2022-01-19]/**/report-task.txt - found 0 file(s)
##[warning]No analyses found in this build! Please check your build configuration.
I find this problem hard to solve because it seems like the analysis report is there but after applying the include pattern there are zero matches. The pipeline contains the following steps:
stages:
- stage:
jobs:
- job: Build_Restore_and_Run_Unit_Tests
pool:
vmImage: ubuntu-latest
steps:
- task: SonarQubePrepare#5
displayName: SonarQube
inputs:
SonarQube: SonarQube
scannerMode: MSBuild
projectKey: $(sonarQubeProjectKey)
projectName: $(sonarQubeProjectName)
extraProperties: |
sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/*/coverage.opencover.xml
- task: DotNetCoreCLI#2
displayName: Restore
inputs:
command: 'restore'
projects: 'src/**/*.csproj'
feedsToUse: 'select'
vstsFeed: '989127ec-b6ef-4c8c-ae54-90dec743281d'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: |
**/*.Test.csproj
arguments: '--configuration $(buildConfiguration) --no-restore'
- task: DotNetCoreCLI#2
displayName: Test
inputs:
command: test
projects: '**/*.Test.csproj'
nobuild: true
arguments: '--no-restore --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover'
publishTestResults: true
- task: PublishCodeCoverageResults#1
displayName: Publish code coverage
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(Agent.TempDirectory)/*/coverage.cobertura.xml
- task: SonarQubeAnalyze#5
displayName: Run code analysis
- task: SonarQubePublish#5
displayName: Publish quality gate result
inputs:
pollingTimeoutSec: '300'
My attempts to fix this issue:
Currently I am using ubuntu-latest. I have tried a Windows image.
Applied time-outs.
Upgraded the SonarQube steps from major version 4 to 5.
Added an unique GUID to each projects csproj.

As Mickaël Caro stated on my similar post in the SonarSource community forum:
Unfortunately some characters are not recognized nor parsed correctly when we try to find the report path (We do have this issue filed here : [VSTS-240] Escape regex characters when finding report path file. - SonarSource).
I changed the [ ] in the name of the build to ( ), and the analysis runs smoothly.

Related

Packages failed to publish due to TypeError: Cannot read property 'toLowerCase' of null

In my azure devops pipeline. A conda package is build firstly and published via UniversalPackages#0 task.
The build and upload code snippet is shown below
- stage: build_upload_package
displayName: Build and upload package
pool:
name: Private Agent Eve
jobs:
- job: build_upload_package
displayName: Build and Upload Package
condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/release'))
container:
image: <azureacr_repo.io>/conda:latest //modified to remove the information
endpoint: NameOfTheServiceConnection //modified to remove the information
steps:
- bash: |
echo "##vso[task.prependpath]$CONDA/bin"
conda build . --output-folder $(Build.ArtifactStagingDirectory)
displayName: Build Package
- bash: echo "##vso[task.setvariable variable=packageName]$(basename $(gitRepoName))"
displayName: Set Package Name
- task: UniversalPackages#0 //Error appears here
displayName: Upload to Azure Universal Packages
inputs:
command: publish
publishDirectory: '$(Build.ArtifactStagingDirectory)'
vstsFeedPublish: 'project_1/feed_1' //modified to remove the information
vstsFeedPackagePublish: '$(packageName)'
versionOption: patch
packagePublishDescription: 'upload a Conda Package'
The error for Universal UniversalPackages task is show below
2020-11-24T17:54:48.9628862Z ##[section]Starting: Upload to Azure Universal Packages
2020-11-24T17:54:48.9645737Z ==============================================================================
2020-11-24T17:54:48.9646470Z Task : Universal packages
2020-11-24T17:54:48.9647068Z Description : Download or publish Universal Packages
2020-11-24T17:54:48.9647636Z Version : 0.175.0
2020-11-24T17:54:48.9648154Z Author : Microsoft Corporation
2020-11-24T17:54:48.9648809Z Help : https://learn.microsoft.com/azure/devops/pipelines/tasks
2020-11-24T17:54:48.9649526Z ==============================================================================
2020-11-24T17:54:49.4896147Z SYSTEMVSSCONNECTION exists true
2020-11-24T17:54:50.1267511Z Downloading: https://08wvsblobprodsu6weus73.vsblob.vsassets.io/artifacttool/artifacttool-linux-x64-Release_0.2.172.zip?sv=2019-02-02&sr=b&sig=FvnCFjIg7ZBmIapFcCcWDC1%2F7pajvzvgo0fR2Y3YYeU%3D&spr=https&se=2020-11-24T18%3A54%3A51Z&sp=r&P1=1606243791&P2=11&P3=2&P4=1nPT99OOlNXc9xNzoCS6LpVATo%2bW4fIskTJ8u6olWMk%3d
2020-11-24T17:54:56.4979070Z Caching tool: ArtifactTool 0.2.172 x64
2020-11-24T17:54:56.7183832Z SYSTEMVSSCONNECTION exists true
2020-11-24T17:54:58.7313555Z ##[error]TypeError: Cannot read property 'toLowerCase' of null
2020-11-24T17:54:58.7323845Z ##[error]Packages failed to publish
2020-11-24T17:54:58.7641267Z ##[section]Finishing: Upload to Azure Universal Packages
Question:
How can I solve this issue?
I think I found the error, gitRepoName is a template parameter, it should be accessed as ${{ gitRepoName }} not the $(gitRepoName)
I did some tests on this task:UniversalPackages#0. I noticed the following reminder in the task:
Universal package names must be one or more lowercase alphanumeric segments separated by a dash, dot or underscore. The package name must be under 256 characters.
When your packageName contains capital letters or other special characters, this task will fail.
You could try the following scripts:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
$name = $(basename $(gitRepoName)).ToLower()
Write-Host "##vso[task.setvariable variable=packageName]$name"
- task: UniversalPackages#0
inputs:
command: 'publish'
publishDirectory: '$(Build.ArtifactStagingDirectory)'
vstsFeedPublish: 'project_1/feed_1'
vstsFeedPackagePublish: '$(packageName)'
versionOption: 'patch'
The Powershell task could Lowercase the RepoName.

File from previous step cannot be found in Azure DevOps-Pipeline

In a pipeline I have two different steps. The first one generates some files, the second should take these files as an input.
the Yaml for that pipeline is the following:
name: myscript
stages:
- stage: Tes/t
displayName: owasp-test
jobs:
- job: owasp_test
displayName: run beasic checks for site
pool:
name: default
demands: Agent.OS -equals Windows_NT
steps:
- task: DotNetCoreCLI#2
inputs:
command: 'build'
projects: '**/*.sln'
- task: dependency-check-build-task#5
inputs:
projectName: 'DependencyCheck'
scanPath: '**/*.dll'
format: 'JUNIT'
- task: PublishTestResults#2
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: '**/*-junit.xml'
the dependency-check-build-task returns an XML-File:
File upload succeed.
Upload 'P:\Azure-Pipelines-Agent\_work\2\TestResults\dependency-check\dependency-check-junit.xml' to file container: '#/11589616/dependency-check'
Associated artifact 53031 with build 21497
The following step (PublishTestResults) SHOULD take that file but returns
##[warning]No test result files matching **/*-junit.xml were found.
instead. I can see that file in the artifact after the pipeline is run.
This is because your report is written to Common.TestResultsDirectory which is c:\agent_work\1\TestResults (for Microsoft Hosted agents), and publish test task looks in System.DefaultWorkingDirectory which is c:\agent_work\1\s.
Please try:
- task: PublishTestResults#2
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: '**/*-junit.xml'
searchFolder: '$(Common.TestResultsDirectory)'
I had the same trouble:
I fixed changing the Agent Specification

how to convert classic build job to yaml build in AzureDevops

We have a working classic build job in azure Devops with an self hosted agent pool. But when we tried to convert this build job to yaml method, while executing no agents are getting assigned and its hanging. Could you please correct me here if i am doing something task.
Error
"All eligible agents are disabled or offline"
below is the converted yaml file from classic build - agent job
pool:
name: MYpool
demands: maven
#Your build pipeline references an undefined variable named ‘Parameters.mavenPOMFile’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972
steps:
- task: Maven#3
displayName: 'Maven pom.xml'
inputs:
mavenPomFile: '$(Parameters.mavenPOMFile)'
- task: CopyFiles#2
displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
inputs:
SourceFolder: '$(system.defaultworkingdirectory)'
Contents: '**/*.war'
TargetFolder: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: Root'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
ArtifactName: Root
condition: succeededOrFailed()
- task: CopyFiles#2
displayName: 'Copy wars to build directory'
inputs:
SourceFolder: '$(build.artifactstagingdirectory)/target'
TargetFolder: '/home/myadmin/builds/$(build.buildnumber)'
- task: CopyFiles#2
displayName: 'copying docker file to Build Directory'
inputs:
SourceFolder: Admin
TargetFolder: '/home/myadmin/builds/$(build.buildnumber)'
- bash: |
# Write your commands here
mv /home/myadmin/builds/$(build.buildnumber)/mypack0.0.1.war /home/myadmin/builds/$(build.buildnumber)/ROOT.war
displayName: 'Name war file Root.war'
- task: Docker#2
displayName: 'Build the docker image'
inputs:
repository: 'mycontainerregistry.azurecr.io/myservice'
command: build
Dockerfile: '/home/myadmin/builds/$(build.buildnumber)/Dockerfile'
tags: '$(Build.BuildNumber)-DEV'
- bash: |
# Write your commands here
docker login mycontainerregistry.azurecr.io
docker push mycontainerregistry.azurecr.io/myservice:$(Build.BuildNumber)-DEV
displayName: 'Push Docker Image'
- task: CopyFiles#2
displayName: 'Copy Deployment file'
inputs:
SourceFolder: /home/myadmin/kubernetes
TargetFolder: '/home/myadmin/builds/$(build.buildnumber)'
- task: qetza.replacetokens.replacetokens-task.replacetokens#3
displayName: 'Replace image in deployment file'
inputs:
rootDirectory: '/home/myadmin/builds/$(build.buildnumber)'
targetFiles: '**/*.yml'
In my previous answer, I said when I wait for nearly 20-30 mins, the interface of agent will prompt below message.
In fact, this is a process which upgrade the agent to latest version automatically.
Yes, when you using YAML with private agent, the agent version must be the latest one. No matter you add the demands or not.
For our system, the agent version is a implicit demand that your agent must satisfied with the latest one when you applying it in YAML.
If it is not satisfied, it will be blocked and the agent upgrade process will be forced to be performed automatically by system after some times.
So, to execute the private agent in YAML successfully, please upgrade the agent to latest one manually.
Since what my colleague and I talked are all private to microsoft in this ticket, sorry you could not get visible on this summary. So, here I take the screenshots about it, and you can refer to it: https://imgur.com/a/4OnzHp3
We are still working on why the system prompting so confusing message like: All eligible agents are disabled or offline. And, am trying to do some contribution to let this message more clear, for example: no agents meet demands: agent version xxx.

Azure DevOps test -xml not found after running the Cypress tests

Added a Publish test results task in Azure DevOpsCI/CD pipeline, test were successfull, but after running the test it complaints about ##[warning]No test result files matching **/test-*.xml were found. Could someone please advise on how can we resolve similar problem ?
Publish Test Results task : configuration
Test result format= JUnit
Test results files= **/test-*.xml
Search folder = $(System.DefaultWorkingDirectory)
Test results title = Cypress Test Results
note: I have try adding the search folder path as follows: C:\agent_work\r5\a\drop\ui-tests\cypress
package.json to run the tests
"scripts": {
"test": "cypress run --record --key <key value here>"
}
My directory path in server:
C:\agent_work\r5\a\drop\ui-tests\cypress
My friend, I was facing the same issue on Azure DevOps.
In my case, the folder where the xml files were generated was reports on the root of the repo, that depends on how you got configured Junit on your cypress.json file
So In my case, the solution was changing this on azure-pipelines.yml
testResultsFiles: "results/*.xml"
searchFolder: $(System.DefaultWorkingDirectory)
So that's the entire setup of the testing pipeline
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '12.x'
displayName: 'Install Node.js'
- script: "npm i"
displayName: "Install project dependencies"
- script: "npm run cy:verify"
displayName: "Cypress Verify"
- script: "source cypress.env" # comment this script to run tests against production
displayName: "Using env variables to change url to test against development branch"
- script: "npm run cy:run-report"
displayName: "Run Cypress Tests"
- task: PublishBuildArtifacts#1
displayName: "Publish Artifact: cypress-azure-devops screenshots"
inputs:
PathtoPublish: cypress/screenshots
ArtifactName: "CypressAzureDevopsTestRunScreenshots"
condition: failed()
- task: PublishTestResults#2
displayName: "Publish Test Results"
condition: succeededOrFailed()
inputs:
testResultsFormat: "JUnit"
testResultsFiles: "results/*.xml"
searchFolder: $(System.DefaultWorkingDirectory)
mergeTestResults: true
testRunTitle: 'Test Results'
continueOnError: true
Saludos desde Argentina 🇦🇷

How to fail the build pipeline if "Tests Failed" in azure pipelines?

I want to fail the build pipeline if a single test failed with azure pipelines.
Azure can successfully detect that my tests entered a failed state however it gives a success state to the entire build pipeline:
The question is how to make azure give a failed build state if the tests stage failed?
Here's my azure-pipelines.yml :
# Build ASP.NET Core project using Azure Pipelines
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core?view=vsts
pool:
vmImage: 'Ubuntu 16.04'
variables:
buildConfiguration: 'Release'
steps:
- script: |
dotnet build --configuration $(buildConfiguration)
dotnet test dotnetcore-tests --configuration $(buildConfiguration) --logger trx
dotnet publish --configuration $(buildConfiguration) --output $BUILD_ARTIFACTSTAGINGDIRECTORY
- task: PublishTestResults#2
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'
- task: PublishBuildArtifacts#1
The original answer didn't work for me, but it looks like there was a lot of discussion on this, and there's now a failTaskOnFailedTests param for the task. That seems to work.
- task: PublishTestResults#2
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'
failTaskOnFailedTests: true
I'm still surprised this wasn't default behavior.
Try to add failOnStandardError: 'true' in the task inputs:
- task: PublishTestResults#2
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'
failOnStandardError: 'true'
Untick the below-highlighted option