【问题标题】:Assign value to powershell variable within Jenkins pipeline在 Jenkins 管道中为 powershell 变量赋值
【发布时间】:2021-07-18 16:35:02
【问题描述】:

我遇到了一些关于如何在 Jenkins 管道中运行 Powershell 脚本以及如何捕获输出的文档。 但是我想将捕获的输出用于下一个节点 powershell 脚本。 例如

node {
    def msg = powershell(returnStdout: true, script: 'Write-Output "PowerShell is mighty!"')
}

现在我想在 Powershell 脚本的下一个节点中使用msg。就像我们可以将其分配给 powershell 变量,然后使用该变量执行操作一样。 关于如何实现这一点的任何指示?

【问题讨论】:

    标签: powershell jenkins jenkins-pipeline


    【解决方案1】:

    您可以将 powershell 输出分配给环境变量并在后续节点中使用:

    node {
        env.msg = powershell(returnStdout: true, script: 'Write-Output "PowerShell is mighty!"')
    }
    
    node {
        def output = powershell(returnStdout: true, script: '''
            $message = ($env:msg).trim()
            Write-Output $message
        ''')
        println(output)
    }
    

    注意变量 msg 之前的 env 前缀。您可以使用 $env: 前缀后跟变量名在下一个节点的 powershell 中检索变量。不要忘记修剪 powershell 中的变量 (.trim()) 以删除换行符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 2021-12-05
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      相关资源
      最近更新 更多