简体   繁体   English

如何从命令行上传文件作为 jenkins 中的文件参数

[英]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:我正在 Jenkins 的命令行中使用字符串参数触发构建:

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.例如,如果我的项目构建 main.c,那么我希望能够触发构建并从命令行上传我的 main.c。

Is this possible?这可能吗?

This is described in the Jenkins Remote access API page:这在Jenkins 远程访问API 页面中有描述:

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请注意,您需要使用 URL /build 而不是 /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 .我必须确保参数param1param2fileParm存在于 Jenkins 作业My_Remote_Jenkins_Job

The solution I have used (based on Christophers suggestion of using jenkins-cli ) is:我使用的解决方案(基于 Christophers 使用jenkins-cli 的建议)是:

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使用 main.c 的文件参数会将您的本地 hello.c 作为 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.不幸的是,自 Jenkins 2.165 以来, Russels使用 CLI 的回答不再有效,因为 Jenkins 命令行界面的“远程”操作模式已被删除。

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.使用本地文件或 = 用于标准输入/输出(例如 install-plugin、build -p、support)的命令选项或参数现在只接受后者。

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.通过为文件参数“main.c”传递一个空字符串,我们向 CLI 指示应该从标准输入读取文件。 We are using shell redirection operator "<" to redirect the file "hello.c" to standard input.我们使用 shell 重定向运算符“<”将文件“hello.c”重定向到标准输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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