我有一个带有 .groovy 脚本的共享库,我在 jenkinsfile 中调用该脚本,如下所示:
MySharedLibFunction{ .. some args}
我的共享库中还有一个我想要执行的 .ps1 文件。但如果我这样做powershell pwd
从我的共享库函数中,当我从 jenkinsfile 调用该函数时,当前工作目录是 jenkinsfile 所在管道的 jenkins 工作目录(这通常是您想要的)。
有没有办法访问共享库中的文件?我想要做powershell -File ps1FileInMySharedLibVarsFolder.ps1
您只能使用内置步骤获取内容libraryResource
。这就是为什么有以下功能我的共享库 https://github.com/TYPO3-infrastructure/jenkins-pipeline-global-library-chefci/blob/master/src/org/typo3/chefci/helpers/JenkinsHelper.groovy将其复制到临时目录并返回文件的路径:
/**
* 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
在您的共享库中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)