Jenkins 2-如何从Jenkins工作流中获取用户角色(角色策略插件)-管道插件 [英] Jenkins 2 - How to get user role (Role Strategy Plugin) from Jenkins Workflow - Pipeline Plugin

查看:286
本文介绍了Jenkins 2-如何从Jenkins工作流中获取用户角色(角色策略插件)-管道插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问在Jenkins 2管道(工作流)的角色策略插件"中配置的用户角色/角色:

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
}

推荐答案

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 2-如何从Jenkins工作流中获取用户角色(角色策略插件)-管道插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆