简体   繁体   English

Jenkins在groovy脚本中使用REST API创建文件夹

[英]Jenkins create Folder with REST API in groovy script

i am trying to create a Folder in Jenkins using a groovy script. 我正在尝试使用groovy脚本在Jenkins中创建一个文件夹。 For that i use an REST API call. 为此,我使用REST API调用。 My starting point is was basically a curl command: 我的出发点基本上是curl命令:

curl -XPOST "http://userName:YourApiToken@JenkinsURL:Port/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder" -H "Content-Type:application/x-www-form-urlencoded"

I got this from https://gist.github.com/marshyski/abaa1ccbcee5b15db92c and found later out that it also works if you put the user into the URL. 我是从https://gist.github.com/marshyski/abaa1ccbcee5b15db92c获得的 ,后来发现,如果您将用户放入URL,它也可以工作。 Using this line in the Windows cmd works perfectly and generates me a new Folder in my Jenkins. 在Windows cmd中使用此行可以完美工作,并在我的Jenkins中生成一个新文件夹。

Now i try to do the same thing but from a groovy script. 现在,我尝试从普通脚本中执行相同的操作。 For that i use the following code: 为此,我使用以下代码:

import groovyx.net.http.*

def post = new URL("http://userName:YourApiToken@JenkinsURL:Port/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder").openConnection();
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
    println(post.getInputStream().getText());
}

I got this basically from Groovy built-in REST/HTTP client? 我基本上是从Groovy内置的REST / HTTP客户端获得的? (i have heard apache Http is deprecated) (我听说过不推荐使用Apache Http)

Long story short i did not manage to create a folder with the given groovy script. 长话短说,我没有用给定的常规脚本创建文件夹。 I always get an 403 response (I run the script from windows cmd and inside jenkins with the script console). 我总是收到403响应 (我使用脚本控制台从Windows cmd和jenkins内部运行脚本)。

I also played around with Jenkins-crumb but that seems not necessary because the above cmd command already worked. 我还与Jenkins-crumb一起玩耍,但这似乎没有必要,因为上面的cmd命令已经起作用。

So the question is does anyone know what i am missing? 所以问题是有人知道我在想什么吗? why is the groovy script not working but the curl command is? 为什么groovy脚本不起作用,但是curl命令起作用? Or has anyone a different approach on how to generate a Folder in Jenkins from a groovy script? 还是有人用不同的方法从普通脚本生成Jenkins中的文件夹?

Edit: When i try to run the curl command in the groovy script with .execute() i get an "The system cannot find the file specified" Error. 编辑:当我尝试使用.execute()在groovy脚本中运行curl命令时,出现“系统找不到指定的文件”错误。

String command = "curl -XPOST "+ '"http://user.name:ApiToken@JenkinsURL:8080/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder"'+ ' -H "Content-Type:application/x-www-form-urlencoded"'
println(command)
command.execute()

curl command not present on windows platform - so you have 在Windows平台上不存在curl命令-因此您拥有

"The system cannot find the file specified". “该系统找不到指定的文件”。

403 - forbidden. 403-禁止。 something wrong with auth I guess. 我猜认证有问题。

better to move user&pass to header: 最好将用户和传递移动到标题:

post.setRequestProperty("Authorization", "Basic ${"username:password".bytes.encodeBase64().toString()}")

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

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