Using Jenkins Version Environment Variable in the MSBuild step

后端 未结 4 820
情深已故
情深已故 2021-02-05 23:45

I\'m currently using the Jenkins \"Version Number Plug-In\" to set an Environment Variable for build version. This works fine within jenkins but i need a way to pass this to MSB

相关标签:
4条回答
  • 2021-02-06 00:15

    The easiest way I´ve found to do this is using the Change Assembly Version. Using that plugin you only need to provide the version number (or the environment variable used) and it will replace it in all the AssemblyInfo.cs files in your workspace.

    0 讨论(0)
  • 2021-02-06 00:20

    A very quick work around to this is to create a powershell task before your msbuild task. This is assuming you defined VERSION variable as above (without the %%). And you checked delete workspace before build starts.

    This is the code for the powershell task.

    gci -rec -Filter *AssemblyInfo* | ForEach { (Get-Content $_.FullName) | ForEach-Object {$_ -replace "1.0.0.0",$env:VERSION } | Set-Content $_.FullName}
    
    0 讨论(0)
  • 2021-02-06 00:30

    Environment Variable Name should be written without the percent-marks ('%'), like this:

    VERSION
    

    apart from that, I think you are good to go.

    May also consider changing
    ${BUILDS_ALL_TIME}
    to
    ${BUILDS_ALL_TIME, XX}

    (this will enforce a two-digit build-number with leading zeros)

    0 讨论(0)
  • 2021-02-06 00:32

    This is a MSBuild target that replaces the revision number in the file GlobalAssemblyInfo.cs with the SVN_REVISION variable from Jenkins.

    We have a multi project solution where each project references the same GlobalAssemblyInfo for common information (like version). Modify this so it fits your setup.

    By having the Exists condition the target executes on developer machines where the MSBuildCommunityTasks is not installed. In those cases the version number in GlobalAssemblyInfo.cs is left untouched.

    <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>
    
    0 讨论(0)
提交回复
热议问题