c++ - Gitlab CI Runner 预定义宏 MSBuild

标签 c++ visual-studio msbuild gitlab

您好,我正在尝试使用环境变量作为 Gitlab 运行器设置上 MSBuild 过程的一部分,以传递 CI_PIPELINE_ID 和 CI_COMMIT_SHA 以使用增量构建 ID 和提交 sha 来构建应用程序以跟踪二进制文件版本。

我的.gitlab-ci.yml 配置如下:

variables:
  Solution: Project.sln

before_script:
  - "echo off"
  - 'call "%VS140COMNTOOLS%\vsvars32.bat"'
  - echo.
  - set
  - echo.
  - echo %HALCONROOT%|find "13" >nul
  - if errorlevel 1 (echo not13) else (set HALCONVERSION=HALCON_13)

stages:
  - build

build:
  stage: build
  script:
  - echo building...
  - 'msbuild.exe /p:Configuration="Release" /p:Platform="x64" "%Solution%"'
  tags:
  - "HALCON 13"
  except:
  - tags

我需要做什么才能在我的 C++ 项目中访问这样的环境变量?

#ifndef CI_COMMIT_SHA
#define COMMIT_SHA                  0
#else
#define COMMIT_SHA                  CI_COMMIT_SHA
#endif

最佳答案

你可以使用Settings --> CD/CD --> Secret variables来定义你想在.yml中使用的变量,例如:

Secret variables

然后你可以像下面这样使用它:

script:
- echo $USERNAME

关于在C++中访问环境变量,你可以试试:

std::string getEnvVar( std::string const & key ) const
{
    char * val = getenv( key.c_str() );
    return val == NULL ? std::string("") : std::string(val);
}

关于c++ - Gitlab CI Runner 预定义宏 MSBuild,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47876903/

相关文章:

C++ 不识别 3points 运算符

c++ - 更改 DEF 文件中的函数签名

asp.net-mvc - 安装 KB2993928 后 ASP.NET MVC4 解决方案无法构建

c++ - 在 C++ 中函数 SFINAE 的方法

c++ - 这个右值签名模式有意义吗?

C++ Gdiplus 单色像素值

visual-studio - Visual Studio 中是否有类似 Eclipse Perspective 的东西?

visual-studio-2012 - 使用 msbuild.exe 时显示列错误

command-line - 强制控制台命令的区域性

c++ - 为什么定义 operator+ 来调用 operator+= 比相反更有效?