You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins声明式多分支流水线配置CMake构建测试失败求助

问题:Jenkins声明式多分支流水线CMake构建失败

配置Jenkins声明式多分支流水线,用CMake构建并运行单元测试时失败,Jenkins日志输出如下:

Obtained Jenkinsfile from ee03d9be78e0bf8585e6e122fcc03f6e13365cff
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/mattwalsh/.jenkins/workspace/matt-wash-projects_main
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
using credential mattwalsh-github-creds
Cloning the remote Git repository
Using shallow clone with depth 2
Cloning repository https://github.com/mysticrecords/out-of-tree-builds.git
 > git init /Users/mattwalsh/.jenkins/workspace/matt-wash-projects_main # timeout=10
Fetching upstream changes from https://github.com/mysticrecords/out-of-tree-builds.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.1 (Apple Git-130)'
using GIT_ASKPASS to set credentials mattwalsh-github-creds
 > git fetch --tags --force --progress --depth=2 -- https://github.com/mysticrecords/out-of-tree-builds.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.com/mysticrecords/out-of-tree-builds.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision ee03d9be78e0bf8585e6e122fcc03f6e13365cff (main)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f ee03d9be78e0bf8585e6e122fcc03f6e13365cff # timeout=1
Commit message: "jenkinsfile"
 > git rev-list --no-walk ee03d9be78e0bf8585e6e122fcc03f6e13365cff # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Configure)
[Pipeline] dir
Running in /Users/mattwalsh/.jenkins/workspace/matt-wash-projects_main/build
[Pipeline] {
[Pipeline] cmake
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
Stage "Build" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
Stage "Test" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] archiveArtifacts
Archiving artifacts
[Pipeline] xunit
INFO: Processing CTest-Version 3.x (default)
INFO: [CTest-Version 3.x (default)] - No test report file(s) were found with the pattern 'build/*.xml' relative to '/Users/mattwalsh/.jenkins/workspace/matt-wash-projects_main' for the testing framework 'CTest-Version 3.x (default)'.
Did you enter a pattern relative to (and within) the workspace directory?
Did you generate the result report(s) for 'CTest-Version 3.x (default)'?"
INFO: Check 'Skipped Tests' threshold.
INFO: Check 'Failed Tests' threshold.
[Pipeline] deleteDir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: There is no Cmake installation selected. Please review the build step configuration and make sure the installation is configured on the Global Tool Configuration page.

GitHub has been notified of this commit’s build result

Finished: FAILURE

使用的Jenkins声明式流水线代码如下(本地测试脚本可正常运行):

pipeline {
  agent any
  stages {
    stage('Configure') {
      steps {
        dir('build') {
          cmake(
            installation: 'InSearchPath'
          )
          sh 'cmake .'
        }
      }
    }
    stage('Build') {
      steps {
        dir('build') {
          sh 'cmake --build .'
        }
      }
    }  
    stage('Test') {
      steps {
        dir('build') {
          sh 'ctest -C checkin --output-junit unittest.xml'
        }
      }
    }
  }
  post {
    always {
      // Archive the CTest xml output
      archiveArtifacts (
        artifacts: 'build/*.xml',
        fingerprint: true
      )

      // Process the CTest xml output with the xUnit plugin
      xunit (
        testTimeMargin: '3000',
        thresholdMode: 1,
        thresholds: [
          skipped(failureThreshold: '0'),
          failed(failureThreshold: '0')
        ],
      tools: [CTest(
          pattern: 'build/*.xml',
          deleteOutputFiles: true,
          failIfNotNew: false,
          skipNoTestFiles: true,
          stopProcessingIfError: true
        )]
      )

      // Clear the source and build dirs before next run
      deleteDir()
    }
  }
}

已在Jenkins中配置CMake插件,使用内置节点构建,求解决方法。


解决步骤

1. 配置Jenkins全局工具中的CMake

日志明确提示未选择有效的CMake安装,按以下步骤操作:

  • 进入Jenkins主页,点击左侧系统管理 → 全局工具配置
  • 找到CMake配置区域:
    • 点击添加CMake,给安装设置一个自定义名称(比如CMake,不要用InSearchPath,该名称并非默认有效标识)
    • 可选自动安装(节点需联网),或手动指定CMake的本地路径(Mac环境下通常为/usr/local/bin/cmake)
    • 保存配置

2. 修正Jenkinsfile中的CMake执行逻辑

你的流水线同时使用了Jenkins的cmake步骤和sh 'cmake .',属于重复操作,且路径参数有误:

方式一:使用Jenkins CMake插件步骤

stage('Configure') {
  steps {
    dir('build') {
      cmake(
        installation: 'CMake', // 填写全局配置的CMake名称
        sourceDir: '..', // 源码在workspace根目录,build为子目录,需指定上级路径
        buildDir: '.',
        args: '-DCMAKE_BUILD_TYPE=Release' // 可添加额外CMake参数
      )
      // 无需再单独执行sh 'cmake .'
    }
  }
}

方式二:直接用Shell命令调用CMake

如果不想依赖Jenkins CMake插件,确保节点PATH中包含CMake路径,修改为:

stage('Configure') {
  steps {
    dir('build') {
      sh 'cmake ..' // 指向workspace根目录的CMakeLists.txt
    }
  }
}

3. 调整CTest命令参数

本地测试正常但Jenkins中可能存在构建类型不匹配问题,修改测试步骤:

stage('Test') {
  steps {
    dir('build') {
      sh 'ctest -C Release --output-junit unittest.xml' // 替换为实际使用的构建类型,如Debug/Release
    }
  }
}

4. 验证节点CMake环境

先执行临时流水线确认内置节点的CMake可用性:

pipeline {
  agent any
  stages {
    stage('Check CMake') {
      steps {
        sh 'cmake --version'
      }
    }
  }
}

若此步骤失败,说明本地内置节点未安装CMake,需先在本地机器完成CMake安装。


内容的提问来源于stack exchange,提问作者Matt Walsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 11:22:26