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

jenkins file parameter not uploaded
jenkins required parameter
jenkins string parameter
jenkins integer parameter
jenkins boolean parameter
jenkins build job parameters
jenkins parameterized build python
how to pass parameters 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

How to use file parameter in jenkins, The uploaded file will not retain the same name as it has on your local computer. It will be named after the File location argument specified in  File parameter. File parameter allows a build to accept a file, to be submitted by the user when scheduling a new build. The file will be placed inside the workspace at the known location after the check-out/update is done, so that your build scripts can use this file. Define Custom Parameter Types. A plugin can define custom parameter types.

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.

How to Upload a File in Jenkins | Using File Parameter, Learn how to upload a file in Jenkins using file parameter. At times you might be required to add a field in your build with the help of which your  JENKINS-4296; File upload option from command line results in NPE. from the command line. How do I pass the source file parameter location command line

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

Uploading a text file to the repository using Jenkins, Code Sample - Jenkins: Upload text file to repository software project; Add a new "Execute shell" build step with the following command: of the build; Add a new "Upload files to MobileCloud" with the following parameters: Jenkins has a built-in command line interface that allows users and administrators to access Jenkins from a script or shell environment. This can be convenient for scripting of routine tasks, bulk updates, troubleshooting, and more. The command line interface can be accessed over SSH or with the Jenkins CLI client, a .jar file distributed with

Parameterized Build - Jenkins, File parameter allows a build to accept a file, to be submitted by the user when To use with wget, quote the URL on the command line too. i try to upload a file trough the option "file-parameter" into the workspace. The stash step allows capturing files matching an inclusion pattern (**/target/*.jar) for reuse within the same Pipeline. Once the Pipeline has completed its execution, stashed files are deleted from the Jenkins master. 2: The parameter in agent/node allows for any valid Jenkins label expression.

Remote access API - Jenkins, Another example - sending a "File Parameter": ? Command Line Interface with a lot of options provided in the libraries. Creating, listing views  Daniel Beck added a comment - 2017-01-22 23:42 Long standing issue. /build is just wrong to use programmatically, so there needs to be a way to upload files with /buildWithParameters .

[JENKINS-4296] File upload option from command line results in , How do I pass the source file parameter location into the above URL from the command line?. Please provide this support in the hudson cli. Before that, we need to install and configure Jenkins to talk to S3 and Github. We will see each of these in detail here. Goal: Configure Jenkins plugins to talk to S3 and Github and build a simple pipeline which will upload a file checked into Github to S3. Each time you make a change to the file, the pipeline will be automatically triggered.

Comments
  • Not sure whether you can do this from the HTTP interface, but it may be possible from the CLI (see ci.jenkins-ci.org/cli). There you can normally just provide the path to the file on the command line and it's uploaded appropriately.
  • Do you have a special reason why you need to upload your source file to the Jenkins build, rather than having the build retrieve it from source control ? The latter method is a fundamental capability of Jenkins, so it's a lot easier to set up.
  • Christopher: I'll try that. gareth_bowles: that's not really what I want to do but I thought it might explain the question. I'm actually trying to upload a mercurial forest snapshot which I don't believe is handled by Jenkins. Maybe that should be another question...
  • @Christopher That works really nicely. If you post it as an answer I'd vote for it.
  • Note: At least when running Jenkins 2.46.2, posting a file (multipart form data) to /build instead of /buildWithParameters causes the "Location" response header to not point to a queue item (e.g. [JENKINS]/queue/item/55/). Instead "Location" will just point to the job base url (e.g. [JENKINS]/job/[JOB_NAME]/). This makes it hard to know which build number the triggered build corresponds to!
  • Strille's comment is still valid as of version 2.73. So, when you need to pass a file, the options are a) use the /build URL (but then there's no option to retrieve the build number), or b) use the /buildWithParameters URL, which breaks the file upload, but which gives a build number. Is there really no way to get the build number for builds with file parameters...?
  • Thanks, I went this route but failed to see the @ character before the file name... works great now.
  • Does anyone know how to do this with libcurl?
  • But do you know what exactly path to file should be ? file0=@/some/folder/path/template.zip When I'm trying to pass the file from my local machine, it doesn't work. curl -X POST https://ctech.jjc-devops.com/job/ctech/job/pipeline/job/dev/job/storage/buildWithParameters?token=ctech --user username:userpass --form file0=@D:/Repository/test.yml --form json='{"parameter": [{"name":"pipeline.yaml", "file":"file0"}, {"name":"source_env.json", "file":""}, {"name":"target_env.json", "file":""}]}'