我为此感到震惊。我收到错误消息“无法在空对象上调用方法getSidsForRole()”。我有
当我使用下面的代码运行管道时,我得到了上面的错误。请帮我缺少什么?
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
答案 0 :(得分:0)
以下语法适用于插件版本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()