gpt4 book ai didi

jenkins - 如何访问共享库中的文件?

转载 作者:行者123 更新时间:2023-12-04 10:23:02 24 4
gpt4 key购买 nike

我有一个带有.groovy脚本的共享库,可以在jenkinsfile中调用它,如下所示:

MySharedLibFunction{ .. some args}

我要执行的共享库中也有一个.ps1文件。但是,如果当我从jenkinsfile调用共享库函数时从共享库函数中执行 powershell pwd,则当前工作目录就是jenkinsfile所在管道的jenkins工作目录(通常是您想要的)。

有没有办法访问共享库中的文件?我想做 powershell -File ps1FileInMySharedLibVarsFolder.ps1

最佳答案

您只能使用内置步骤libraryResource获得内容。这就是为什么在my shared library中具有以下功能将其复制到临时目录并返回该文件的路径的原因:

/**
* Generates a path to a temporary file location, ending with {@code path} parameter.
*
* @param path path suffix
* @return path to file inside a temp directory
*/
@NonCPS
String createTempLocation(String path) {
String tmpDir = pwd tmp: true
return tmpDir + File.separator + new File(path).getName()
}

/**
* Returns the path to a temp location of a script from the global library (resources/ subdirectory)
*
* @param srcPath path within the resources/ subdirectory of this repo
* @param destPath destination path (optional)
* @return path to local file
*/
String copyGlobalLibraryScript(String srcPath, String destPath = null) {
destPath = destPath ?: createTempLocation(srcPath)
writeFile file: destPath, text: libraryResource(srcPath)
echo "copyGlobalLibraryScript: copied ${srcPath} to ${destPath}"
return destPath
}

当它返回临时文件的路径时,您可以将其传递到需要文件名的任何步骤:
sh(copyGlobalLibraryScript('test.sh'))

用于共享库中 resources/test.sh中驻留的文件。

关于jenkins - 如何访问共享库中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46498924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com