Incrementing build number with Hudson build

5.2k views Asked by At

Here is what I am trying to achieve. I currently use Hudson build to do builds for me on a remote computer. I currently have to open my solution and manually update the [assembly: AssemblyVersion("1.2.6.190")] numbers in two files and then commit my changes to SVN before running the build through Hudson. (the hudson job is not set to run unless you clcik build now)

I would like to find a way to automatically increment only the last number every time Hudson does a build.

I would like it to increment by 1 (not timestamped or similar).

Any ideas or links to other material that may help would be appreciated =)

Thanks,

Toby

1

There are 1 answers

0
Nick Nieslanik On

I use the PowerShell plug-in for Jenkins and use Powershell to find all files that match a pattern (say AssemblyInfo.*), then read the files in and use the built-in regex functionality in PowerShell (the -match and -replace operations) to find and replace the AssemblyVersion attributes, changing the last octet to the current Jenkins build number.

function assign-build-number
{
    #get the build number form Jenkins env var
    if(!(Test-Path env:\BUILD_NUMBER))
    {
        return
    }

    #set the line pattern for matching
    $linePattern = 'AssemblyFileVersion'
    #get all assemlby info files
    $assemblyInfos = gci -path $env:ENLISTROOT -include AssemblyInfo.cs -Recurse

    #foreach one, read it, find the line, replace the value and write out to temp
    $assemblyInfos | foreach-object -process {
        $file = $_
        write-host -ForegroundColor Green "- Updating build number in $file"
        if(test-path "$file.tmp" -PathType Leaf)
        {
            remove-item "$file.tmp"
        }
        get-content $file | foreach-object -process {
            $line = $_
            if($line -match $linePattern)
            {
                #replace the last digit in the file version to match this build number.
                $line = $line -replace '\d"', "$env:BUILD_NUMBER`""
            }

            $line | add-content "$file.tmp"

        }
        #replace the old file with the new one
        remove-item $file
        rename-item "$file.tmp" $file -Force -Confirm:$false
   }
}