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

标签 jenkins jenkins-plugins jenkins-pipeline

我有一个带有.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/

相关文章:

jenkins - 在 Docker 插件中构建 Jenkins 时安装失败

Jenkins-取消卸载插件

jenkins - 在 Jenkins Pipeline Library 中查找未使用的端口

jenkins - 如何在声明性管道中获取下游作业的 build_number?

amazon-web-services - 云信息 : Outputs - Can you return a value from a file

Jenkins SSH 插件 : Parameterizing remote host

macos - Jenkins调用unix/var/run/docker.sock : connect: permission denied MacOS

jenkins - 是否可以组合或混合 Jenkins 声明式和脚本化管道?

git pull origin master,不 pull 检查是否有冲突

Jenkins Pipeline 每个分支的环境变量配置