Rafael Ferreira
Rafael Ferreira

Reputation: 338

How to get the file path from the workspace in Jenkins

My jenkins is located at myjenkins:8080/

After I run my Job it generates one file and I would like to get the complete path of it to give it to the user.

Example:

myjenkins:8080/job/FUTURE/job/GetFullPath/2/execution/node/3/ws/

I want to give this path to the user and the user would see the file generated there.

    pipeline {
        agent { label env.Machine}
        stages {
            stage('PREPARE'){
                steps{
                    script{
                        env.custom_stage_name = "PREPARE"
                        bat '%FOLDER%\\CreateFile.bat'
                    }
                }
            }
            stage('BUILD'){
                steps{
                    ws("c:\\jenkins\\workspace\\Test") {
                        bat 'xcopy ' + '%FOLDER%\\File.txt ' + "c:\\jenkins\\workspace\\Test /I /s /e /h /y"
                    }
                }
            }
        }
    }

Upvotes: 0

Views: 9011

Answers (1)

agentsmith
agentsmith

Reputation: 1326

Based on your title it's not clear to me if you just need a way to get the current working directory or you want to expose the directory so you can access it using a browser.

If you meant the second case: First at all, I think this is not possible without using a workaround. And there are some problems you need to work around!

First of all let me show you how to get the path to the workspace (if it is no obvious)
You can get the Path to your workspace by using the variable ${env.WORKSPACE}
Example:

pipeline {
   agent any
   stages {
       stage('Hello') {
            steps {
                echo "${env.WORKSPACE}"
            }
        }
    }
}

Concatenate the file: def my_file = "${env.WORKSPACE}/my.file"

To your actual problem

  1. Unless your filename differs every run, it will get overwritten. There is no guarantee that the file in your workspace folder is preserved. To keep it you should tell Jenkins to archive (see point 2) your artifacts.

  2. I can't image any good reason to expose your workspace like you want to do. So, let me give you some alternative first examples:

    • You want to give or sent Jenkins's log-files to someone: You can do this using the email-ext plugin which allows attaching the log. This is a neat way to get information about the build state and sending out the log to people.
    • You want to save your build artifacts - and I think this is what you actually want. In this case you should archive them. See jenkins-docs on how to do this.
    • If the "archive artifacts" also do not fit your needs: You can use a separate Freestyle project. A Freestyle projects allows viewing and downloading (as a zip-file) its workspace content. But: This is by far not the best solution!
      Basically you copy your files from your pipeline project to this freestyle project. You can access it using the freestyle project's URL.
  3. If you really want to expose the workspace using an URL

    This involves a separate webserver, since I can't think of a way on how to tell Jenkins to expose it's working directories as a webservice.
    If you just want to gain access to your workspace folder, you can expose it using a separate webserver. The ideas is to run a very simple web-server to serve the current workspace directory.
    Here is one example on how to deploy exactly this using python.

Upvotes: 2

Related Questions