How to Inherit permissions of a Job Template in Template instances?

Article ID:235795207
1 minute readKnowledge base


  • I want that jobs created from a Job template inherit the RBAC groups and role filters assigned to that Template.



In the groovy transformer, you can access the template model (the Job Template) of the template instance (The Job created from the Job Template) via the variable instance.

From that object you can retrieve the property that encapsulates RBAC groups and role filters - that is com.cloudbees.hudson.plugins.modeling.integrations.rbac.ModelProxyGroupContainer. You then need to serialize the groups and role filters inside a nectar.plugins.rbac.groups.JobProxyGroupContainer.

Following is a minimal example:

<?xml version='1.0' encoding='UTF-8'?>
def modelGroups = []
def modelRoleFilters = []
if (instance != null && instance.model != null && instance.model.getProperties() != null) {
    def groupContainers = instance.model.getProperties().get(com.cloudbees.hudson.plugins.modeling.integrations.rbac.ModelProxyGroupContainer.class)
    if(groupContainers != null) {
        modelGroups = groupContainers.groups
        modelRoleFilters = groupContainers.roleFilters
    <% if (modelGroups != "") { %>
            <% modelGroups.each { group -> %>
            <% } %>
            <% modelRoleFilters.each { roleFilter -> %>
            <% } %>
    <% } %>
<scm class="hudson.scm.NullSCM"/>
Be aware that this considered unsafe and required an administrator to allow several methods in the Manage Jenkins  In-Process Script Approval screen. See Script Security Plugin for more details.
Be aware that any changes to the permissions of your Job Template will not be automatically reflected to the template instances. For that you need to force the re-evaluation of the instance(s) transformer by re-saving the Job Template.