首页 > 解决方案 > 包含在 Jenkins 管道脚本中时,Powershell 脚本不起作用

问题描述

我试图用 Powershell 实现的目标如下:在 Build Server 上的 AssemblyInfo.cs 文件中增加 Build Number。经过 100 多次不同变体的迭代后,我的脚本现在看起来像下面这样,我仍然无法让它工作。该脚本在 Powershell 控制台中运行良好,但是当包含在 Jenkins 管道脚本中时,我得到了各种难以修复的错误......

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

标签: powershelljenkins

解决方案


用 Jenkins 关键字代替 PowerShell 的 groovy 方法怎么样:

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

它将读取所有相关文件并仅替换与版本正则表达式匹配的每次出现的版本的构建部分。


推荐阅读