Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

curl

jenkins

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.

like image 885
Kedar Kulkarni Avatar asked Jul 06 '16 01:07

Kedar Kulkarni


1 Answers

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.

like image 161
Jayan Avatar answered Nov 01 '22 07:11

Jayan