Russell Gallop
Russell Gallop

Reputation: 1699

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?

Upvotes: 16

Views: 27549

Answers (4)

zett42
zett42

Reputation: 27766

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 (e.g., 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.

Upvotes: 0

Russell Gallop
Russell Gallop

Reputation: 1699

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

Upvotes: 7

pmgarvey
pmgarvey

Reputation: 741

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.

Upvotes: 14

Sampo
Sampo

Reputation: 5048

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

Upvotes: 17

Related Questions