Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

access API from groovy #4495

drdt ·

Hi, I am making some maintenance jobs that manipulate other jobs. I see that many of the things I want to do (create/modify configurations, stop running builds, clean up workspaces) are documented for the REST API. But not in the API docs for groovy.

Can you give me some pointers how I can translate these, or access the same functionality from a groovy script?

  • replies 5
  • views 207
  • stars 0
robinshen ADMIN ·

You may refer to Java source of various resources and translate into groovy as necessary. They are located under package ""

drdt ·

So, I have had a lot of success with this; I now have configurations being created on the fly for on-demand builds, copied from a template (and even updated if the template changes). The next step is to have them automatically cleaned up when they are no longer needed.

We already have retention policies set so that builds are deleted after a week. Now I am trying to have the configuration delete itself automatically once the last build is removed. I have set this up as an "After Build Delete Script":

if (! configuration.getLatestBuild()) system.configurationManager.delete( configuration );

Now, when I delete the last build in the configuration, I get thrown into an error page saying "Database constraint violated: could not execute statement". However, I can navigate back to the dashboard from this page and see that the configuration was deleted as I wished.

I tried putting the above code in a try/catch block, but I think the error is coming from someplace else. Is there a better way to do this, or a way to block this message from coming up? This is the stack dump:

java.sql.SQLIntegrityConstraintViolationException: (conn=249) Cannot add or update a child row: a foreign key constraint fails (`quickbuild_2022_07`.`QB_AUDIT`, CONSTRAINT `FK_AUDIT_CONF` FOREIGN KEY (`QB_CONFIGURATION_ID`) REFERENCES `QB_CONFIGURATION` (`QB_ID`))
robinshen ADMIN ·

I guess you are testing this from UI. When delete build from UI, QB will log the action, and since configuration is deleted, the audit action fails as it can not find the configuration key to audit build deletion event. However if your builds are deleted from build cleanup strategy, it will not be audited, and will not encounter such error. ·

You should not let a configuration delete itself.
I will give you an example. You cannot kill yourself by strangling yourself. You will suffocate and faint before you really die.

Create a cleaning configuration and schedule it to run daily.

drdt ·

Thanks for the help, it is working like a dream in my nightly maintenance; I was even able to add an audit entry to the parent so people know where it went.

I understand what Duc is saying, but I am using a guillotine rather than strangulation, which works quickly enough the patient doesn't pass out, so the only problem I encounter is if I stand too close when it is activated. Since only privileged users can delete builds manually, I am documenting the edge case and everyone is happy.