diff --git a/Jenkinsfile b/Jenkinsfile index 2e97042..4b3221a 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,8 +9,6 @@ pipeline { tools { go 'go' // 使用Jenkins手动配置的Go工具 - // 修复:使用正确的SonarQube Scanner工具类型 - 'hudson.plugins.sonar.SonarRunnerInstallation' 'sonarQube' } environment { @@ -152,28 +150,25 @@ pipeline { echo '🔍 运行SonarQube代码扫描...' script { try { - // 创建SonarQube配置文件 - writeFile file: 'sonar-project.properties', text: """ -sonar.projectKey=${SONAR_PROJECT_KEY} -sonar.projectName=Golang Demo -sonar.projectVersion=${BUILD_NUMBER} -sonar.sources=. -sonar.exclusions=**/*_test.go,**/vendor/**,**/*.mod,**/*.sum -sonar.tests=. -sonar.test.inclusions=**/*_test.go -sonar.test.exclusions=**/vendor/** -sonar.go.coverage.reportPaths=coverage.out -sonar.sourceEncoding=UTF-8 -sonar.language=go -""" - - // 使用Jenkins配置的SonarQube Scanner工具 - sh """ - sonar-scanner \ - -Dsonar.projectKey=${SONAR_PROJECT_KEY} \ - -Dsonar.host.url=${SONAR_HOST_URL} \ - -Dsonar.login=${SONAR_TOKEN} - """ + // 使用withSonarQubeEnv包裹,自动配置sonar-scanner环境 + withSonarQubeEnv('sonarQube') { + sh ''' + sonar-scanner \ + -Dsonar.projectKey=${SONAR_PROJECT_KEY} \ + -Dsonar.host.url=${SONAR_HOST_URL} \ + -Dsonar.login=${SONAR_TOKEN} \ + -Dsonar.projectName="Golang Demo" \ + -Dsonar.projectVersion=${BUILD_NUMBER} \ + -Dsonar.sources=. \ + -Dsonar.exclusions=**/*_test.go,**/vendor/**,**/*.mod,**/*.sum \ + -Dsonar.tests=. \ + -Dsonar.test.inclusions=**/*_test.go \ + -Dsonar.test.exclusions=**/vendor/** \ + -Dsonar.go.coverage.reportPaths=coverage.out \ + -Dsonar.sourceEncoding=UTF-8 \ + -Dsonar.language=go + ''' + } echo "✅ SonarQube代码扫描完成" } catch (Exception e) {