Siddharth Seth
Siddharth Seth

Reputation: 664

Powershell script not working when included in Jenkins Pipeline Script

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}"
...

Upvotes: 0

Views: 421

Answers (1)

Noam Helmer
Noam Helmer

Reputation: 6824

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.

Upvotes: 1

Related Questions