无法在空对象上调用方法getSidsForRole

问题描述

我为此感到震惊。我收到错误消息“无法在空对象上调用方法getSidsForRole()”。我有

  1. 插件“基于角色的授权策略” 3.0版
  2. 已创建名为“ devops”和“ developers”的全局角色。已授予所需的访问权限。
  3. 将jenkins用户添加到上述角色。

当我使用下面的代码运行管道时,我得到了上面的错误。请帮我缺少什么?

stage('Authorize')
        {
            steps{
                script{
                    if (currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause) != null)
                    {
                        println("Job is triggered by upstream build job,so need to authorize again. Let it run.");
                    }
                    else
                    {
                        def authStrategy = Jenkins.instance.getAuthorizationStrategy()
                        if (authStrategy instanceof com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy)
                        {
                            def devops_users = authStrategy.roleMaps.globalRoles.getSidsForRole('devops')
                            def project_users = authStrategy.roleMaps.globalRoles.getSidsForRole('developers')
                            def user=currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()
                            def env=params.Environment

                            if(devops_users.contains(user) || (project_users.contains(user) && (env!='prod')))
                            {
                            println("Authorization successful! "+ user + " is authorised to run this pipeline");
                            }
                            else
                            {
                            println(user + " is not authorised to run this pipeline in "+ env +" environment");
                            currentBuild.result = "FAILED"
                            error("Authorization failed!!! "+ user +" has insufficient privileges...")
                            }
                        }
                   else {
                        throw new Exception("RBAC plugin not found. Install it before using from plugin manager or contact DevOps Team")
                        }
                    }
                }
            }
        }

即使下面在“脚本控制台”中运行的简单代码也返回null

import com.michelin.cio.hudson.plugins.rolestrategy.*

def authStrategy = Jenkins.instance.getAuthorizationStrategy()
def roleMap = authStrategy.roleMaps.get("globalRoles")
print authStrategy  // Print Correctly
print roleMap      // Prints Null

解决方法

以下语法适用于插件版本3.0

def role_maps = authStrategy.getRoleMap(com.synopsys.arc.jenkins.plugins.rolestrategy.RoleType.Global)
                            def devops_users = role_maps.getSidsForRole('devops')
                            def project_users = role_maps.getSidsForRole('developers')
                            def user=currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...