Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Is ardán dearbhaithe cáilíochta cód foinse oscailte é SonarQube a thacaíonn le raon leathan teangacha cláir agus a sholáthraíonn tuairisciú ar mhéadracht cosúil le dúbailt cód, comhlíonadh caighdeáin códaithe, clúdach tástála, castacht cód, fabhtanna féideartha, agus níos mó. Déanann SonarQube léirshamhlú go caothúil ar thorthaí anailíse agus ligeann sé duit dinimic na forbartha tionscadail a rianú le himeacht ama.

Cuspóir: Taispeáin d'fhorbróirí stádas rialaithe cáilíochta cód foinse i SonarQube.

Tá dhá réiteach ann:

  • Rith script chun stádas rialaithe cáilíochta an chóid foinse a sheiceáil i SonarQube. Mura n-éiríonn le rialú cáilíochta an chóid foinse i SonarQube, ansin teip ar an tionól.
  • Taispeáin stádas rialaithe cáilíochta an chóid foinse ar phríomhleathanach an tionscadail.

Suiteáil SonarQube

Chun sonarqube a shuiteáil ó phacáistí rpm, úsáidfimid an stór https://harbottle.gitlab.io/harbottle-main.

Déanaimis an pacáiste a shuiteáil leis an stór do CentOS 7.

yum install -y https://harbottle.gitlab.io/harbottle-main/7/x86_64/harbottle-main-release.rpm

Suiteáilimid sonarqube féin.

yum install -y sonarqube

Le linn na suiteála, déanfar an chuid is mó de na forlíontáin a shuiteáil, ach ní mór duit findbugs agus pmd a shuiteáil

yum install -y sonarqube-findbugs sonarqube-pmd

Seoladh an tseirbhís agus cuir leis an am tosaithe

systemctl start sonarqube
systemctl enable sonarqube

Má thógann sé tamall fada é a luchtú, ansin cuir gineadóir uimhreacha randamacha /dev/./urandom go dtí deireadh na roghanna sonar.web.javaOpts

sonar.web.javaOpts=другие параметры -Djava.security.egd=file:/dev/urandom

Script a rith chun stádas rialaithe cáilíochta cód foinse a sheiceáil i SonarQube.

Ar an drochuair, níor nuashonraíodh an breiseán sonar-break-maven-plugin le fada an lá. Mar sin déanaimis ár script féin a scríobh.

Le haghaidh tástála úsáidfimid an stór https://github.com/uweplonus/spotbugs-examples.

Iompórtáil go Gitlab. Cuir an comhad .gitlab-ci.yml leis:

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=~/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  SONAR_HOST_URL: "http://172.26.9.226:9000"
  LOGIN: "admin" # логин sonarqube
  PASSWORD: "admin" # пароль sonarqube

cache:
  paths:
    - .m2/repository

build:
  image: maven:3.3.9-jdk-8
  stage: build
  script:
    - apt install -y jq || true
    - mvn $MAVEN_CLI_OPTS -Dmaven.test.failure.ignore=true org.jacoco:jacoco-maven-plugin:0.8.5:prepare-agent clean verify org.jacoco:jacoco-maven-plugin:0.8.5:report
    - mvn $MAVEN_CLI_OPTS -Dmaven.test.skip=true verify sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$LOGIN -Dsonar.password=$PASSWORD -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
    - export URL=$(cat target/sonar/report-task.txt | grep ceTaskUrl | cut -c11- ) #URL where report gets stored
    - echo $URL
    - |
      while : ;do
          curl -k -u "$LOGIN":"$PASSWORD" "$URL" -o analysis.txt
          export status=$(cat analysis.txt | jq -r '.task.status') #Status as SUCCESS, CANCELED, IN_PROGRESS or FAILED
          echo $status
          if [ ${status} == "SUCCESS" ];then
            echo "SONAR ANALYSIS SUCCESS";
            break
          fi
          sleep 5
      done
    - curl -k -u "$LOGIN":"$PASSWORD" "$URL" -o analysis.txt
    - export status=$(cat analysis.txt | jq -r '.task.status') #Status as SUCCESS, CANCELED or FAILED
    - export analysisId=$(cat analysis.txt | jq -r '.task.analysisId') #Get the analysis Id
    - |
      if [ "$status" == "SUCCESS" ]; then
        echo -e "SONAR ANALYSIS SUCCESSFUL...ANALYSING RESULTS";
        curl -k -u "$LOGIN":"$PASSWORD" "$SONAR_HOST_URL/api/qualitygates/project_status?analysisId=$analysisId" -o result.txt; #Analysis result like critical, major and minor issues
        export result=$(cat result.txt | jq -r '.projectStatus.status');

        if [ "$result" == "ERROR" ];then
          echo -e "91mSONAR RESULTS FAILED";
          echo "$(cat result.txt | jq -r '.projectStatus.conditions')"; #prints the critical, major and minor violations
          exit 1 #breaks the build for violations
        else
          echo -e "SONAR RESULTS SUCCESSFUL";
          echo "$(cat result.txt | jq -r '.projectStatus.conditions')";
          exit 0
        fi
      else
          echo -e "e[91mSONAR ANALYSIS FAILEDe[0m";
          exit 1 #breaks the build for failure in Step2
      fi
  tags:
    - docker

Níl an comhad .gitlab-ci.yml foirfe. Thástáil má chríochnaigh tascanna scanadh sonarqube leis an stádas: "RATH". Go dtí seo ní raibh aon stádas eile. Chomh luath agus a bheidh stádas eile ann, ceartóidh mé .gitlab-ci.yml sa phost seo.

Stádas rialaithe cáilíochta an chóid foinse a thaispeáint ar phríomhleathanach an tionscadail

Suiteáil an breiseán do SonarQube

yum install -y sonarqube-qualinsight-badges

Téimid go dtí SonarQube ag http://172.26.9.115:9000/
Cruthaigh úsáideoir rialta, mar shampla "suaitheantais".
Logáil isteach go SonarQube faoin úsáideoir seo.

Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Téigh go dtí “Mo chuntas”, cruthaigh comhartha nua, mar shampla leis an ainm “read_all_repository” agus cliceáil “Gin”.

Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Feicimid go bhfuil comhartha le feiceáil. Ní bheidh sé le feiceáil ach 1 uair.

Logáil isteach mar riarthóir.

Téigh go dtí Cumraíocht -> Suaitheantais SVG

Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Cóipeáil an comhartha seo isteach sa réimse "Comhartha suaitheantas gníomhaíochta" agus cliceáil ar an gcnaipe Sábháil.

Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Téigh go Riarachán -> Slándáil -> Teimpléid Cead -> Teimpléad réamhshocraithe (agus teimpléid eile a bheidh agat).

Ní mór don úsáideoir suaitheantais an ticbhosca "Brabhsáil" a sheiceáil.

Tástáil.

Mar shampla, déanaimis an tionscadal a ghlacadh https://github.com/jitpack/maven-simple.

Déanaimis an tionscadal seo a allmhairiú.

Cuir an comhad .gitlab-ci.yml le fréamh an tionscadail leis an ábhar seo a leanas.

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=~/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  SONAR_HOST_URL: "http://172.26.9.115:9000"
  LOGIN: "admin" # логин sonarqube
  PASSWORD: "admin" # пароль sonarqube

cache:
  paths:
    - .m2/repository

build:
  image: maven:3.3.9-jdk-8
  stage: build
  script:
    - mvn $MAVEN_CLI_OPTS -Dmaven.test.failure.ignore=true org.jacoco:jacoco-maven-plugin:0.8.5:prepare-agent clean verify org.jacoco:jacoco-maven-plugin:0.8.5:report
    - mvn $MAVEN_CLI_OPTS -Dmaven.test.skip=true verify sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$LOGIN -Dsonar.password=$PASSWORD -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
  tags:
    - docker

In SonarQube beidh cuma mar seo ar an tionscadal:

Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Cuir málaí le README.md agus beidh cuma mar seo orthu:

Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Breathnaíonn an cód taispeána suaitheantais mar seo:

Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Teaghrán taispeána na suaitheantais a pharsáil:

[![Quality Gate](http://172.26.9.115:9000/api/badges/gate?key=com.github.jitpack:maven-simple)](http://172.26.9.115:9000/dashboard?id=com.github.jitpack%3Amaven-simple)
[![Название](http://172.26.9.115:9000/api/badges/gate?key=Project Key)](http://172.26.9.115:9000/dashboard?id=id-проекта)
[![Coverage](http://172.26.9.115:9000/api/badges/measure?key=com.github.jitpack:maven-simple&metric=coverage)](http://172.26.9.115:9000/dashboard?id=com.github.jitpack%3Amaven-simple)
[![Название Метрики](http://172.26.9.115:9000/api/badges/measure?key=Project Key&metric=МЕТРИКА)](http://172.26.9.115:9000/dashboard?id=id-проекта)

Cá háit a bhfaighfear/seiceáil Eochair an Tionscadail agus aitheantas an tionscadail.

Tá Eochair an Tionscadail ag bun ar dheis. Tá aitheantas an tionscadail san URL.

Stádas rialaithe cáilíochta cód foinse i SonarQube a thaispeáint d'fhorbróirí

Is féidir le roghanna chun méadracht a fháil féach anseo.

Gach iarratas tarraingt le haghaidh feabhsúcháin, réiteach fabhtanna a chur faoi bhráid an stór seo.

Comhrá teileagram faoi SonarQube https://t.me/sonarqube_ru
Comhrá teileagram faoi DevSecOps - slán DevOps https://t.me/sec_devops

Foinse: will.com

Add a comment