Jenkins 可扩展选择,具有基于用户角色的用户特定项目

标签 jenkins

我遇到了一种情况,我想更改 Jenkins 参数化构建中选择参数的内容。

就我而言,我想要一个用于部署应用程序“部署我的应用程序”的项目。当构建这个项目时,用户会看到一个选择参数。我想根据用户角色更改此列表的内容。即具有“dev_deploy”角色的人将能够查看开发环境,具有“test_deploy”角色的人将能够查看测试环境等。

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

我知道我可以编写一些凹槽脚本来生成选择的列表项。

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

我只是不知道由谁来掌握用户角色?

有人知道我该怎么做,或者有不同的解决方案吗?

非常感谢

最佳答案

好的,经过几次搜索,我找到了源代码 ( https://github.com/jenkinsci/role-strategy-plugin/tree/master/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy )

经过进一步阅读和一些尝试后,我想出了这个......

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

这对我有用。当您知道如何做时就很容易!

关于Jenkins 可扩展选择,具有基于用户角色的用户特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35311562/

相关文章:

github - 在 GitHub 合并事件上触发 Jenkins 构建

c# - 我可以使用 Jenkins 在 Ubuntu 上构建 C# 项目吗?

jenkins - 在 dreamhost 中存储 Jenkins 作业构建的日志文件/控制台输出

ssh - 服务器 ip 上的 Jenkins ssh 作为构建参数传递

maven - Jenkins 报告测试构建成功,即使存在由异常导致的失败?

amazon-web-services - 与Jenkins生成的zip文件一起部署时,Lambda函数将引发未找到类的异常

java - Jenkins 不重构文件夹名称,SonarQue 给出响应

php - 为什么 Behat PHP 命令在终端中有效,但在 Jenkins 中无效?

git - 无法使用 'Poll SCM' 和 Git notifyCommit 选项触发构建

jenkins - Jenkins xml配置为基于Groovy的Jenkins Job DSL