首页 > 解决方案 > 在 jenkins 文件中编写 Powershell 或直接在 jenkins 中编写

问题描述

我是 Jenkins 的新手,我想用它来自动化一些任务。Powershell 插件已安装和配置,目前可以使用。我已经有脚本来处理我的任务,比如说 12 个脚本文件,那么 12 个脚本文件。我有一些关于如何最好地将其集成到 Jenkins 管道中的问题。

我想知道是否最好将我的 Powershell 脚本作为单独的文件保留,或者将它们放在存储库中并让 Jenkins 将它们拉下来并运行它们,或者将 Powershell 文件的内容直接复制到 Jenkins 中并从那里运行它们而不是调用单个文件。

其次,我的脚本需要在它们之间传递值。是否有可能有一个 Jenkins 管道,它为不同的任务调用多个 Powershell 脚本文件,我可以将任务 1 中的值传递给 2 等吗?

我想知道最好的方法,调用 Ps 脚本文件并将值返回到 Jenkins 变量的示例会很好,无论是调用 PS 文件还是直接运行 Powershell 代码。

标签: powershelljenkins

解决方案


Where you store the PowerShell scripts depends a lot on the details and preference. Personally, I would put them right in the Jenkinsfile if I only needed them at this one place. If I needed them in different places, I'd put them in Git.

You can return the stdout of a PowerShell command, store it in a variable and use that in another PowerShell script like so:

def msg = powershell(returnStdout: true, script: 'Write-Output "PowerShell is mighty!"')
powershell "use $msg here"

Problem here is that you only can get strings out of it and no PowerShell objects. You can also return the exit code and store it in a variable.

For more information, see this.


推荐阅读