Kedar Kulkarni
Kedar Kulkarni

Reputation: 95

How to upload file to jenkins and use it for build?

I am very new to Jenkins, and I have been trying to use curl to build my job. Along with this curl I want to send a file to Jenkins which should be placed in particular directory of my workspace. I have been googling alot, but could not seem to find clear documentation that could lead me to solution. Please Guide. I looked at some other articles on Google and StackOverflow as well, but couldn't find what I am looking for.

curl -X POST JENKINS_URL/job/JOB_NAME/build \ --form file0=/home/abc.xml \ --form json='{"parameter": [{"name":"/workspace", "file":"file0"}]}'

I tried this as well, didn't seem to work.

Upvotes: 5

Views: 16840

Answers (2)

Jayan
Jayan

Reputation: 18459

From Jenkins file parameter help

Specifies the location, relative in the workspace, where the uploaded file will be placed (for example, like "jaxb-ri/data.zip")

The uploaded file location is relative to workspace. You need to copy/move it in a script. The name portion is job's file argument's name. It is not name of your file

Here is from script. The job has a file parameter named RECORDS_LIST.

curl -X POST  http://localhost:8080/job/builder/build \
             --form attachedfile=@c:/1.txt \
             --form json='{"parameter": [{"name":"RECORDS_LIST", "file":"attachedfile"}]}'

Yours could be

curl -X POST JENKINS_URL/job/JOB_NAME/build \ 
  --form file0=/home/abc.xml \ 
  --form json='{"parameter": [{"name":"YOUR_JOBS_FILE_ARGUMENT_NAME", "file":"file0"}]}'

Please note that jenkins command line api can handle file upload

java -jar jenkins-cli.jar -s http://localhost:8080/ build builder \
      -p YOUR_JOBS_FILE_ARGUMENT_NAME=/home/abc.xml

[edit after seeing OP's own answer]

Alert

The filename path part in curl command has '@' (note that @ before path) . The jenkins cli does not need it. This error, when happens, is difficult to track.

Upvotes: 4

Kedar Kulkarni
Kedar Kulkarni

Reputation: 95

Everything Jayan said is correct. I am just making it more simple, for beginners like me. Remember: @ sign is very important here in our curl url. Also, YOUR_JOBS_FILE_ARGUMENT_NAME is path where you want to store the file, along with name and extension of file. If you put abcd.xml in file location of your file parameter it will save abcd.xml in root of your workspace, if you want to put it in a folder xyz, you should write file location to be xyz/abcd.xml in your Filelocation of file parameter when you configure your job on jenkins and also make sure to use same in curl. So if you want to put abc.xml to xyz/abcd.xml on jenkins, please use command as :

curl -X POST JENKINS_URL/job/JOB_NAME/build \ 
  --form file0=@/home/abc.xml \ 
  --form json='{"parameter": [{"name":"xyz/abcd.xml", "file":"file0"}]}'

This is very basic stuff, but that is where I stumbled upon.

Upvotes: 0

Related Questions