Jenkins/Hudson CLI API to modify the node labels using Groovy

前端 未结 3 1517
死守一世寂寞
死守一世寂寞 2021-02-20 03:13

Does anyone know how to modify the Jenkins/Hudson node labels in a non-manually way? I mean, thorough an API like the CLI API that this tool offers (without restarting Jenkins/H

3条回答
  •  故里飘歌
    2021-02-20 03:36

    Note: the other answers are a bit old, so it could be that the API has appeared since then.

    Node labels are accessed in the API as a single string, just like in the Configure screen.

    To read and write labels: Node.getLabelString() and Node.setLabelString(String).

    Note that you can get the effective labels as well via: Node.getAssignedLabels(), which returns a Collection of LabelAtom that includes dynamically computed labels such as the 'self-label' (representing the node name itself).

    Last, these methods on the Node class are directly accessible from the slave objects also, e.g. as a System Groovy Script:

    hudson = hudson.model.Hudson.instance
    hudson.slaves.findAll { it.nodeName.equals("slave4") }.each { slave -> 
      print "Slave  $slave.nodeName : Labels: $slave.labelString"
      slave.labelString = slave.labelString + " " + "offline"
      println "   --> New labels: $slave.labelString"
    }
    hudson.save()
    

提交回复
热议问题