如何使用Jenkins Pipeline Groovy脚本检索当前工作区?

N.R*_*ddy 23 groovy jenkins jenkins-pipeline

我试图使用Groovy管道脚本获取我的Jenkins构建的当前工作区:

node('master') {
    // PULL IN ENVIRONMENT VARIABLES
    // Jenkins makes these variables available for each job it runs
    def buildNumber = env.BUILD_NUMBER
    def workspace = env.WORKSPACE
    def buildUrl = env.BUILD_URL

    // PRINT ENVIRONMENT TO JOB
    echo "workspace directory is ${workspace}"
    echo "build URL is ${env.BUILD_URL}"
}
Run Code Online (Sandbox Code Playgroud)

它返回:

[Pipeline] Allocate node : Start
Running on master in /Users/Shared/Jenkins/Home/jobs/test/workspace
[Pipeline] node {
[Pipeline] echo
workspace directory is null
[Pipeline] echo
build URL is http://localhost:8080/job/test/5/
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)

Lit*_*lip 23

对我来说,只是${workspace}在没有初始化变量'workspace'的情况下工作.

  • 我也是,但只有大写的`$ {WORKSPACE}`有效,`$ {workspace}`不行 (14认同)

Krz*_*soń 6

目前还没有包含变量,所以你必须使用shell-out-read-file方法:

sh 'pwd > workspace'
workspace = readFile('workspace').trim()
Run Code Online (Sandbox Code Playgroud)

或者(如果在主节点上运行):

workspace = pwd()
Run Code Online (Sandbox Code Playgroud)


chr*_*s_f 5

我认为您还可以在特定节点上执行 pwd() 函数:

node {
    def PWD = pwd();
    ...
}
Run Code Online (Sandbox Code Playgroud)


Ism*_*ail 5

bat对于在工作中使用并需要访问工作区的任何人的快速说明:

这是行不通的

$WORKSPACE 这里提到的https://issues.jenkins-ci.org/browse/JENKINS-33511仅适用于 PowerShell。所以你的代码应该可以powershell执行

 stage('Verifying Workspace') {
  powershell label: '', script: 'dir $WORKSPACE'
}
Run Code Online (Sandbox Code Playgroud)