Azure DevOps使用来自Docker Hub的自定义Docker映像构建C ++项目

问题描述

我的目标是使用Microsoft Visual Studio c ++编译器在Windows环境中使用Azure DevOps管道在位桶上构建一个c++ project

由于我的项目是一个复杂的项目,具有一定的依赖性,所以我认为应该使用docker映像来构建它。为了编译我的项目,我使用CMake,因此我使用了msvc-2019 docker并添加了CMake(我知道原来的版本已经将CMake作为Microsoft Visual Studio c ++可选工具的一部分了,但是为了进行测试,我需要下载并安装一些软件包)。如果我从计算机本地运行此docker image,则可以编译并运行c ++示例项目。为此,我从Windows Docker Desktop应用程序运行docker映像,然后打开该正在运行的docker映像的CLI。一旦进入码头,我就会这样做:

git clone http://bitbucket.org/apalomer/window_docker_azuredevops_example.git
cd window_docker_azuredevops_example
mkdir build
cd build
cmake ..
cmake --build . --config RelWithDebInfo
RelWithDebInfo\hello_world.exe

哪个产生输出:

C:\window_docker_azuredevops_example\build>cmake ..
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763.
-- The CXX compiler identification is MSVC 19.27.29111.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.291
10/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/window_docker_azuredevops_example/build

C:\window_docker_azuredevops_example\build>cmake --build . --config RelWithDebInfo
Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  Building Custom Rule C:/window_docker_azuredevops_example/CMakeLists.txt
  hello_world.cpp
  hello_world.vcxproj -> C:\window_docker_azuredevops_example\build\RelWithDebInfo\hello_world.exe
  Building Custom Rule C:/window_docker_azuredevops_example/CMakeLists.txt

C:\window_docker_azuredevops_example\build>RelWithDebInfo\hello_world.exe
hello world

因此虚拟环境可以正常工作。

现在docker映像可以工作了,我在Azure DevOps中创建了一个project并创建了一个新管道。在管道创建期间,我选择Bitbucket Cloud作为源,选择正确的存储库,然后在其中选择azure-pipelines.yml文件。该文件的内容如下:

# C/C++ with GCC
# Build your C/C++ project with GCC using make.
# Add steps that publish test results,save build artifacts,deploy,and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/c-cpp/gcc

trigger:
- master
- hotfix/*
- release/*

pool:
  vmImage: 'windows-2019'

container: apalomer/windows:msvc-2019

steps:
- script: |
    mkdir build
    cd build
    cmake ..
    cmake --build . --config RelWithDebInfo
    RelWithDebInfo\hello_world.exe
  displayName: 'hello world compilation'

但是,管道无法构建,并出现以下错误:

-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763.
CMake Error at CMakeLists.txt:3 (project):
  Failed to run MSBuild command:

    C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe

  to get the value of VCTargetsPath:

    Exit code 0xc0000142
    



-- Configuring incomplete,errors occurred!

在我看来,这毫无意义,因为所使用的Docker映像已安装了正确的软件,并且在DockerFile以vcvarsall.bat x64结尾的情况下,应该调用CMD ["C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat","x64","&&","cmd.exe","-NoLogo","-ExecutionPolicy","Bypass"]

有人可以指出我做错了什么吗?

P.S。给定的example project没有任何依赖性,但是,它说明了我的问题,并且一旦起作用,构建具有正确依赖性的新Docker映像将不是一项复杂的任务。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)