Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-57252: Optional support for shelving projects instead of deleting them #1174

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

cpfeiffer
Copy link

It would be nice to let the seed job archive unused projects instead of deleting them right away.

@cpfeiffer
Copy link
Author

Note: I tried to test the successful shelving in the ExecuteDslScriptsSpec test, but the shelving is done asynchronously via jenkins's queue. If you have a suggestion how to easily wait for it to finish, please share.

@cpfeiffer
Copy link
Author

The test failure was unrelated to this change, so I just rebased on current master and force-pushed to trigger a new build.

private void shelve(Item project) {
Jenkins jenkins = Jenkins.get();
if (! (project instanceof BuildableItem)) {
LOGGER.log(Level.WARNING, "Unable to shelve " + project + " since it is not a BuildableItem");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be logged to the build output. No one will notice the problem when the info is hidden in the Jenkins log.

Would it also make sense to set the build result to unstable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
BuildableItem item = (BuildableItem) project;
if (jenkins.getPlugin(SHELVE_PLUGIN_ID) == null) {
LOGGER.log(Level.WARNING, "Unable to shelve project " + item + " since the " + SHELVE_PLUGIN_ID + " plugin is not installed.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also done.

LOGGER.log(Level.WARNING, "Unable to shelve project " + item + " since the " + SHELVE_PLUGIN_ID + " plugin is not installed.");
return;
}
jenkins.getQueue().schedule(new ShelveProjectTask(item), 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, when shelving a lot of projects this will flood the build queue. Unfortunately there seems to be no bulk API in the shelve plugin. Can you open a feature request for the shelve plugin?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


then:
jenkinsRule.jenkins.getItemByFullName('different-job') instanceof FreeStyleProject
// jenkinsRule.jenkins.getItemByFullName('test-job') == null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a proper test. Maybe you can use Spock's PollingConditions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since deletion of parent folders doesn't work as long as the folder contains jobs, I had to change this to wait for the shelve task to finish and then delete any parent folders. That way I could actually test that the shelved project is gone afterwards.

I'm not 100% sure if the waiting for the shelve task completion will work in all scenarios.
ExecuteDslScripts is a build step, adding the ShelveProjectTask to the build queue and waiting for it to finish. AFAIU, the queue will always run Queue.FlyweightTask instances regardless of the number of executors configured. But I'm not a Jenkins developer, so if you have any input on that, please tell.

I don't know how to test that folders are getting deleted since "test-job" and "different-job" both have the same parent folder. One of them always exists, so the folder is never getting deleted.

LOGGER.log(Level.WARNING, "Unable to shelve project " + item + " since the " + SHELVE_PLUGIN_ID + " plugin is not installed.");
return;
}
jenkins.getQueue().schedule(new ShelveProjectTask(item), 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShelveProjectTask does not seem to check if the user has permission to delete a project. That is done upfront in ShelveProjectAction. So the permission must be checked here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

… separately

Folders fail get removed when they still contain jobs, so we can only remove them
when the jobs are gone. This means that we have to wait for the shelve task to finish
before attempting to remove the folders.
@daspilker
Copy link
Member

@cpfeiffer can you ask the maintainer of the shelve plugin about the status of JENKINS-27734 and jenkinsci/shelve-project-plugin#14? That seems to be the better approach for handling folders and should be implemented first. Than Job DSL does not need any special handling for folders.

@CorevistCI
Copy link

Build finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants