执行需要Jenkins管道自变量的Powershell脚本

时间:2019-06-05 15:17:14

标签: powershell jenkins continuous-integration

我有一个看起来像这样的项目:

project
|
├───src
│       Utility.groovy
│
└───vars
        pipeline.groovy
        script.ps1

pipeline.groovy中,我有实用程序对象:

def util = new Utility(this)

和调用util.scriptCall(arg1: "1", arg2: "2", arg3: "3")的步骤,该步骤定义为:

this.context.powershell returnStdout: true, script: ".\\script.ps1 -Arg1 ${args.arg1} -Arg2 ${args.arg2} -Arg3 ${args.arg3} -Workspace ${this.context.env.workspace}"

从我发现的所有内容来看,我都应该正确地调用它,但是出现以下错误:

.\script.ps1 : The term '.\script.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我已经安装了Powershell插件,但是缺少与Jenkins相关的任何文档的文档也无济于事。我已将script.ps1移到src文件夹中,甚至尝试为此创建一个scripts文件夹,并得到相同的结果。

知道我为什么调用脚本被解释为cmdlet吗?

2 个答案:

答案 0 :(得分:2)

因为该脚本在您尝试调用该脚本的位置不可用。

使用绝对路径调用脚本。如果路径包含空格,请使用调用运算符(&)。

& "absolutePathToScript/script.ps1" 

我不太确定,但是Jenkins将通过$env:workspace注入工作空间(至少它将在常规的PowerShell构建步骤中注入),因此您可以通过以下方式调用脚本:

& "$($env:workspace)/vars/script.ps1"

答案 1 :(得分:1)

我只是叮当。工作区是正在构建的Git存储库。我不得不将脚本重新定位到该项目,并且按预期工作。

相关问题