Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete a job using Job DSL plugin(script) in Jenkins?

I am very new to Jenkins and Job DSL plugin. After a little research, I found how to create a job using DSL and now I am trying to delete a job using DSL. I know to disable a job using this following code:

//create new job
//freeStyleJob("MyJob1", closure = null);

job("MyJob1"){
  disabled(true);
}

It is working perfectly fine. But, I couldn't find any method to delete another job in jenkins.

Please help!

Thanks!

like image 686
Srijani Ghosh Avatar asked Nov 18 '15 15:11

Srijani Ghosh


People also ask

How do I delete an existing job in Jenkins?

Click on View that you want to remove from. Click on "Edit View" in the left toolbar. Scroll down and uncheck your job (you will see the "Jobs" area in the "Job Filter" section).

What is DSL script in Jenkins?

A Job DSL script consists of API methods provided by the Job DSL plugin; you can use these API methods to configure different aspects of a job, such as its type (freestyle versus pipeline jobs), build triggers, build parameters, post-build actions, and so on.

Is project deletion possible in Jenkins?

To delete any job from Gui,Go to your job and on left side,click on delete project. Save this answer.

What is DSL in pipeline?

Job DSL gives you a way to create other jobs according to a Groovy script. So if you want a set of X related jobs (e.g. a pipeline), you'll create one Job DSL job, write the script, run that job, and then you'll have those X jobs, plus the one to create those jobs.


2 Answers

To delete a job, you have to set the "Action for removed jobs" option to "Delete" in the "Process Job DSLs" build step configuration. Then remove the job from your script and run the seed job.

like image 72
daspilker Avatar answered Sep 28 '22 19:09

daspilker


Each instance of the Job Dsl plugin tracks what jobs (and views) it creates. When it is run again, you can configure what it does to jobs (and views) that were present the previous time this instance was run, but are not present this time.

Let's a assume you have two files you use to create jobs.

seed_jobdsl.groovy:

job('seed_all') {
  steps {
    dsl {
      external('*_jobdsl.groovy')  
      // default behavior
      // removeAction('IGNORE')      
    }
  }
}

test_jobdsl.groovy:

job('test_stuff') {
  steps {
    shell('echo "I live!")
  }
}

This will leave jobs created by seed_all unchanged even if they are not present in the list of job created the next time seed is run.

To get jobs to be deleted, change your seed job code:

seed_jobdsl.groovy:

job('seed_all') {
  steps {
    dsl {
      external('*_jobdsl.groovy')  
      removeAction('DELETE')      
    }
  }
}

Now, run seed_all job to apply your change (seed_all overwrites its own configuration when run). Then make the following change:

test_jobdsl.groovy:

job('test_other') {
  steps {
    shell('echo "The job is dead, long live the new job!"')
  }
}

Run seed_all again. You notice test_stuff will be deleted and test_other will be created. If you remove test_jobdsl.groovy and then run seed_all, test_other will be deleted.

like image 43
BitwiseMan Avatar answered Sep 28 '22 17:09

BitwiseMan