目录
一、实验
1.GitLabCI 代码扫描
二、问题
1.GitLab 执行sonar-scanner命令报错
一、实验
1.GitLabCI 代码扫描
(1)打开maven项目
(2)maven项目流水线调用公共库
(3)项目组添加token认证
(4)修改公共库流水线文件
(5)修改CI.yaml
.pipelineInit:tags:- buildstage: .prevariables:GIT_CHECKOUT: "true"script:- ls -l.cibuild:tags:- buildstage: buildscript:- echo "${BUILD_SHELL}"- ${BUILD_SHELL}artifacts:paths:- ${ARTIFACT_PATH}.citest:tags:- buildstage: testscript:- echo "${TEST_SHELL}"- ${TEST_SHELL}# artifacts:# reports:# - junit: ${TEST_REPORTS}.sonarscan:tags: - buildstage: sonarscanscript: |-sonar-scanner -Dsonar.host.url=http://192.168.204.8:9000 \-Dsonar.projectKey=${CI_PROJECT_NAME}\-Dsonar.projectName=${CI_PROJECT_NAME} \-Dsonar.projectVersion=${CI_COMMIT_SHA} \-Dsonar.login=${SONAR_AUTH_TOKEN} \-Dsonar.ws.timeout=30 \-Dsonar.projectDescription=${CI_PROJECT_TITLE} \-Dsonar.links.homepage=${CI_PROJECT_URL} \-Dsonar.links.ci=${CI_PIPELINE_URL} \-Dsonar.sources=src \-Dsonar.sourceEncoding=UTF-8 \-Dsonar.java.binaries=target/classes \-Dsonar.java.test.binaries=target/test-classes \-Dsonar.java.surefire.report=target/surefire-reports \-Dsonar.core.codeCoveragePlugin=jacoco \-Dsonar.jacoco.reportPaths=target/jacoco.exec
(6) 修改gitlab-ci.yaml,添加代码扫描阶段sonarscan
include:- project: 'devops03/devops03-gitlabci-lib'ref: masterfile: "/jobs/CI.yaml"workflow:rules:#新建分支永远不执行- if: $CI_PIPELINE_SOURCE == "web" #允许手动触发when: always- if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"when: never# 其他情况永远执行- when: always#取消每个阶段自动下载代码,即全局关闭作业代码下载
variables:GIT_CHECKOUT: "false"BUILD_SHELL: "mvn clean package -DskipTests -s settings.xml" ## 构建命令TEST_SHELL: "mvn test -s settings.xml" ## 测试命令ARTIFACT_PATH: "target/*.jar" ## 制品lujingTEST_REPORTS: "target/surefire-reports/TEST-*.xml" ## 测试报告stages:- build- test- sonarscanpipelineInit:extends: - .pipelineInitcibuild:extends:- .cibuildcitest:extends:- .citestsonarscan:extends:- .sonarscan
(6)maven项目运行流水线
(7)成功
(8)查看日志
(9)sonarqube查看项目
二、问题
1.GitLab 执行sonar-scanner命令报错
(1)报错
报错1:命令未找到
报错2:符号连接的层数过多
(2)原因分析
问题1:环境变量未声明
问题2:bin目录软连接过多
(3)解决方法
第一种方式:删除软连接
第二种方式:重新安装sonar-scanner插件
这里采用第二种方式。
#下载地址
https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.8.0.2856-linux.zip#解压在 opt 目录
cd /opt
unzip sonar-scanner-cli-4.8.0.2856-linux.zip #修改配置文件
cd /opt/sonar-scanner-4.8.0.2856-linux/conf
vim sonar-scanner.propertiessonar.host.url=http://192.168.204.8:9000
sonar.sourceEncoding=UTF-8#在 /etc/profile 的末尾添加环境变量
vim /etc/profileexport SONAR_SCANNER_HOME=/opt/sonar-scanner-4.8.0.2856-linux
export PATH=$SONAR_SCANNER_HOME/bin:$PATH#环境变量立即生效
source /etc/profile#检查一下命令是否可用
sonar-scanner -v