[Jenkins] to create a slave by jenkinsapi

Due to manually configure the slave is not quick and easy, it is hoped created directly by slave python script.

Using the Module

The use of third-party modules jenkinsapi call Jenkins rest Interface:

from jenkinsapi.jenkins import Jenkins

Specific description and examples can be viewed: https: //pypi.org/project/jenkinsapi/

Jenkins created objects

Set Jenkins address, Jenkins created objects using the account that has sufficient rights.

= jenkins_url " HTTP: //xx.xx.xx.x/ "  # Jenkins Address 
username = " XXX_XX "  # Jenkins username 
password = " xxxx "  # Jenkins password 

# create objects Jenkins 
Jenkins = Jenkins (jenkins_url, 
                  Requester = Requester ( username, password, 
                                      BaseURL = jenkins_url, ssl_verify = False))

Creating nodes

Because in order to create nodes for Android monkey testing, the node is expected to be a Windows system, it is the node created by jnlp started.

# Remote working directory 
Workspace = " C: \ ProgramData \ monkeyTest " 
# get the computer name & username for naming Slave 
PC_Name = os.popen ( ' hostname ' ) .read () Strip (). 
User_name = getpass.getuser () .strip () 

# create a JNLP (the Java Webstart) slave 
DEF creatSlave ():
     # create a slave remote working directory 
    IF os.path.exists (Workspace):
         Print ( " Workspace directory already exists " )
     the else : 
        os.mkdir (Workspace) 
        Print ( " was created Workspace: " +workspace)
    #slave配置
    node_dict = {
        'num_executors': 1,                            # Number of executors
        'node_description': "monkey slave of "+PC_name,# Just a user friendly text
        'remote_fs': workspace,                        # Remote workspace location
        'labels': "monkey test node",                  # Space separated labels string
        'exclusive': True                              # Only run jobs assigned to it
    }
    new_slave = jenkins.nodes.create_node(user_name +"_"+ PC_name, node_dict)
    print("已创建节点"+new_slave.name)
    return new_slave

Direct call createSlave (), you can create a to-node links Jenkins

 

Guess you like

Origin www.cnblogs.com/6970-9192/p/11273945.html