How to upload file from command line as file param

2020-02-09 08:31发布

问题:

I am triggering builds with string parameters from the command line in Jenkins with:

curl http://jenkins:8080/job/Build/buildWithParameters?PARAM=value&token=token

I now want to trigger a build with a file as a file parameter from the command line.

For example if my project builds main.c then I would like to be able to trigger a build and upload my main.c from the command line.

Is this possible?

回答1:

This is described in the Jenkins Remote access API page:

curl http://jenkins/job/$JOB_NAME/build -F file0=@PATH_TO_FILE -F json='{"parameter": [{"name":"FILE_LOCATION_AS_SET_IN_JENKINS", "file":"file0"}]}'

Note that you need to use the URL /build instead of /buildWithParameters



回答2:

If you need to send both string parameters and a file parameter, you can do the following:

json='{"parameter": [{"name": "param1", "value": "value1"},
  {"name": "param2", "value": "value2"},
  {"name":"fileParam", "file":"file0"}]}'

url=http://jenkins/job/My_Remote_Jenkins_Job/build

curl -v $url -F file0=@/some/folder/path/template.zip -F json="$json" --user username:password

I had to make sure that the parameters param1, param2 and fileParm exist in the Jenkins job My_Remote_Jenkins_Job.



回答3:

The solution I have used (based on Christophers suggestion of using jenkins-cli) is:

java -jar jenkins-cli.jar -s http://jenkins:8080 build Build -p main.c=hello.c

Which with a File Parameter of main.c will upload your local hello.c to the the workspace of the Build job as main.c