在 Jenkins Pipeline 插件中获取工作区目录的绝对路径 [英] Get absolute path to workspace directory in Jenkins Pipeline plugin

查看:32
本文介绍了在 Jenkins Pipeline 插件中获取工作区目录的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在对 Jenkins Pipeline 插件(以前称为 Workflow 插件)进行一些评估.阅读文档我发现我目前无法使用env.WORKSPACE:

I'm currently doing some evaluation on the Jenkins Pipeline plugin (formerly know as Workflow plugin). Reading the documentation I found out that I currently cannot retriev the workspace path using env.WORKSPACE:

以下变量当前在工作流脚本中不可用:

The following variables are currently unavailable inside a workflow script:

NODE_LABELS

NODE_LABELS

工作空间

SCM 特定的变量,例如 SVN_REVISION

SCM-specific variables such as SVN_REVISION

有没有其他方法可以获取当前工作区的绝对路径?我需要它运行一些测试,而这些测试又会获取一些参数(某些可执行文件的绝对路径).我已经尝试在 @NonCPS 部分中使用 new File("").absolutePath() ,但看起来非 CPS 的东西总是在主服务器上执行.

Is there any other way how to get the absolute path to the current workspace? I need this running some test which in turn gets some parameter (absolute path to some executable file). I already tried using new File("").absolutePath() inside a @NonCPS section but looks like the non-CPS stuff gets always executed on the master.

有没有人知道如何在不运行一些批处理脚本的情况下获得这个路径,该脚本将路径存储到某个文件中,稍后可以再次读取?

Does anybody have a clue how to get this path without running some batch script which stores the path into some file which later on can be read in again?

推荐答案

Pipeline Nodes and Processes Plugin(Pipeline 插件的一个组件,默认安装),WORKSPACE 环境变量再次可用.此版本于 2016 年 9 月 23 日发布,因此它应该适用于所有最新的 Jenkins 实例.

Since version 2.5 of the Pipeline Nodes and Processes Plugin (a component of the Pipeline plugin, installed by default), the WORKSPACE environment variable is available again. This version was released on 2016-09-23, so it should be available on all up-to-date Jenkins instances.

node('label'){
    // now you are on slave labeled with 'label'
    def workspace = WORKSPACE
    // ${workspace} will now contain an absolute path to job workspace on slave

    workspace = env.WORKSPACE
    // ${workspace} will still contain an absolute path to job workspace on slave

    // When using a GString at least later Jenkins versions could only handle the env.WORKSPACE variant:
    echo "Current workspace is ${env.WORKSPACE}"

    // the current Jenkins instances will support the short syntax, too:
    echo "Current workspace is $WORKSPACE"

}

这篇关于在 Jenkins Pipeline 插件中获取工作区目录的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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