Search code examples
jenkinsjenkins-job-dsl

How to set the owner of a Jenkins folder when creating it with a seeder job?


I've written a seeder job using the Job DSL Plugin to create folders with a predefined collection of jobs inside. I also use the Ownership Plugin to control which users get access to each folder.

Now I need the user who fired the seeder job to be assigned as the folder's primary owner.

I tried setting "Assign job creators as owners" configuration option, but the result I get is that the created folder's owner is SYSTEM.

enter image description here

Is there a way to set the primary owner of the folder programmatically?


Solution

  • In the end I was able to do this by getting the name of the current use as the Cause of the currentBuild variable and then assign it to the folder by using the configure step like so:

    user = currentBuild.getCause(Cause.UserIdCause)
    folder("MyFolder") {
        configure { folder ->
            folder / 'properties' / 'org.jenkinsci.plugins.ownership.model.folders.FolderOwnershipProperty' / 'ownership' {     
                primaryOwnerId(user.getUserId())
                ownershipEnabled('true')
            }
        }
    }
    

    If anyone can provide a less verbose way to do this I'll mark that as the accepted answer.