我正在进行一个CMake项目。对于CI,我决定使用Azure Pipelines。然而,在测试阶段,我在MacOS上遇到了一个小问题。问题是MacOS无法找到测试可执行文件,即使它在那里。
早些时候,我的项目没有正确构建。但现在,除了MacOS上的小故障外,整个管道都成功运行了(多亏了Stack Overflow社区(。我已经更新了我的问题,现在它告诉什么是问题以及我是如何解决的,这样它可能会对像我这样的新进入CI世界的人有所帮助
编辑1:
早些时候,我的CMake任务没有触发构建过程。这是因为我提供的是不向CMake提供任何命令行参数。我所做的就是:
- task: CMake@1
displayName: Generate CMake Cache
inputs:
workingDirectory: build
我假设CMake任务会自动驱动构建过程,因为文档中没有明确说明该任务的实际作用。除了打印CMake的用法之外,什么也没做。然后我发现我们必须使用适当的命令行参数运行CMake任务两次(一次用于项目配置,然后用于实际构建(。
编辑2:
这是我更新的AzurePipelines.yml文件
Azure管道CI
stages:
- stage: Build
displayName: Build
jobs:
- job: RunCMakeTask
displayName: Run CMake Task
strategy:
matrix:
LinuxDebug:
OS: 'Linux'
imageName: 'ubuntu-latest'
BuildConfiguration: 'Debug'
LinuxRelease:
OS: 'Linux'
imageName: 'ubuntu-latest'
BuildConfiguration: 'RelWithDebInfo'
MacOSDebug:
OS: 'MacOS'
imageName: 'macos-latest'
BuildConfiguration: 'Debug'
MacOSRelease:
OS: 'MacOS'
imageName: 'macos-latest'
BuildConfiguration: 'RelWithDebInfo'
WindowsDebug:
OS: 'Windows'
imageName: 'windows-latest'
BuildConfiguration: 'Debug'
WindowsRelease:
OS: 'Windows'
imageName: 'windows-latest'
BuildConfiguration: 'RelWithDebInfo'
pool:
vmImage: $(imageName)
steps:
- script: mkdir $(BuildConfiguration)
displayName: Create Build Directory
workingDirectory: $(Build.SourcesDirectory)
- task: CMake@1
displayName: Generate CMake Cache
inputs:
workingDirectory: $(BuildConfiguration)
cmakeArgs: '-DCMAKE_BUILD_TYPE=$(BuildConfiguration) ..'
- task: CMake@1
displayName: Run Build Process
inputs:
workingDirectory: $(BuildConfiguration)
cmakeArgs: '--build . --config $(BuildConfiguration)'
- task: PublishPipelineArtifact@1
displayName: Publish Build Artifact
inputs:
targetPath: $(BuildConfiguration)
artifactName: '$(OS)$(BuildConfiguration)'
- stage: Test
displayName: Test
dependsOn: Build
jobs:
- job: RunCTestOnWindows
displayName: Run CTest on Windows
variables:
OS: Windows
strategy:
matrix:
Debug:
BuildConfiguration: 'Debug'
Release:
BuildConfiguration: 'RelWithDebInfo'
pool:
vmImage: 'windows-latest'
steps:
- task: DownloadPipelineArtifact@2
displayName: Download Build Artifact
inputs:
artifact: '$(OS)$(BuildConfiguration)'
path: $(Build.SourcesDirectory)/$(BuildConfiguration)
- script: ctest -C $(BuildConfiguration) --output-on-failure
workingDirectory: $(BuildConfiguration)
- job: RunCTestOnUnixBasedSystems
displayName: Run CTest on Unix Based Systems
strategy:
matrix:
LinuxDebug:
OS: 'Linux'
imageName: 'ubuntu-latest'
BuildConfiguration: 'Debug'
LinuxRelease:
OS: 'Linux'
imageName: 'ubuntu-latest'
BuildConfiguration: 'RelWithDebInfo'
MacOSDebug:
OS: 'MacOS'
imageName: 'macos-latest'
BuildConfiguration: 'Debug'
MacOSRelease:
OS: 'MacOS'
imageName: 'macos-latest'
BuildConfiguration: 'RelWithDebInfo'
steps:
- task: DownloadPipelineArtifact@2
displayName: Download Build Artifact
inputs:
artifact: '$(OS)$(BuildConfiguration)'
path: $(Build.SourcesDirectory)/$(BuildConfiguration)
- script: find $(BuildConfiguration)/Tests -type f -name "Test*" ! -name "*.*" ! -exec chmod u+rx {} ;
displayName: Change File Permissions
- script: ctest -C $(BuildConfiguration) --output-on-failure
workingDirectory: $(BuildConfiguration)
管道在Windows和Linux上运行良好,但我在MacOS上遇到了一个小问题。在MacOS上,ctest
即使在那里也无法找到测试可执行文件。(如果我的管道或CMakeLists.txt文件中有任何问题,它也应该在Windows和Linux上失败(
编辑3:
在MacOS的测试阶段,我得到了错误:
测试项目/home/vsts/work/1/s/Debug开始1:StringOperations_CaseIgnore找不到可执行文件/Users/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperations在以下位置查看:/Users/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperations/Users/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperations/Users/runner/work/1/s/Debug/Tests/StringOperations/Debug/TestStringOperations/Users/runner/work/1/s/Debug/Tests/StringOperations/Debug/TestStringOperations调试//用户/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperations调试//用户/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperationsUsers/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperationsUsers/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperationsUsers/runner/work/1/s/Debug/Tests/StringOperations/Debug/TestStringOperationsUsers/runner/work/1/s/Debug/Tests/StringOperations/Debug/TestStringOperationsDebug/Users/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperationsDebug/Users/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperations1/1测试#1:StringOperations_CaseIgnore***未运行0.00秒
0%的测试通过,1个中有1个测试失败
总测试时间(实际(=0.00秒
以下测试失败:1-StringOperations_CaseIgnore(不是运行(找不到可执行文件:/Users/runner/work/1/s/Debug/Tests/StringOperations/TestStringOperations运行CTest 时出错
编辑4:
我试图通过使用来检查测试可执行文件是否真的存在
ls -l Debug/Tests/StringOperations
这是输出:
drwxr-xr-x 3 vsts docker 4096 Aug 6 15:05 CMakeFiles
-rw-r--r-- 1 vsts docker 1208 Aug 6 15:05 cmake_install.cmake
-rw-r--r-- 1 vsts docker 642 Aug 6 15:05 CTestTestfile.cmake
-rw-r--r-- 1 vsts docker 9838 Aug 6 15:05 Makefile
-rwxr--r-- 1 vsts docker 1715072 Aug 6 15:05 TestStringOperations
这证实了测试可执行文件(TestStringOperations(在Windows和Linux的相同位置,但该过程仍然失败。
如果你需要的话,下面是这个可执行文件的CMakeLists.txt:
Set(SRC StringOperations.cpp)
Add_Executable(TestStringOperations ${SRC})
Target_Include_Directories(TestStringOperations PUBLIC
${HEADER_PATH}/StringOperations
)
Target_Link_Libraries(TestStringOperations
PRIVATE ${GTEST_LIBS}
PRIVATE StringOperations
)
Add_Test(NAME StringOperations_CaseIgnore COMMAND TestStringOperations)
我试着在Stack Overflow和其他一些网站上寻求关于这个问题的帮助,但他们的解决方案对我没有好处。
例如:CTest找不到可执行文件和CMake:如何指定CTest应该查找可执行文件的目录?
如果你需要更多信息,这里是我在GitHub上的项目。您也可以参考dev.azure.com.上的管道日志
你能帮我解决这个问题吗?欢迎就本文件的总体实施提出任何建议。
尝试在构建后运行发布管道工件任务。正如评论中所回避的那样,这将发布您的构建内容,并允许跨阶段共享。
完成此操作后,您还可以在管道的UI中将其视为已发布的工件。
# Publish pipeline artifacts
# Publish (upload) a file or directory as a named artifact for the current run
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Pipeline.Workspace)'
artifactName: # 'drop'
另外,在这个阶段结束时可以进行的另一项彻底检查是包括一个powershell脚本,以查看您正在工作的目录的内容:
-powershell: Get-ChildItem -Path 'Insert root path' -recurse