Jenkins remote publishing script method, jenkins publishing script

Source: Internet
Author: User
Tags scp command

Jenkins remote publishing script method, jenkins publishing script

Jenkins remote scripting method. In the local jenkins release script, run the cp command to move the compiled jar file to another release directory on the same server and then release it. Normally, this is certainly not the case. It should be the location specified on other servers, and there may be more than one number of servers.

The scp command is used to move a file from one server to another. However, when using shell scripts to call scp, it will face a problem, that is, scp forces a password to be entered in interactive mode, unlike mysql and other options with-u-p. In order to achieve automated deployment, we need to borrow the keep CT script.

Verify CT Installation

Cenos executes the yum install unzip CT-y command to install the script. because it depends on tcl, it will also install tcl together.

The four most critical commands in secondary CT are send, secondary CT, spawn, and interact.

Send: used to send the string receive CT to the process: receive the string spawn from the process: Start a new process interact: allow user interaction between files transmitted between servers

First, let's do A small experiment. The requirement is to transfer the file to server B by transferring it to server. Here, we use reverse CT to transfer files between two servers.
Create A tranport. sh file under the directory/home/pec/tmp specified by server.

Cd/home/pec/tmp // switch to the specified directory touch tranport. sh // create the file vi tranport. sh // Edit

Add the following content in tranport. sh:

#!/usr/bin/expectspawn scp -P 22 /var/lib/jenkins/workspace/demo-test/target/demo-test-0.0.1-SNAPSHOT.jar  root@192.168.56.103:/home/pec/wwwexpect "*assword" {set timeout 300; send "123456\r";}expect "*#"interact

Note:
1./var/lib/jenkins/workspace/demo-test/target/demo-test-0.0.1-SNAPSHOT.jar indicates the file path of server,
2. root@192.168.56.103:/home/pec/www represents [B remote host user name @ B Remote Host IP Address: B remote host to receive files directory]
3. 123456 indicates the password of the remote host B.

The last step is to execute the tranport. sh file. Before execution, you must grant tranport. sh executable permissions. Otherwise, it cannot be executed.

Chmod + x startup. sh // grant the object row permission./startup. sh // execute the file

Follow the prompts to transfer files from one server to another. You can view the data in B's server directory.

Remote publishing script

The requirements are basically the same as those described above,Use jenkins to remotely transmit the compiled jar package of server A to server B and run it on server B.

Differentiate jobs. The jinkens on server A is responsible for compiling and packaging and executing the release script on server B. Server B prepares the release location and release script for server A to call. Of course, server B must have a Java Runtime Environment.

Server B is ready first:

Release Directory:/home/pec/www release Script: startup. sh files that store pid: demo-test.pid

All of the above names can be used as long as they have the same suffix and are used incorrectly.

Mkdir/home/pec/www // new directory cd/home/pec/www // switch to directory touch demo-test.pid // new file store pid touch startup. sh // create the script file startup. shchmod + x startup. sh // startup the script. sh grants the executable permission vi startup. sh // Edit

Add the following code to the startup. sh file:

#!/bin/bashwww_path=/home/pec/wwwjar_name=demo-test-0.0.1-SNAPSHOT.jarpid=$(cat /home/pec/www/demo-test.pid)if ${pid}; then        echo "pid is null"else        kill -9 ${pid}ficd ${www_path}java -jar ${jar_name} &echo $! > /home/pec/www/demo-test.pid

OK. The preparation for server B is complete.

What to do on server:
If you don't want to see anything else, just run the script and configure Post Steps in the jenkins task. The script is as follows:

#!/bin/bash export BUILD_ID=dontKillMewww_path=/home/pec/wwwjar_path=/var/lib/jenkins/workspace/demo-test/targetjar_name=demo-test-0.0.1-SNAPSHOT.jarexpect -c "spawn scp -P 22 ${jar_path}/${jar_name}  root@192.168.56.103:${www_path}expect  \"*assword\" {set timeout 300; send \"123456\r\";}    expect  \"yes/no\" {send \"yes\r\"; exp_continue;}"expect -c "spawn ssh root@192.168.56.103expect  {        \"*assword\" {set timeout 300; send \"123456\r\";}         \"yes/no\" {send \"yes\r\";exp_continue}}   expect \"*#\" send \"cd /home/pec/www\r\"expect \"*#\"send \"sh startup.sh\r\"expect \"*#\"send \"exit\""

The remote publishing of the service can be completed immediately after the service is created. There are no specific jenkins configurations and other things, such as JDK, mvaen, and git configurations. please Google them on your own.

Related Article

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.