1
votes

I have a Windows machine as Jenkins slave. Using groovy script I need to achieve the following:

1) Create a folder on the Jenkins slave

2) Copy a file from local machine to the Jenkins slave

3) Modify the PATH variable on the Jenkins slave

1
Can you explain why you want to modify the PATH variable? If you are intending to copy an executable onto the Jenkins agent, then you'll know the path to the executable to invoke it, or it can be set on the agent properties configuration - see answer below.elworthy

1 Answers

3
votes

In a freestyle project you can add a Execute Groovy Script build step and use this syntax to create a folder or a nested folder structure using:

new File("new").mkdir()
new File("dir/sub").mkdirs()

These folders will be created within the current workspace folder on the Jenkins agent. To create elsewhere you need to give an explicit path:

new File("C:/AFT/new").mkdir()

To copy a file on the agent to itself:

new File('copiedInWorkSpace.txt') << new File('C:/AFT/source.txt').text

To copy a file from a network share on to the agent:

new File('copiedInWorkSpace.txt') << new File('//share/path/source.txt').text

See my comments on your question about PATH, but this can be configured on an agent level - under Node Properties > Environment variables. The help section shows:

'Jenkins also supports a special syntax, BASE+EXTRA, which allows you to add multiple key-value pairs here, which will be prepended to an existing environment variable.

For example, if you have a machine which has PATH=/usr/bin, you could add to the standard path by defining an environment variable here, with the name PATH+LOCAL_BIN and value /usr/local/bin. This would result in PATH=/usr/local/bin:/usr/bin being exported during builds executed on this machine. PATH+LOCAL_BIN=/usr/local/bin will also be exported. Multiple entries are prepended to the "base" variable according to the alphabetical order of the "extra" part of the name.'