【问题标题】:Using Jenkins Version Environment Variable in the MSBuild step在 MSBuild 步骤中使用 Jenkins 版本环境变量
【发布时间】:2012-09-02 22:42:03
【问题描述】:

我目前正在使用 Jenkins“版本号插件”为构建版本设置环境变量。这在 jenkins 中运行良好,但我需要一种方法将其传递给 MSBuild,以便更新 exe 和 dll 的版本号。我尝试了下面的配置,但它没有更新构建版本

【问题讨论】:

    标签: msbuild jenkins jenkins-plugins


    【解决方案1】:

    环境变量名称应该写成没有百分号('%'),像这样:

    VERSION
    

    除此之外,我认为你很高兴。

    也可以考虑换
    ${BUILDS_ALL_TIME}

    ${BUILDS_ALL_TIME, XX}

    (这将强制使用带有前导零的两位内部版本号)

    【讨论】:

    • 是的,我想通了,而且似乎 MSBuild.exe 默认不支持 /p:version。你需要搞乱 MSBuld 扩展。我最终只是编写了一个控制台应用程序来在构建之前更新 AssemblyInfo.cs 并在环境变量中调用它。这比使用复杂的 msbuild 扩展要快
    • 好吧,我希望你能找到一种将版本传递给构建的好方法,因为我们还为此使用了定制的脚本......
    【解决方案2】:

    这是一个 MSBuild 目标,它将文件 GlobalAssemblyInfo.cs 中的修订号替换为来自 Jenkins 的 SVN_REVISION 变量。

    我们有一个多项目解决方案,其中每个项目引用相同的 GlobalAssemblyInfo 以获得公共信息(如版本)。修改它,使其适合您的设置。

    通过存在条件,目标在未安装 MSBuildCommunityTasks 的开发人员计算机上执行。 在这些情况下,GlobalAssemblyInfo.cs 中的版本号保持不变。

    <Target Name="InjectSVNRevision" Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets')">
    <!-- When this build is done by the Jenkins CI server; the svn revision number is set in the environment variable SVN_REVISION
         This little snippet replaces the revision number in GlobalAssemblyInfo.cs with this svn revision number
      -->
    <Message Text="Injecting SVN revision number in GlobalAssemblyInfo.cs" />
    <FileUpdate Files="GlobalAssemblyInfo.cs"
        Multiline="true"
        Singleline="false"
        Regex="(AssemblyVersion|AssemblyFileVersionAttribute|AssemblyFileVersion)\(&quot;([0-9]+\.[0-9]+\.[0-9]+)(\.[0-9]+)?&quot;\)"
        ReplacementText="$1(&quot;$2.$(SVN_REVISION)&quot;)" 
        Condition="$(SVN_REVISION) != '' "/>
    </Target>
    

    【讨论】:

      【解决方案3】:

      我发现最简单的方法是使用Change Assembly Version。使用该插件,您只需提供版本号(或使用的环境变量),它将在您工作区的所有 AssemblyInfo.cs 文件中替换它。

      【讨论】:

        【解决方案4】:

        解决此问题的一个非常快速的方法是在您的 msbuild 任务之前创建一个 powershell 任务。这是假设您如上所述定义了 VERSION 变量(没有 %%)。并且您在构建开始之前检查了删除工作区。

        这是 powershell 任务的代码。

        gci -rec -Filter *AssemblyInfo* | ForEach { (Get-Content $_.FullName) | ForEach-Object {$_ -replace "1.0.0.0",$env:VERSION } | Set-Content $_.FullName}
        

        【讨论】:

          【解决方案5】:

          以下是我的总结,使其发挥作用:

          安装此插件:

          https://plugins.jenkins.io/versionnumber/
          https://plugins.jenkins.io/change-assembly-version-plugin/
          

          确保您的 AssemblyInfo.cs 包含:

          [assembly: AssemblyVersion("1.0.0.0")]
          [assembly: AssemblyFileVersion("1.0.0.0")]
          

          设置您的版本变量名称不带“$”。

          定义一个不错的版本号,例如:'1.1.${BUILD_NUMBER}.${SVN_REVISION}'

          添加“更改程序集版本”-构建步骤。 在“${...}”示例中输入您的变量:“${VERSION}”

          如果您仍然遇到构建错误。检查项目文件和 AssemblyInfo.cs 是否存在合并冲突。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-04-04
            • 2015-07-09
            • 1970-01-01
            • 1970-01-01
            • 2017-02-03
            • 2018-09-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多