2017-09-30 45 views
2

我有一个共享库,我在一个叫jenkinsfile这样的.groovy作为脚本:如何访问共享库中的文件?

MySharedLibFunction{ .. some args} 

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

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

回答

3

您只能使用内置步骤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文件。

+0

我也使用这样的解决方案。但它似乎对我来说很笨拙 - 我更喜欢像选项一样自动将库(或至少它是'资源'文件夹)复制到工作区的选项... – Roman