如何访问共享库中的文件? [英] How do I access files in a Shared Library?

查看:141
本文介绍了如何访问共享库中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有.groovy脚本的共享库,我在jenkinsfile中调用了这样的脚本:

I have a Shared Library with a .groovy script that I call in a jenkinsfile like this:

MySharedLibFunction{ .. some args}

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

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

是否可以访问共享库中的文件?我想做powershell -File ps1FileInMySharedLibVarsFolder.ps1

Is there a way to access files in the shared lib? I want to do powershell -File ps1FileInMySharedLibVarsFolder.ps1

推荐答案

您只能使用内置步骤libraryResource获取内容.这就是为什么

You can only get the contents using the built-in step libraryResource. That's why have the following functions in my shared library to copy it to a temporary directory and return the path to the file:

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

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

As it returns the path to the temp file, you can pass this to any step expecting a file name:

sh(copyGlobalLibraryScript('test.sh'))

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

for a file residing in resources/test.sh within your shared library.

这篇关于如何访问共享库中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆