Jenkins distributed build

Source: Internet
Author: User
Tags groovy script

Jenkins distributed build

Jenkins Distributed build (Jenkins Distributed builds)

Preface:

When an automated test case needs to be executed on multiple PCs or virtual machines, if a tomcat + jenkins-like environment is set up in each virtual machine, this will cause disadvantages such as high resource usage of each virtual machine and high configuration and maintenance costs for the environment. In this case, you can use the Jenkins distributed build method.

1. Jenkins node configuration

1. Master Configuration

1) Go to the Master's http: // ip: 8080/jenkins/web page.

2) choose System Management> node management.

3) Click "create node"


 

Remote working directory: Specify the working directory of the remote node machine, that is, the workspace directory where the code from checkout in the Job is located.

There are four startup methods:

Launch slave agents on Unix machines via SSH: This method can be selected when the node is Unix slaves

Launch slave agents via Java Web Start: Use JNLP to establish a connection between slave and master

Launch slave via execution of command on the Master: Using command line

Let Jenkins control this Windows slave as a Windows service: Jenkins will control the Windows slave as a Windows service

For Windows operating system nodes, the second Launch slave agents via Java Web Start is recommended.

Click "save" and a node in the Master will be configured.

2. slave node configuration

After the node is configured with Launch slaveagents via Java Web Start, you can see on the node interface that its slave has three startup methods, essentially one, download the slave-agetn.jnlp file from the master to the virtual machine where the slave is located, and then run the file.

Note: If the ip address in Run from slave command line is not set in the jenkins system configuration interface, it will be localhost, therefore, you must first check the ip address of your master machine.

For convenience, you can write the connection method to the bat batch file.

1) log on to the node Virtual Machine and create a remote working directory (D: \ jenkins) that is just configured)

2) create a bat file with the following command: start_jenkins_agent.bat content:

Javaws http: // 192.168.10.181: 8080/jenkins/computer/Windows_181_to_4400/slave-agent.jnlp

3) double-click the running file to view the connection between the slave node and the master node.

Note: Click File -- Installas a services to run the slave agent on a Windows system. Theoretically, it is convenient to start the server on startup. However, as a Windows service, it cannot interact with the GUI, therefore, Firefox cannot be started for automatic selenium testing. Therefore, if the automated testing is related to the GUI, do not Install as a services here.

Ii. Job Configuration

Create a new job in the Master, check Restrict when this project can be run on the configuration page, and select the node on which the Label is to run, in this way, you can specify the slave node on which the job runs.

Iii. Jenkins slave auto-start

1) If you want to enable auto start for all users. put the bat Startup File shortcut in the '\ Documents and Settings \ All Users \ "start" Menu \ Program \ start' directory

2) If you only need to enable auto start for the specified user. put the bat Startup File shortcut in the '\ Documents and Settings \ <username> \ "start" Menu \ Program \ start' directory

4. Jenkins node monitoring

Due to system running and network environment factors, jenkins nodes will inevitably encounter system disconnections and node disconnection. Therefore, to discover these problems, you need to monitor the nodes,

Can use the recommended monitoring Reconnect Mechanism here: https://wiki.jenkins-ci.org/display/JENKINS/Monitor+and+Restart+Offline+Slaves

1) Because jenkins node monitoring uses the jenkins internal api called by groovy scripts, you need to install the groovy plugin plug-in first.

2) create a new job named monitor in the Master and set it to run every 30 minutes.

3) Add the following steps to build the Excute system Groovy script:

Enter the groovy script (because there is no email authentication step in wiki, the script here adds email authentication ):

Import hudson. model. * import hudson. node_monitors. * import hudson. slaves. * import java. util. concurrent. * jenkins = Hudson. instanceimport javax. mail. internet. *; import javax. mail. * import javax. activation. * def sendMail (slave, cause) {// here, variables in parametric building are used. If this method is not used, comment out the variables and use toAddress = build below. buildVariableResolver. resolve ("EMAIL_RECEIVERS") message = slave + "slave is down. check http://192.168.10.181:8080/jenkins/computer/ "+ Slave +" \ nBecause "+ cause subject =" [jenkins node monitoring] "+ slave +" slave is offline "// toAddress = "***@***. com ;***@***. com "fromAddress = "***@***. com "host =" SMTP_SERVER "port =" SMTP_PORT "Properties props = new Properties (); // the server that sent the mail props. setProperty ("mail. smtp. host "," smtp. ***. com "); // protocol for sending mail props. setProperty ("mail. transport. protocol "," smtp "); // whether to verify the connection to the server. sending an email is the props to be verified. se TProperty ("mail. smtp. auth "," true "); // when verification is required, the Authenticator object Authenticator authenticator = new Authenticator () is automatically obtained from the Session () {@ Override protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication ("your user name", "your passwd"); // enter the user name and password}; Session lSession = Session. getInstance (props, authenticator); MimeMessage msg = new MimeMessage (lSession); // t Okenize out the recipients in case they came in as a list StringTokenizer tok = new StringTokenizer (toAddress, ";"); ArrayList emailTos = new ArrayList (); while (tok. hasMoreElements () {emailTos. add (new InternetAddress (tok. nextElement (). toString ();} InternetAddress [] to = new InternetAddress [emailTos. size ()]; to = (InternetAddress []) emailTos. toArray (to); msg. setRecipients (MimeMessage. recipientTy Pe. TO, to); InternetAddress fromAddr = new InternetAddress (fromAddress); msg. setFrom (fromAddr); msg. setFrom (new InternetAddress (fromAddress); msg. setSubject (subject); msg. setText (message) Transport transporter = lSession. getTransport ("smtp"); transporter. connect (); transporter. send (msg);} def getEnviron (computer) {def env def thread = Thread. start ("Getting env from $ {computer. name} ", {env = compu Ter. environment}) thread. join (2000) if (thread. isAlive () thread. interrupt () env} def slaveAccessible (computer) {getEnviron (computer )?. Get ('path ')! = Null} def numberOfflineNodes = 0def numberNodes = 0for (slave in jenkins. slaves) {def computer = slave. computer numberNodes ++ println "" println "Checking computer $ {computer. name}: "def isOK = (slaveAccessible (computer )&&! Computer. offline) if (isOK) {println "\ t \ tOK, got PATH back from slave $ {computer. name }. "println ('\ tcomputer. isOffline: '+ slave. getComputer (). isOffline (); println ('\ tcomputer. isTemporarilyOffline: '+ slave. getComputer (). isTemporarilyOffline (); println ('\ tcomputer. getOfflineCause: '+ slave. getComputer (). getOfflineCause (); println ('\ tcomputer. offline: '+ computer. offline);} else {numberOf FlineNodes ++ println "ERROR: can't get PATH from slave $ {computer. name }. "println ('\ tcomputer. isOffline: '+ slave. getComputer (). isOffline (); println ('\ tcomputer. isTemporarilyOffline: '+ slave. getComputer (). isTemporarilyOffline (); println ('\ tcomputer. getOfflineCause: '+ slave. getComputer (). getOfflineCause (); println ('\ tcomputer. offline: '+ computer. offline); sendMail (computer. name, slave. g EtComputer (). getOfflineCause (). toString () if (slave. getComputer (). isTemporarilyOffline () {if (! Slave. getComputer (). getOfflineCause (). toString (). contains ("Disconnected by") {computer. setTemporarilyOffline (false, slave. getComputer (). getOfflineCause ()} else {computer. connect (true) }}println ("Number of Offline Nodes:" + numberOfflineNodes) println ("Number of Nodes:" + numberNodes)

Note: In jenkins 1.582, a server Load balancer instance may be dropped and cannot be reconnected. It can be reconnected only after restart, cancelledKeyException can cause all JNLP slaves to disconnect (and the problem remains until restart): https://issues.jenkins-ci.org/browse/JENKINS-24050

Therefore, we recommend that you upgrade jenkins to the latest version or later than 1.582.

 

Automatically generate and publish GitBook (Nginx) using GitLab + Jenkins in the LAN)

Linux + Git + Maven + Jenkins + Neuxs automated compilation environment setup

CentOS6 install Jenkins

Use Jenkins to configure Git + Maven for automated building

Configure Jenkins + Maven + Git for continuous integration and automatic deployment

Distributed building and deployment of Jenkins-nodes

Jenkins details: click here
Jenkins: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.