How to access files in a shared library?

I have a shared library with a .groovy script that I call in a jenkins file like this:

MySharedLibFunction{ .. some args}

I also have a .ps1 file in my shared library that I want to execute. But if I execute powershell pwdfrom my shared library function, when I call this function from my jenkinsfile, the current working directory is the jenkins working directory of my pipeline, which contains the jenkinsfile (usually this is what you want).

Is there a way to access files in a shared library? I want to dopowershell -File ps1FileInMySharedLibVarsFolder.ps1

+4
source share
1 answer

libraryResource. , :

/**
  * 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 .

+4

Source: https://habr.com/ru/post/1686675/


All Articles