Writing in the front

Taking advantage of the National Day holiday, many friends are learning quietly, some are in order to successfully pass the interview, enter the big factory promotion and salary increase. Others seek to further consolidate and improve their professional skills in hopes of becoming Internet architects or even technologists. Some people asked me how to automate the build and release of Jenkins projects.

A friend’s Question

We still through a picture to intuitively understand the question of small partners.

Old rule. Let’s get straight to business.

Based on the environment

All environments are configured on a CentOS 6.5×64 server. IP address: 192.168.209.121 Host name: liuyazhuang121 And host name to IP address mapping is configured on the server.

Building a Web project

Environment configuration

First, let’s look at how Jenkins can automatically build and publish Web projects to remote Tomcat. We use the environment: Jdk1.8+Maven3.6.3+Tomcat9.0+Jenkins2.19.3+SSH+Shell

Among them, the JDK, Maven and Tomcat friends able to download and install and configure the system environment, can go to the link download.csdn.net/download/l1… Download jenkins2.19.3.

Configure SSH password-free login

ssh-keygen -t rsa
cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys/
Copy the code

Configuration Jenkins

Start the Jenkins

Copy the downloaded Jenkins War package to ${TOMCAT_HOME}/webapp, start Tomcat, and type in your browserhttp://192.168.209.121:8080/jenkins, as shown in the figure below, then prove that Jenkins started successfully.

Installing a plug-in

The plugins installed by default above are not enough

Click System Management -> Plug-in Management -> Optional Plug-ins -> Filter search box to find the following plug-ins:

  • Maven Integration plugin
  • SSH plugin
  • Deploy to container Plugin

Set system management -Global Tool Configuration

Configure JDK and Maven in system administration-Global Tool Configuration as follows:

Configure system administration -Configure Global Security

Register and log in to the user

Configure SSH remote hosts

Configure SSH remote hosts in System Management > System Settings

Click the Add button to set the login mode, as shown below:

Click the Check Connection button, and if Successfull Connection is displayed, SSH configuration is successful, as shown below:

New project Construction

Configuration project Construction

General- Configures the project name

Source code management – Configure SVN information

Configure the Pre Steps

Configure the Post Steps

Save the configuration.

Build the project

Go back to the home page and click the Build Project Now button as shown below:

You can see that the construction progress bar for maven_test appears on the page. Click the progress bar to enter the log output page, as shown below:

If the following log is displayed, tomcat is successfully built and deployed to tomcat.

Tomcat. Sh scripts

Finally, we present the contents of the tomcat.sh script, as shown below.

#! /bin/sh
  
## java env  
## service name    
SERVICE_NAME=tomcat_service  

case "The $1" in  
  
    start)  
         /usr/local/tomcat/test-tomcat-10000/bin/startup.sh 
        ;;  
  
    stop)  
        sleep 5  
        # #
        ## edu-service-aa.jar  
        ## edu-service-aa-bb.jar  
        P_ID=`ps -ef | grep -w "test-tomcat-10000" | grep -v "grep" | awk '{print $2}'`  
        if [ "$P_ID"= ="" ]; then  
            echo "= = =$SERVICE_NAME process not exists or stop success"  
        else  
            echo "= = =$SERVICE_NAME process pid is:$P_ID"  
            echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID"  
            kill  $P_ID  
        fi  
        ;;  
  
    restart)  
        $0 stop  
        sleep 2  
        $0 start  
        echo "=== restart $SERVICE_NAME"  
        ;;  
  
    *)  
        ## restart  
        $0 stop  
        sleep 2  
        $0 start  
        ;;  
  
esac  
exit 0 
Copy the code

Building a Java project

Jenkins build Jar package publish to remote server and Build War package publish to remote server configuration is basically the same, but the Post Steps configuration is slightly different.

Configure the Post Steps

Post Steps to configure Jar packages to publish to remote servers are as follows:

Build the project

The account – server. Sh scripts

Finally, we also present the contents of the account-server.sh script, as shown below.

#! /bin/sh
  
## java env  
export JAVA_HOME=/usr/local/ JDK/jdk1.8.0 _144export JRE_HOME=$JAVA_HOME/jre  
  
## service name  
APP_NAME=account  
  
SERVICE_DIR=/usr/local/dubbo-server/$APP_NAME  
SERVICE_NAME=medcare-dubbo-$APP_NAME  
JAR_NAME=$SERVICE_NAME\.jar  
PID=$SERVICE_NAME\.pid  
  
cd $SERVICE_DIR  
  
case "The $1" in  
  
    start)  
        nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar $JAR_NAME >/dev/null 2>&1 &  
        echo $! > $SERVICE_DIR/$PID  
        echo "=== start $SERVICE_NAME"  
        ;;  
  
    stop)  
        kill `cat $SERVICE_DIR/$PID`  
        rm -rf $SERVICE_DIR/$PID  
        echo "=== stop $SERVICE_NAME"  
  
        sleep 5  
        # #
        ## edu-service-aa.jar  
        ## edu-service-aa-bb.jar  
        P_ID=`ps -ef | grep -w "$SERVICE_NAME" | grep -v "grep" | awk '{print $2}'`  
        if [ "$P_ID"= ="" ]; then  
            echo "= = =$SERVICE_NAME process not exists or stop success"  
        else  
            echo "= = =$SERVICE_NAME process pid is:$P_ID"  
            echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID"  
            kill9 -$P_ID  
        fi  
        ;;  
  
    restart)  
        $0 stop  
        sleep 2  
        $0 start  
        echo "=== restart $SERVICE_NAME"  
        ;;  
  
    *)  
        ## restart  
        $0 stop  
        sleep 2  
        $0 start  
        ;;  
  
esac  
exit 0  
Copy the code

Big welfare

WeChat search the ice technology WeChat 】 the public, focus on the depth of programmers, daily reading of hard dry nuclear technology, the public, reply within [PDF] have I prepared a line companies interview data and my original super hardcore PDF technology document, and I prepared for you more than your resume template (update), I hope everyone can find the right job, Learning is a way of unhappy, sometimes laugh, come on. If you’ve worked your way into the company of your choice, don’t slack off. Career growth is like learning new technology. If lucky, we meet again in the river’s lake!

In addition, I open source each PDF, I will continue to update and maintain, thank you for your long-term support to glacier!!

Write in the last

If you think glacier wrote good, please search and pay attention to “glacier Technology” wechat public number, learn with glacier high concurrency, distributed, micro services, big data, Internet and cloud native technology, “glacier technology” wechat public number updated a large number of technical topics, each technical article is full of dry goods! Many readers have read the articles on the wechat public account of “Glacier Technology” and succeeded in job-hopping to big factories. There are also many readers to achieve a technological leap, become the company’s technical backbone! If you also want to like them to improve their ability to achieve a leap in technical ability, into the big factory, promotion and salary, then pay attention to the “Glacier Technology” wechat public account, update the super core technology every day dry goods, so that you no longer confused about how to improve technical ability!