我需要向特定用戶添加一些權限(讀取,構建,工作區,取消等)到很多作業。 我想知道有一種方法可以使用groovy腳本來代替手動執行。Jenkins使用groovy爲作業添加權限
1
A
回答
2
據我所知,你所要做的是不可能的。
鏈接:https://wiki.jenkins-ci.org/display/JENKINS/Matrix-based+security
我會建議使用「配置切片」如果你想在同一時間應用到多個作業的變化。
https://wiki.jenkins-ci.org/display/JENKINS/Configuration+Slicing+Plugin
,但我不認爲權限是反正切配置的一部分。
1
您可以使用執行系統常規步驟運行就像一個腳本:
import hudson.security.AuthorizationMatrixProperty
def jobWithPermissionsYouWant = "template-job-name"
def jobToAddPermissions = "job-to-update-name"
def jen = Jenkins.getInstance();
def templateJob = jen.getItem(jobWithPermissionsYouWant);
//get permissions from another job (use it as permissions template)
def autTemplate = templateJob.getProperty(AuthorizationMatrixProperty.class)
def permissionSetToAdd = autTemplate.getGrantedPermissions()
//get the authorization matrix property of the job you want to update
def jobToUpdate = jen.getItem(jobToAddPermissions);
def autToUpdate = jobToUpdate.getProperty(AuthorizationMatrixProperty.class)
def currPermissionSet = autToUpdate.getGrantedPermissions()
//for each permission in the template job, add permission to the job you want to update
permissionSetToAdd.each{
autToUpdate.add(it.key, "[email protected]")
}
println("the permissions after update: ${autToUpdate.getGrantedPermissions()}")
(如果你弄清楚如何獲得許可對象而不需要從不同的工作閱讀它,請更新)
1
我們並不需要創建新的權限對象,只需要創建新的hudson.security.AuthorizationMatrixProperty:
Jenkins.instance.getItem("JobName")
.removeProperty(hudson.security.AuthorizationMatrixProperty)
Jenkins.instance.getItem("JobName")
.addProperty(new hudson.security.AuthorizationMatrixProperty())
Jenkins.instance.getItem("JobName")
.getProperty(hudson.security.AuthorizationMatrixProperty)
.add("hudson.model.Item.Build:DEV")
其中「hudson.model.Item.Build:DEV」是一個權限字符串喲你可以從xml文件中獲得你需要的配置權限
1
我試過上面的解決方案,他們差不多工作。我所有的嘗試都會導致當前的內存權限反映新的設置,但這些權限並未保存在config.xml中,所以當Jenkins重新啓動時,權限就會丟失。安德魯胡拉的解決方案的基礎上,我想出了這個代碼,跨重新啓動功能:
AbstractProject proj = Hudson.instance.getItem("my_job")
AuthorizationMatrixProperty authProperty = proj.getProperty(hudson.security.AuthorizationMatrixProperty)
authProperty.add("hudson.model.Item.Build:DEV")
Map<Permission, Set<String>> permissionMap = authProperty.getGrantedPermissions()
proj.removeProperty(hudson.security.AuthorizationMatrixProperty)
proj.addProperty(new AuthorizationMatrixProperty(permissionMap))
1
你應該能夠拉它的部分出做你想做的事,有點像這樣。
AbstractProject proj = Hudson.instance.getItem("YourJob")
AuthorizationMatrixProperty authProperty = proj.getProperty(AuthorizationMatrixProperty.class)
Map<Permission,Set<String>> newPermissions = new HashMap<Permission, Set<String>>()
newPermissions.put(Item.BUILD, users)
proj.addProperty(new AuthorizationMatrixProperty(newPermissions))
proj.save()
更有趣的部分是如果您需要合併權限。
您也可以在上面的鏈接中找到相應的示例。
相關問題
- 1. Jenkins Shelving使用Groovy的作業
- 2. 在Jenkins groovy作業配置中使用latestSuccessfulBuild編號
- 3. 如何使用groovy腳本爲現有jenkins作業添加執行批處理命令
- 4. 如何在使用groovy的Jenkins中添加嵌套視圖?
- 5. 使用groovy向Jenkins添加全局密碼
- 6. 如何在Jenkins中爲Groovy插件添加jar?
- 7. hudson/jenkins - 在新作業頁面中添加作業模板類型
- 8. 在Active Choice反應參數中使用Groovy腳本檢索Jenkins作業名稱
- 9. 如何使用groovy腳本獲取jenkins中所有特定作業的編號?
- 10. 爲ASPNET帳戶添加權限
- 11. 爲NT AUTHORITY \ NETWORKSERVICE添加權限
- 12. Plone:爲角色添加權限
- 13. 爲什麼要添加READ_PHONE_STATE權限?
- 14. 爲特定部分添加權限Kentico
- 15. 如何控制特定jenkins作業集的權限,同時向整個組織提供構建權限
- 16. 如何通過Groovy中的Jenkins API執行jenkins作業保存事件?
- 17. Jenkins共享groovy庫git merge觸發作業
- 18. groovy腳本更新現有jenkins作業與鬆弛通知
- 19. Jenkins管道作業報告Groovy綁定錯誤
- 20. Jenkins和CMake:權限錯誤
- 21. Jenkins遷移後的權限
- 22. Jenkins整體/讀取權限
- 23. 在作業描述中使用HTML生成的Jenkins作業
- 24. Jenkins中的作業
- 25. 權限在cron作業否認COMAND
- 26. Oracle 9i統計作業的權限
- 27. 帶CakePHp ACL權限的Cron作業
- 28. Hadoop MapReduce作業權限被拒絕
- 29. 使用PHP添加/刪除cron作業
- 30. Microsoft Graph添加Directory.Read.All權限
所以沒有辦法使用腳本 – user3502786 2014-10-19 12:43:55
nope - 我不知道一種方式對不起。 – 2014-10-20 08:22:00