0

What I am trying to achieve with Powershell is as follows: Increment the Build Number in the AssemblyInfo.cs file on the Build Server. My Script looks like below right now after over a 100 iterations of different variations I am still unable to get it to work. The script works well in the Powershell console but when included into the Jenkins Pipeline Script I get various errors that are proving hard to fix...

def getVersion (file) {
    def result = powershell(script:"""Get-Content '${file}' | 
    Select-String '[0-9]+\\.[0-9]+\\.[0-9]+\\.' | 
    foreach-object{$_.Matches.Value}.${BUILD_NUMBER}""", returnStdout: true)
    echo result
    return result
}
...
powershell "(Get-Content ${files[0].path}).replace('[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+', 
${getVersion(files[0].path)})) } | Set-Content ${files[0].path}"
...
3
  • 1
    Take a look at the Assembly Version Plugin, it pretty much does what you what out of the box. Here is the relevant pipeline step reference for pipeline jobs. Commented Jul 27, 2021 at 11:38
  • Yes, I did come across this plugin early on ... But it says on it's homepage "Up for Adoption"... that made me jittery about using it...Thus decided to roll my own script...I will check and try it out Commented Jul 27, 2021 at 12:12
  • Just checked with manager ... Not Ok to use the plugin ... Will have to stick to rolling out my own script ... Commented Jul 27, 2021 at 12:37

1 Answer 1

1

How about a groovy approach (with Jenkins keywords) instead of PowerShell:

def updtaeAssemblyVersion() {
   def files = findFiles(glob: '**/AssemblyInfo.cs')
   files.each {
       def content = readFile file: it.path
       def modifedContent = content.repalceAll(/([0-9]+\\.[0-9]+\\.[0-9]+\\.)([0-9]+)/,"\$1${BUILD_NUMBER}")
       writeFile file: it.path, text: modifedContent
   }
}

It will read all relevant files and replace only the build section of the version for every occurrence that matches the version regex.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, exactly what I ended up doing ... I was mixing Groovy code with Powershell script ... Which did not go down well ... Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.