linux

如何在Linux上使用Jenkins进行代码质量检查

小樊
39
2025-11-26 12:36:25
栏目: 智能运维

在 Linux 上使用 Jenkins 进行代码质量检查

一 环境准备

二 安装与配置插件

三 配置质量检查方式

四 示例 Jenkinsfile

pipeline {
  agent any
  tools {
    // 若已在“全局工具配置”命名了 SonarQube Scanner,请保持一致
    sonarQube 'SonarQubeScanner'
  }
  stages {
    stage('Checkout') {
      steps { checkout scm }
    }
    stage('Build & Test') {
      steps {
        // 示例:Maven 项目
        sh 'mvn -B clean verify'
      }
    }
    stage('SonarQube Analysis') {
      steps {
        withSonarQubeEnv('SonarQube') {
          sh '''
            sonar-scanner \
              -Dsonar.projectKey=my_project \
              -Dsonar.projectName=my_project \
              -Dsonar.projectVersion=1.0 \
              -Dsonar.sources=src \
              -Dsonar.java.binaries=target/classes \
              -Dsonar.sourceEncoding=UTF-8
          '''
        }
      }
    }
    stage('Quality Gate') {
      steps {
        timeout(time: 1, unit: 'HOURS') {
          waitForQualityGate abortPipeline: true
        }
      }
    }
  }
  post {
    always {
      // 可选:发布 Checkstyle / PMD / SpotBugs 报告
      // publishHTML(target: [reportDir: 'target/site/checkstyle', reportFiles: 'checkstyle.html', reportName: 'Checkstyle Report'])
      // publishHTML(target: [reportDir: 'target/site/pmd', reportFiles: 'pmd.html', reportName: 'PMD Report'])
      // publishHTML(target: [reportDir: 'target/site/spotbugs', reportFiles: 'index.html', reportName: 'SpotBugs Report'])
    }
  }
}

五 运行与排错要点

0
看了该问题的人还看了