diff --git a/Dockerfile b/Dockerfile index 3f4239d..259ad5d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,9 +17,6 @@ WORKDIR /app # 直接复制已构建的二进制文件(在Jenkins中已经通过go build构建完成) COPY golang-demo . -# 复制配置文件(如果存在) -COPY .env* ./ - # 创建日志目录并设置权限 RUN mkdir -p /app/logs && chown -R goapp:goapp /app && chmod +x /app/golang-demo diff --git a/Jenkinsfile b/Jenkinsfile index 9e23d22..a1bca1f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,6 +9,8 @@ pipeline { tools { go 'go' // 使用Jenkins手动配置的Go工具 + // 添加SonarQube Scanner工具 + sonarQubeScanner 'sonarQube' } environment { @@ -150,26 +152,30 @@ pipeline { echo '🔍 运行SonarQube代码扫描...' script { try { - // 使用sonar-scanner for Go项目 - sh """ - # 创建sonar-project.properties文件 - cat > sonar-project.properties << EOF + // 创建SonarQube配置文件 + writeFile file: 'sonar-project.properties', text: """ sonar.projectKey=${SONAR_PROJECT_KEY} -sonar.projectName=golang-demo -sonar.projectVersion=1.0.0 +sonar.projectName=Golang Demo +sonar.projectVersion=${BUILD_NUMBER} sonar.sources=. -sonar.exclusions=vendor/**,**/*_test.go,**/testdata/** +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.host.url=${SONAR_HOST_URL} -sonar.login=${SONAR_TOKEN} -EOF - - # 运行sonar-scanner - sonar-scanner || echo "SonarQube扫描工具未安装,跳过扫描" +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} """ echo "✅ SonarQube代码扫描完成" + } catch (Exception e) { echo "⚠️ SonarQube扫描失败,继续构建流程: ${e.getMessage()}" }