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!
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).
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.
To delete any job from Gui,Go to your job and on left side,click on delete project. Save this answer.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With