使用Jenkins pipeline执行需要参数的Powershell脚本

4

我有一个类似于这样的项目:

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?


1
这意味着它在“当前目录”(.)中找不到该脚本。你知道运行该命令时的当前工作目录是什么吗?如果不知道,也许可以加上 pwdls 等命令来查看输出。也许你所在的目录是 project,那么你需要调用 ./src/script.ps1(顺便说一下,PowerShell 在使用正斜杠 / 时效果很好,以防你想避免一直转义反斜杠)。 - briantist
嘿,感谢提供/信息!我一直以为Jenkins的工作空间是vars目录,但我会尝试更改路径,看看能否得到任何结果。 - starscream_disco_party
2个回答

3

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

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

& "absolutePathToScript/script.ps1" 

我不是百分之百确定,但 Jenkins 会通过 $env:workspace 注入工作空间(至少在普通 PowerShell 构建步骤中会这样做),所以你可以通过以下方式调用脚本:

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

工作区是git存储库,所以我最终不得不重新定位脚本。不过还是感谢您提供的有用信息! - starscream_disco_party

1
我只是个小白,Workspace 是正在构建的 Git 仓库。我必须将脚本移到该项目中,然后它按预期工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接