powershell - 包含在 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}"
...
解决方案
用 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
}
}
它将读取所有相关文件并仅替换与版本正则表达式匹配的每次出现的版本的构建部分。
推荐阅读
- c++ - 每个节点 4 个字符的字符串双向链表
- data-science - 在 n > 2 的情况下,评估 n 向拆分测试最有说服力的方法是什么?
- powershell - 如何使用正则表达式删除以前的数据库快照?
- c# - 如何隐藏在 Visual Studio 16.10 中随处可见的 ...?
- linux - Radeon 开发人员面板未检测到正在运行的程序
- javascript - 这个 id 比较算法有什么问题,内置在 NodeJs 中?
- android - 应用程序在通过 UsbManager 授予连接 [设备] 的权限时崩溃
- typescript - 多个复选框的Vue过滤列表
- java - 现在我无法弄清楚如何在最早的截止日期的图像中显示正确的输出到目前为止这是我的代码如何
- typescript - 在空对象上使用所需的属性