如何将 Jenkins 管道变量用于 powershell 或 cmd 代码?

How to use Jenkins pipeline variable into powershell or cmd code?

我需要修改 Jenkins 管道脚本。这是一个片段。它适用于重命名中的 Def。我得到的错误是

groovy.lang.MissingPropertyException: No such property: newFileName for class: groovy.lang.Binding

我希望将版本号注入文件名,然后由自主开发的应用程序处理该文件名,以将其移动到需要去的地方和其他一些东西。我尝试将连接的 ${version} 传递到上传可执行文件中,但它随后作为文字而不是变量出错。

更新:我预定义变量并得到一个新错误

powershell.exe : newFileName : The term 'newFileName' is not recognized as the name of a cmdlet, function, script file, or operable At C:\workspace\workspace\Centurion Dashboard@tmp\durable-eb5db1ae\powershellWrapper.ps1:3 char:1 & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm

  • CategoryInfo : NotSpecified: (newFileName : T...e, or >operable :String) [], RemoteException
    • FullyQualifiedErrorId : NativeCommandError

想法?

stage('Rename') {
      powershell """
        rename-item -Path "C:\Users\Administrator\Documents\Advanced Installer\Projects\Proj\Setup Files\SetupCenturionDashboard.exe" -NewName "OtherProject-${version}.exe"
        def newFileName = "C:\Users\Administrator\Documents\Advanced Installer\Projects\Proj\Setup Files\OtherProject-" + ${version} + ".exe"
        echo ${newFileName} 
    """
} 

    stage('Upload') {
      bat label: 'Upload', script: 'C:\Static\BuildHelper\BuildHelper.exe ${newFileName} "develop" "home" "${version}"'
}

powershell != groovy

您不能 运行 groovy 在 powershell 块内部

powershell """

"""

def newFileName 是 groovy,这就是你得到错误的原因:

The term 'newFileName' is not recognized as the name of a cmdlet...

如您所见,newFileName 可执行文件在 windows 上不存在。

变量 windows

如果您只需要将版本和 newFileName 变量传递给您的 powershell 或 bat 代码,您需要使用 windows syntax %var% 作为变量,而不是 unix ${var} 和如果可能的话在您的管道中使用全局变量:

def version = "1.0.0";
def newFileName;

stage('Rename') {
    powershell """
        rename-item -Path "C:\Users\foo.exe" -NewName "OtherProject-%version%.exe"
    """
    newFileName = "C:\Users\OtherProject-${version}.exe"
} 

stage('Upload') {
    bat label: 'Upload', script: 'C:\Static\BuildHelper\BuildHelper.exe %newFileName% "develop" "home" "%version%"'
}