简体   繁体   中英

removing jenkins job as using jenkins job dsl

I am using jenkins job dsl to create pipelineJob, I do not know how to delete those jobs through the same plugin.

I wondered around in the code base, and I think it is not doable.

I thought of using the rest api to make a call to the api to delete a job, can anyone give me any lead on how to do that in groovy or extending a Java class.

Basically it would be:

  • read the jobs name in the job.groovy file
  • make the call to the rest api (jenkins_base_url+/job+/doDelete)

Huuuge THANKS

I haven't used the scripts from here and here , but they look promising.

import jenkins.model.*

def matchedJobs = Jenkins.instance.items.findAll { job ->
    job.name =~ /my_regex_here/
}

matchedJobs.each { job ->
    println job.name
    //job.delete();
}

If your jobs do not share a common pattern in the name and you cannot use Regex, here and here are some resources for reading files with groovy.

Finally, to meet my needs I had to make an api call.

It looks like this:

RestApiJobManagement jm = new RestApiJobManagement(baseUrl)
HttpResponseDecorator resp = jm.restClient.get(path: 'crumbIssuer/api/xml')   
    if (resp.status == 200) {                                                     
        restClient.headers[resp.data.crumbRequestField] = resp.data.crumb         
    }                                                                             
resp = jm.restClient.post(
    path: '/job/${job.jobName}/doDelete',
    requestContentType: 'charset=UTF-8'
)
println "status ${resp.status}"

The only issue is that I am unable to read through a jenkinsjobdsl.groovy file and get all job names to fill ${job.jobName}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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