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

戏子无情 提交于 2019-12-24 00:38:06

问题


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!


回答1:


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 to 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.




回答2:


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.



来源:https://stackoverflow.com/questions/33784488/how-can-i-delete-a-job-using-job-dsl-pluginscript-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!