简体   繁体   中英

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!

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

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