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

Rus*_*lop 13 curl command-line-interface jenkins

我在Jenkins的命令行中使用字符串参数触发构建:

curl http://jenkins:8080/job/Build/buildWithParameters?PARAM=value&token=token
Run Code Online (Sandbox Code Playgroud)

我现在想要从命令行触发带有文件作为文件参数的构建.

例如,如果我的项目构建main.c,那么我希望能够触发构建并从命令行上传我的main.c.

这可能吗?

Sam*_*mpo 14

这在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"}]}'
Run Code Online (Sandbox Code Playgroud)

请注意,您需要使用URL/build而不是/ buildWithParameters

  • **注意:**至少在运行Jenkins 2.46.2时,将文件(多部分表单数据)发布到/ build而不是/ buildWithParameters会导致“ Location”响应标头不指向队列项目(例如[JENKINS] /队列/项目/ 55 /)。相反,“位置”将仅指向作业基本网址(例如[JENKINS] / job / [JOB_NAME] /)。这使得很难知道触发的版本对应哪个版本号! (2认同)

小智 9

如果需要发送字符串参数和文件参数,可以执行以下操作:

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
Run Code Online (Sandbox Code Playgroud)

我必须确保参数param1,param2并且fileParm存在于詹金斯工作My_Remote_Jenkins_Job.


Rus*_*lop 6

我使用的解决方案(基于Christophers建议使用jenkins-cli)是:

java -jar jenkins-cli.jar -s http://jenkins:8080 build Build -p main.c=hello.c
Run Code Online (Sandbox Code Playgroud)

使用main.c的File参数将您的本地hello.c作为main.c上载到Build作业的工作区