简体   繁体   中英

How to upload file from command line as file parameter in jenkins

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?

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

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 .

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

Unfortunately Russels answer for using the CLI doesn't work anymore since Jenkins 2.165, because the “Remoting” operation mode of the Jenkins command-line interface has been removed.

From the Jenkins blog :

Command options or arguments which took either a local file or = for standard input/output (eg, install-plugin, build -p, support) now only accept the latter.

This means the command-line must be changed like this:

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

By passing an empty string for the file parameter "main.c", we indicate to the CLI that the file should be read from standard input. We are using shell redirection operator "<" to redirect the file "hello.c" to standard input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM