The Traveling Coder
The Traveling Coder

Reputation: 311

Get Jenkins Workspace files

I am currently working on creating a simple Jenkins Plugin. I am trying to find a way to access the files in the workspace that Jenkins has.

For instance, if I have Jenkins pull my repo, it downloads all the files to a workspace folder on the computer. How would I find this exact location?

I am trying to zip the files and be able to send them out to an API endpoint. Thank you all

Upvotes: 0

Views: 2181

Answers (2)

apr_1985
apr_1985

Reputation: 1962

There is an env var called WORKSPACE that contains the location. e.g. for a job named banana in the folder sandbox

stages {
    stage('Hello') {
        steps {
            echo "$WORKSPACE"
            sh "ls $WORKSPACE"
            sh "touch banana.txt"
            sh "ls $WORKSPACE"
        }
    }
}

Gives the output

[Pipeline] { (Hello)
[Pipeline] echo
/tmp/workspace/sandbox/banana
[Pipeline] sh
+ ls /tmp/workspace/sandbox/banana
[Pipeline] sh
+ touch banana.txt
[Pipeline] sh
+ ls /tmp/workspace/sandbox/banana
banana.txt

Upvotes: 0

Ian W
Ian W

Reputation: 4767

If developing a plugin, refer to the javadoc.

Before proceeding, ask if "one or more plugins which cover your needs" ? eg: HTTP Post (plus, lots of ways to "zip")

Nevertheless, hudson.FilePath probably meets your use case.

As Jesse Glick explains:

Just use FilePath newFile = build.getWorkspace().child(fileOnDiskPath); unconditionally. You would typically pass a relative pathname; see: https://javadoc.jenkins.io/hudson/FilePath.html#child-java.lang.String-

And if you want to do the compressing : FilePath.TarCompression

Reference a workspace in a pipeline? org.jenkinsci.plugins.workflow.actions.WorkspaceAction

Upvotes: 1

Related Questions