Jenkins可扩展选择,根据用户角色提供用户特定的项目 [英] Jenkins Extensible Choice with user specific items based on users Roles

查看:229
本文介绍了Jenkins可扩展选择,根据用户角色提供用户特定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我想在Jenkins参数化的版本中更改选择参数的内容.

I have a situation where I would like to alter the contents of a choice parameter in a Jenkins parametrised build.

就我而言,我想要一个用于部署应用程序"Deploy My App"的项目.在构建该项目时,将为用户提供一个选择参数.我想根据用户角色更改此列表的内容.即,具有"dev_deploy"角色的人将能够看到开发环境,具有"test_deploy"角色的人将能够看到测试环境等.

In my case I would like one project for deploying the application 'Deploy My App'. When building this project the user is presented with a choice parameter. I would like to alter the contents of this list depending on a user role. i.e. someone with the 'dev_deploy' role will be able to see the dev environments, someone with the 'test_deploy' role will be able to see the test environments etc.

我目前正在使用可扩展选择参数插件和基于角色的授权策略插件.

I am currently using the Extensible Choice Parameter plugin and the Role-based Authorization Strategy plugin.

我知道我可以编写一些棘手的脚本来生成供选择的列表项.

I know that I can write some groovey script to generate the list items for the choice.

def result = ["-------"]

def roles=??????

if(roles.get('dev_deploy') {
    //Add dev environments
    result.add('dev1')
    ....
}
if(roles.get('test_deploy') {
    //Add test environments
    result.add('test1')
    ....
}

return result

我只是不知道该由谁来担任用户角色?

I just can't figure out who to get hold of the users roles?

有人知道我该怎么做,或者对这个问题有不同的解决方案?

Anyone know how I might do this, or have different solution to the problem?

非常感谢

推荐答案

好,在进行了几次搜索之后,我发现了源代码(

OK, after a few more searches I came across the source (https://github.com/jenkinsci/role-strategy-plugin/tree/master/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy)

在进一步阅读和玩耍之后,我想到了这个……

After further reading and a bit of playing around I came up with this...

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

def result = ["-- Please Select --"]
def authStrategy = jenkins.model.Jenkins.instance.getAuthorizationStrategy()

if(authStrategy instanceof RoleBasedAuthorizationStrategy){
    def currentUser = jenkins.model.Jenkins.instance.getAuthentication().getName();
    def roleMap= authStrategy.roleMaps.get("globalRoles")

    def sids= roleMap.getSidsForRole("Manage_Dev")
    if(sids != null && sids.contains(currentUser)) {
        result.add("dev1")
        ...
    }

    sids= roleMap.getSidsForRole("Manage_Test")
    if(sids != null && sids.contains(currentUser)) {
        result.add("tst1")
        ...
    }
    ...
}

return result

哪个为我工作.当您知道如何时,就很容易!

Which works for me. Easy when you know how!

这篇关于Jenkins可扩展选择,根据用户角色提供用户特定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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