Sanhaji Omar
Sanhaji Omar

Reputation: 332

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:

Huuuge THANKS

Upvotes: 0

Views: 833

Answers (2)

Sanhaji Omar
Sanhaji Omar

Reputation: 332

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}

Upvotes: 0

K. B.
K. B.

Reputation: 3690

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.

Upvotes: 1

Related Questions