使用REST检查jenkins中的文件夹中是否存在作业 [英] Checking job exists within a folder in jenkins using REST

查看:135
本文介绍了使用REST检查jenkins中的文件夹中是否存在作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有文件夹,我可以检查是否存在作业:

I can check that a job exists if there is no folder:

http://myJenkinsInstance:8080/checkJobName?value = TestJob101

我无法解决的是当我使用文件夹插件时,如何检查文件夹中的作业.

What I am can't to work out is how do I check for a job that is within a folder please when I am using the folders plugin.

例如 http://myJenkinsInstance:8080/job/MyFolder/job/TestJob101/

http://myJenkinsInstance:8080/checkJobName?value = job/MyFolder/job/TestJob101/

返回:

‘/’ is an unsafe character

推荐答案

要访问Jenkins REST API,您可以在许多URL中添加\api\json后缀,这将导致Jenkins返回JSON段(如果存在,则返回404) ),您可以消费.在这种情况下,诸如 json2csharp 之类的工具非常宝贵.

To access the Jenkins REST API you can add \api\json suffix to many URLs which will cause Jenkins to return a JSON segment (if it exists, otherwise a 404) that you can consume. Tools like json2csharp are invaluable in this scenario.

消费方式

1.脚本

`curl -XGET 'http://myJenkinsInstance:8080/job/MyFolder/job/TestJob101/api/json' --user user.name:YourAPIToken`

这将返回200或404,具体取决于作业是否已经存在.

This returns either 200 or 404 depending on whether the job already exists.

2.编程语言(如C#)

转到您的文件夹URL并添加api\json,如下所示:

Go to your folder URL and append api\json like so:

http://myJenkinsInstance:8080/job/MyFolder/job/TestJob101/api/json

如果该文件夹存在,您将在浏览器中看到一些json,否则将得到404.复制所有json并将其粘贴到

If that folder exists you will see in the browser some json, otherwise you will get a 404. Copy all of the json and paste it into json2csharp and generate the C#.

您应该获得以下C#:

public class Action
{
    public string _class { get; set; }
}

public class Job
{
    public string _class { get; set; }
    public string name { get; set; }
    public string url { get; set; }
    public string color { get; set; }
}

public class PrimaryView
{
    public string _class { get; set; }
    public string name { get; set; }
    public string url { get; set; }
}

public class View
{
    public string _class { get; set; }
    public string name { get; set; }
    public string url { get; set; }
}

public class RootObject
{
    public string _class { get; set; }
    public List<Action> actions { get; set; }
    public string description { get; set; }
    public string displayName { get; set; }
    public object displayNameOrNull { get; set; }
    public string fullDisplayName { get; set; }
    public string fullName { get; set; }
    public string name { get; set; }
    public string url { get; set; }
    public List<object> healthReport { get; set; }
    public List<Job> jobs { get; set; }
    public PrimaryView primaryView { get; set; }
    public List<View> views { get; set; }
}

您应该将RootObject类重命名为上下文中更有意义的名称,以使您的代码更具可读性,例如FolderCheck

You should rename RootObject class to something more meaningful in your context to make your code more readable, like FolderCheck

一旦有了此代码,就可以对同一URL进行HTTP GET调用. (注意:以编程方式替换文件夹名称将使其可用于您要使用的任何文件夹.)并使用

Once you have this code you can make an HTTP GET call to the same URL. (Note: substituting the folder name in programmatically will make it work for any folder you want to work with.) and deserialise it like so using the Json.NET NuGet package:

var json = Adapter.Get(url, username, apiKey);
JsonConvert.DeserializeObject<FolderCheck>(json);

如果json变量包含有效的json,则说明该文件夹存在.

If the json variable contains valid json, then you know the folder exists.

在Jenkins LTS 2.60.3和2.90上进行了测试.

Tested on Jenkins LTS 2.60.3 and 2.90.

这篇关于使用REST检查jenkins中的文件夹中是否存在作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆