I want to use bitbucket pipelines and sonar to analyze my code. In my bibucket pipelines there are 2 analysis steps, the first one runs fine, I can see every results and analysis, but in the second step - which runs only on the main branch – the build in sonar quality gate doesn’t work.
If I redefine the quality gate, where I selected line coverage and code duplication it is calculated successfully. What is the problem with my setup?
This is my butbucket pipelines:
image: python:3.8
clone:
depth: full # SonarCloud scanner needs the full history to assign issues properly
definitions:
services:
docker:
memory: 3000 # if you got an EOF file error, it can be caused by the
caches:
sonar: ~/.sonar/cache # Caching SonarCloud artifacts will speed up your build
steps:
- step: &build-test-sonarcloud
name: Build, test and analyze on SonarCloud
caches:
- docker # See https://confluence.atlassian.com/bitbucket/caching-dependencies-895552876.html
- sonar
script:
# create environment
- python3 -m venv env
- source env/bin/activate
- apt-get update && apt-get install -y python3 default-jre
- pip install pytest pytest-cov -r requirements.txt
- pytest --cov=src --cov-report xml:coverage-reports/coverage-all.xml
- pipe: sonarsource/sonarcloud-scan:1.2.1
variables:
EXTRA_ARGS: "-Dsonar.sources=src"
DEBUG: 'false'
- step: &check-quality-gate-sonarcloud
name: Check the Quality Gate on SonarCloud
script:
- pipe: sonarsource/sonarcloud-quality-gate:0.1.4
pipelines:
default:
- step: *build-test-sonarcloud
branches:
main:
- step: *build-test-sonarcloud
- step: *check-quality-gate-sonarcloud
pull-requests:
'**':
- step: *build-test-sonarcloud
- step: *check-quality-gate-sonarcloud
I assume you’re talking about the step using the sonarcloud-quality-gate. Can you please clarify what “doesn’t work”? Does the pipe execution fail on Bitbucket? The last couple of lines with the failure message would be useful here.
without the quality gate-step, the bitbucket pipelines are running fine, every value calculated fine.
The built-in quality gate fails with the following message (below):
if I removed the built-in quality gate setup and made a test one that contains only the line coverage and the duplicated lines measures, it worked fine. I played with it and I found that only these 2 measures are working for me fine.
Is this Quality gate really supports Python projects?
This usually happens when the New Code Period is not defined for a project, which prevents computing the quality gate. When this is the case, the project front page has this:
If this is your case, you can resolve it by clicking on Set New Code definition, and choosing one of the options. Then the next run of the pipe should not get the above error.
I was not able to reproduce this scenario: when new code period is defined, the pipe works fine with a Python project and using the default quality gate. When the new code period is not defined, I always get the error about quality gate not computed (correctly), regardless of quality gate (tested with a single condition on duplications).
Please let me know if you really have new code period defined, and still getting the failure!
These are my screenshots from my setup, I think I set what you mentioned, it shows my ‘test’ setup, which works, but if I check the other definitions they didn’t work for me. The other values calculated fine I’m not calculating the quality gate in the development branch.
Wow that’s new. The result of the pipe is accurate: the quality gate was not computed. The scenario is unexpected, since the quality gate must always be computed when the new code period is defined, which is implied by your previous screenshot.
I will investigate deeper to understand what’s going on here, and get back to you.
Looking at the metadata of the project, the New Code period is not defined. Although I still don’t understand how it was possible to get Quality Gate = Failed status in a previous analysis, it’s clear that an administrator should define the New Code, as the yellow box in your screenshot suggests. Once that’s done, the Not computed state should not happen again.
Please give that a try, and let us know if there are still issues.