Jenkins 2 - How to get user role (Role Strategy Plugin) from Jenkins Workflow - Pipeline Plugin

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 06:58:37

问题


I would like to access the user role/roles, configured in Role Strategy Plugin in a Jenkins 2 pipeline (workflow):

node {
      // Get the user Role
}

回答1:


import jenkins.model.Jenkins
import com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy
import com.michelin.cio.hudson.plugins.rolestrategy.Role

node {
    stage('Get Role') {
        def user  = Jenkins.getInstance().getUser(
            Jenkins.getInstance().getItemByFullName(env.JOB_BASE_NAME, Job.class).getBuildByNumber(env.BUILD_ID as int).getCause(Cause.UserIdCause).getUserId()
        )
        def authorization = Jenkins.getInstance().getAuthorizationStrategy()
        //RoleBasedAuthorizationStrategy.{GLOBAL, PROJECT, SLAVE, MACRO_ROLE, MACRO_USER}
        def grantedRoles = authorization.getGrantedRoles(RoleBasedAuthorizationStrategy.GLOBAL)

        for (Role grantedRole : grantedRoles.entrySet()) {
            if (grantedRole.getValue().contains(user.getId())) {
                echo grantedRole.getKey().getName()
            }
        }
    }
}


来源:https://stackoverflow.com/questions/39725289/jenkins-2-how-to-get-user-role-role-strategy-plugin-from-jenkins-workflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!