jenkins Slave API setLabelString adds to User list

痞子三分冷 提交于 2019-12-25 05:25:34

问题


I have the following script under a JOB, which when run adds the userId to the label of the slave.

import jenkins.model.Jenkins
import hudson.model.User
import hudson.security.Permission
import hudson.EnvVars

EnvVars envVars = build.getEnvironment(listener);

def userId= envVars .get('BUILD_USER_ID')
def nodeName= envVars .get('NODE_NAME')
def nodeOffpool= envVars .get('NODE_GOING_OFFPOOL')

allUsers = User.getAll()
println allUsers
println ""

// add userid as a label if doesnot exist
for (slave in hudson.model.Hudson.instance.slaves) {
  if( slave.nodeName.equals(nodeOffpool)) {

  def labelList = (slave.getLabelString()).split()
  println labelList
  // check for user access to machine
  for(label in labelList) {
       println (User.get(label))
       println (User.get(label) in allUsers)
       if (User.get(label) in allUsers) {
            if (label == userId) {
                 println ("This Node has already been assigned to you ($userId)")
            } else {
                 println ("This Node($nodeOffpool) has already been assigned to someone($label) else, you cannot use it now")
                 println ("Please ask the user $label  to release the Node($nodeOffpool)  for you($label) to run")
            }
            return
       }
  };
  println ("before: " + slave.getLabelString())
  // setting the slave with new label
  String newLabel = slave.getLabelString() + " " + userId 
  slave.setLabelString(newLabel)
  println ("after: " + slave.getLabelString())
  }
}

When i run for the first time output looks fine

[user1, user2, SYSTEM, unknown]
[TESTLAB3, TESTLAB4]
TESTLAB3
false
TESTLAB4
false
before: TESTLAB3 TESTLAB4
after: TESTLAB3 TESTLAB4 user1

When i run the second time

[user1, user2, SYSTEM, unknown, TESTLAB3, TESTLAB4]
[TESTLAB3, TESTLAB4, user1]
TESTLAB3
true
This Node(node1) has already been assigned to someone(TESTLAB3) else, you cannot use it now
Please ask the user TESTLAB3  to release the Node(node1)  for you(TESTLAB3) to run
Finished: SUCCESS

Is this issue with Jenkins API. I am using Jenkins 1.573

related questions:

  1. How to identify admins on Jenkins using groovy scripts
  2. jenkins change label as requested

Updated:

I found out the answer by trail and error.

User.get(label)

adds the label as user if it doesnt exist by default. To prevent this addition, we have to use

User.get(label, false)

回答1:


You are adding the username to the slave's label. Printing User.getAll() just prints the list of users for your Jenkins, not the slave's labels, which you modified with the user name.

Here is a script you can use to test if your slave machines have the new labels added to them:

for (slave in jenkins.model.Jenkins.instance.slaves) {
  print "Slave: " + slave.getNodeName() + "\n";  
  print "Label: " + slave.getLabelString() + "\n\n";
}

Also, you might want to change the script so that it only sets the new label for the Slave if it doesn't already contain the user, because you will be adding it multiple times every time the script is run the way it's written now.



来源:https://stackoverflow.com/questions/28972393/jenkins-slave-api-setlabelstring-adds-to-user-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!