如何在 python 中将动态参数传递给 jenkins 资源方法?
How to pass dynamic params to jenkins resource method in python?
我正在尝试使用资源目录中的一些 python 函数扩展 Jenkins 共享库。我可以在我的 jenkins 管道中调用该函数,但我很难传递参数。
在我的 jenkinsfile 中,我尝试了以下内容
script{
getLabelsPerPullRequest.runMyPython(git_url="${env.GIT_URL}", github_token="${env.GITHUB_CREDENTIALS_ID}", prNbr_name="${BRANCH_NAME}")
}
vars 文件夹中的 getLabelsPerPullRequest.runMyPython
如下所示:
def runMyPython(String git_url, String github_token, String prNbr_name) {
final pythonContent = libraryResource('com/sophia/sharedlib/getLabelsPerPullRequest.py')
sh('echo ${git_url} ${github_token} ${prNbr_name}')
writeFile(file: 'getLabelsPerPullRequest.py', text: pythonContent)
sh('chmod +x getLabelsPerPullRequest.py && ./getLabelsPerPullRequest.py -u ${git_url} -t ${github_token} -p ${prNbr_name}')
}
echo
没有返回任何东西。如何将参数从管道传递给函数?
目标是能够使用传递的参数替换最后一个 sh
命令中的标志。我可以使用相同的方法 运行 一个没有参数的 python 脚本,但是这些只能做这么多。
你的共享库函数runMyPython
没问题,只需要用双引号""
代替单引号''
来启用字符串插值和变量替换:
("echo ${git_url} ${github_token} ${prNbr_name}")
另一件事是,据我所知,groovy 不像您使用的那样支持命名参数,它支持 named arguments as map,因此您需要更新对共享库方法的调用至:
getLabelsPerPullRequest.runMyPython(env.GIT_URL, env.GITHUB_CREDENTIALS_ID, BRANCH_NAME)
或者更改您的 runMyPython
函数以接收地图,然后使用 param:value
表示法调用它。
我正在尝试使用资源目录中的一些 python 函数扩展 Jenkins 共享库。我可以在我的 jenkins 管道中调用该函数,但我很难传递参数。
在我的 jenkinsfile 中,我尝试了以下内容
script{
getLabelsPerPullRequest.runMyPython(git_url="${env.GIT_URL}", github_token="${env.GITHUB_CREDENTIALS_ID}", prNbr_name="${BRANCH_NAME}")
}
vars 文件夹中的 getLabelsPerPullRequest.runMyPython
如下所示:
def runMyPython(String git_url, String github_token, String prNbr_name) {
final pythonContent = libraryResource('com/sophia/sharedlib/getLabelsPerPullRequest.py')
sh('echo ${git_url} ${github_token} ${prNbr_name}')
writeFile(file: 'getLabelsPerPullRequest.py', text: pythonContent)
sh('chmod +x getLabelsPerPullRequest.py && ./getLabelsPerPullRequest.py -u ${git_url} -t ${github_token} -p ${prNbr_name}')
}
echo
没有返回任何东西。如何将参数从管道传递给函数?
目标是能够使用传递的参数替换最后一个 sh
命令中的标志。我可以使用相同的方法 运行 一个没有参数的 python 脚本,但是这些只能做这么多。
你的共享库函数runMyPython
没问题,只需要用双引号""
代替单引号''
来启用字符串插值和变量替换:
("echo ${git_url} ${github_token} ${prNbr_name}")
另一件事是,据我所知,groovy 不像您使用的那样支持命名参数,它支持 named arguments as map,因此您需要更新对共享库方法的调用至:
getLabelsPerPullRequest.runMyPython(env.GIT_URL, env.GITHUB_CREDENTIALS_ID, BRANCH_NAME)
或者更改您的 runMyPython
函数以接收地图,然后使用 param:value
表示法调用它。