Jenkins 2 - 如何从 Jenkins Workflow - Pipeline 插件中获取用户角色(角色策略插件)

5

我想在Jenkins 2流水线(工作流)中访问Role Strategy插件中配置的用户角色/角色:

node {
      // Get the user Role
}
1个回答

4
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()
            }
        }
    }
}

此脚本已过时,因为插件现在已成为Jenkins核心的一部分。 - Madox
那么,你有什么建议? - cactuschibre
有没有新的解决方案? - Savke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接