这是一些
Jenkinsfile使您接近所需内容的技巧:
// return the user id that caused this build; else empty string@NonCPSdef user_id_cause() { def CAUSE = currentBuild.rawBuild.getCause( hudson.model.Cause.UserIdCause.class ); return CAUSE ? CAUSE.getUserId() : "";}// return all groups to which the given user id belongs@NonCPSdef groups(USER_ID) { return Jenkins.instance.securityRealm.loadUserByUsername(USER_ID).authorities.collect{ it.toString() };}...env.USER_ID_CAUSE = user_id_cause();if (!env.BRANCH_NAME.endsWith('-dev')) { if (env.USER_ID_CAUSE) { if ('jenkins_admins' in groups(env.USER_ID_CAUSE)) { echo("INFO: user id `${env.USER_ID_CAUSE}` is in the group `jenkins_admins`."); } else { currentBuild.result = 'ABORTED'; error("user id `${env.USER_ID_CAUSE}` is not in the group `jenkins_admins`."); } }}注意事项:
- 这些技巧在很大程度上依赖于需要詹金斯管理员进行“进程内脚本批准”的API函数。
- 上面的示例假定
jenkins_admins
特权用户所属的组的存在-–您的用户/组的情况可能大不相同。 - 通常,使用从Jenkins API函数返回的对象进行播放应在带
@NonCPS
注释的函数中进行,java.io.NotSerializableException
否则会有风险。
参考文献:
- https://github.com/jenkinsci/workflow-cps-plugin/blob/master/README.md
- http://javadoc.jenkins-ci.org/hudson/model/Cause.UserCause.html
- http://javadoc.jenkins-ci.org/hudson/model/Run.html#getCause-java.lang.Class-
- http://javadoc.jenkins.io/hudson/security/SecurityRealm.html#loadUserByUsername-java.lang.String-



