如何使用jenkinsfile中的参数调用resources文件夹中的脚本?

问题描述 投票:1回答:1

我有一个脚本我想传递参数并称之为My-Script.ps1 -arg1 blah -arg2 lkjh

我的脚本在资源文件夹中,我可以像这样导入和调用它:

def myScript = libraryResource 'PSScripts/My-Script.ps1'
powershell myScript

这按预期工作,但我怎么能传递它的参数?

jenkins jenkins-plugins jenkins-pipeline
1个回答
0
投票

这只是一种可以使用的方法。我结束了在函数中包装PowerShell脚本,然后使用名为powershellLibraryScript的共享库和以下代码:

def call(args) {
    args = [
        resource: null,
        script: ""
    ] << args
    def script = libraryResource(args.resource)
    powershell """
$script
# ---------------------------------------------
$args.script
    """
}

然后我打电话给它

powershellLibraryScript resource: 'MyPowerShellResource.ps1', script: 'MyFunction -Arg1 Value1 -Arg2 Value2'

powershell脚本就可以像

function MyFunction() {  
    [CmdletBinding()]
    param (
        [Parameter(Position=0, Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [string] $arg1 = "value1",

        [Parameter(Position=1, Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [string] $arg2 = "value2"
    )

    # Code...
}
© www.soinside.com 2019 - 2024. All rights reserved.