我有一个由 Jenkins 运行的 Groovy 脚本,该脚本包含指令:
Boolean myValue = powershell(script: "./myScript.ps1", returnStatus: true)
运行 Groovy 脚本时,出现以下错误:
powershell.exe : ./myScript.ps1 : The term './myScript.ps1' is not recognized as the name of a
At C:\Program Files (x86)\Jenkins\workspace\MyProject@tmp\durable-f15db675\powershellWrapper.ps1:3 char:1
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (./myScript.ps1... the name of a :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
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.
At C:\Program Files (x86)\Jenkins\workspace\MyProject@tmp\durable-f15db675\powershellScript.ps1:1 char:1
+ ./myScript.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (./myScript.ps1:String) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : CommandNotFoundException
和
myValue
那么 True
。
Groovy 和 PowerShell 脚本都位于同一目录中(至少在 Jenkins 获取 Groovy 脚本的 Git 存储库上)。
在 Groovy 脚本中,还有其他 PowerShell 调用,但使用直接命令,而不是 ps1 脚本文件。他们都很好。
myValue == true
?是因为returnStatus
的说法吗?编辑:为脚本指定的路径是错误的。这 ”。”目录指向工作区,而不是 Groovy 脚本所在的目录。我现在必须找到一种方法来走这条路......
如果 PowerShell 脚本与 Groovy 脚本位于同一目录中,则必须通过 后者 的目录路径调用它,而不是 current (
.
):
注:
以下内容适用于独立的 Groovy 脚本,但是这个答案表明 Jenkins
可能不允许使用
new File()
。我自己无法测试。
这个答案。 此配套答案通过 @SourceUri
// Get the full path of the 'myScript.ps1' script located in the same
// dir. as the executing Groovy script.
powerShellScriptFilePath =
new File(
new File(getClass().protectionDomain.codeSource.location.path).parent,
'myScript.ps1'
)
Boolean myValue = powershell(script: powerShellScriptFilePath, returnStatus: true)