What is continuous integration?

To put it simply, when a large system is split into multiple services, each small team is responsible for one service, updates and releases of services become frequent, tools are used to monitor every submission of code, and continuous automated build processes are called continuous integration.

Why Jenkins for continuous integration?

Jenkins is a powerful application that allows continuous integration and continuous delivery of projects and is a free open source platform that can handle any type of build or continuous integration.

Install Jenkins and other required environments
  • 1. Server environment: centos7(192.168.68.141), Tomcat8, Git, JDk1.8, maven3.5.2
  • 2.Git code Cloud account and warehouse code. Jenkins pulled the code from the warehouse for construction and compilation. After construction, shell command was automatically executed to start the service and complete the release
  • 3. Install jdk1.8, Maven3.5.2, configure JDK and Maven in /etc/profile, and source /etc/profile to make them effective

  • 4. Install git on centos, yum install git-core, enter git –version, configure git global user and mailbox information, first clone project on git server, check whether the project can be cloned by using git command.This step is very importantAt home directory pull code git clone [email protected]: zhuyu1991 / spring – cloud. Pull to local git command, the directory that is configured in the building for the first time in Jenkins, after building, need to configure the Jenkins workspace, In the/root /. Jenkins/workspace /, there are instructions below, then the Jenkins configuration pull on a regular basis in the warehouse code to build, the following screenshot shows code to local warehouse.



  • 4. Download the Jenkins. War, https://jenkins.io/download/
  • 5. Download Tomcat, https://tomcat.apache.org/
  • 6. Put the Jenkins. War package in the Tomcat webapps directory and start Tomcat

  • 7. Then in the browser to visit: http://192.168.68.141:8080/jenkins/, you will see the following interface, representing Jenkins has been deployed, the next step Jenkins installation configuration
  • Get Jenkins admin default password cat/root /. SSH/secrets/initalAdminPassword

  • 8. Go to the screen for selecting plug-ins and select the first one (Install Suggested Plugins) to Install the default Jenkins plug-in
  • 9. After the plug-in is installed, you need to create a user
  • 10. After entering the main interface of the system, click system Management on the left, and then click management Plug-in on the other side. Jenkins needs to install Git Plugin and Maven Integration plug-in
  • 11. After installing the necessary plug-ins, click System Management, and then click Global Tool Configuration to configure JDK, Maven, and git for Jenkins to build. The JDK and Maven paths have been configured in /etc/profile, and the paths have been copied to the corresponding Home

  • 12. The server generates SSH keys. Id_rsa (private key), id_rsa.pub(public key), the public key is configured in the public key management configuration of git repository, and the private key is configured in Jenkins. Jenkins uses the private key to pull the code from git repository, and the git repository verifies whether the pull can be done by comparing the private key with the public key
  • Ssh-keygen -t rsa -c “[email protected]” in /root/.ssh/, generate two files, configure the public key to the project of the git repository, configure the private key to the Jenkins global credential, check the public key and private key: cat /root/.sshh/id_rsa



  • Return to the main screen, click New Task, enter the name, select Build Maven project, and click Save. If there is no Maven, it indicates that Jenkins’ Maven plugin is not installed

  • 14. Configure a Jenkins job



    Select Git, enter the Git repository address, and select the Credentials you created earlier



    Check the polling SCM and build every 10 minutes (H/10 * * * *), triggering the build whenever data is updated in Git



    Build environment select Add timestamps to the Console Output



    Note:The Pre Steps Build needs to enter the pom.xml of the project to be built. For the first Build, since it is clone directory at home, enter /root/spring-cloud/fast/pom.xml. After the first build Jenkins will copy the project to its working directory/root /. Jenkins/workspace /, the second build time need to modify the path for Jenkins through git pull the code in the working directory

Post Steps After the build is complete, add the execute shell script, which means to find the eureka-server process, kill it, and then start the Eureka-server package



  • Click Save and then Build to see a blue and white progress bar on the left. Click the black triangle above the progress bar to see the console output. You can see that Maven is only compiling the project



    You can see that the build succeeded because you set up a script to start the service after a successful build

    So the following shell script will be executed:
#! /bin/bash
echo "********************** Jenkins Stopping SpringBoot Application*************************"Jar_name = eureka - server - 0.0.1 - the SNAPSHOT. Jar pid = ` ps - ef | grep$jar_name | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
#! Kill -9 Forcibly terminates
   echo "Kill 9 pid." $pid
   kill9 -$pid
fi
file_path=/root/.jenkins/workspace/eureka-server/eureka/eureka-server/target/
echo "Execution..."
java -jar $file_path$jar_name
echo "********************** Jenkins Started SpringBoot Application*************************"
1234567891011121314Copy the code



You can see that the log output Started Eureka Server and ok that the build is complete

Visit eureka – http://192.168.68.141:10025/ server address, you can see just build a success



Because the above configuration of the polling SCM, every 10 minutes to query the Git repository, if there is any code submitted, will trigger the build, after the Git repository changes the code submitted (the note information is: add print log), wait 10 minutes to see whether a build

After a lapse of more than 10 minutes, an automatic build was performed and the remarks from git commit were pulled

Well, Jenkins polling pull code in Git warehouse, any change will trigger construction, after construction can execute custom shell script, integration test can be very convenient, package to docker private warehouse, and then take docker image to test and formal environment for online

By default, Jenkins will kill any derived process triggered by Jenkins’ shell command during the build process after completion. Jenkins uses BUILD_ID to identify whether a process is a derivative process of the build process. Therefore, after changing BUILD_ID, Jenkins cannot identify whether a process is a derivative process and the process can be kept running in the background

1. Modify the shell script to enable background startup. In Post Steps, replace the previous script with the following one

OLD_BUILD_ID=$BUILD_ID
echo $OLD_BUILD_ID
BUILD_ID=dontKillMe

# Put shell script or shell command here
sh /restartBoot1.sh

BUILD_ID=$OLD_BUILD_ID
echo $BUILD_ID
123456789Copy the code

Restartboot1. sh file in the root directory of the server. The file is created on centos and the contents are copied to the file.

#! /bin/bash
echo "********************** Jenkins Stopping SpringBoot Application*************************"Pid = ` ps - ef | grep eureka - server - 0.0.1 - the SNAPSHOT | grep -v grep | awk'{print $2}'`
if [ -n "$pid" ]
then
#! Kill -9 Forcibly terminates
   echo "Kill 9 pid." $pid
   kill9 -$pid
fi
echo "Execution..."Nohup Java jar/root /. Jenkins/workspace/eureka - server/had been/eureka - server/target/eureka - server - 0.0.1 - the SNAPSHOT. Jar &echo "********************** Jenkins Started SpringBoot Application*************************"
123456789101112Copy the code

The eureka-server process id is killed by the eureka-server process id. The eureka-server process id is killed by the eureka-server process ID

Jenkins failed to compile Maven Complier on the server because lombok was used in the project. You need to change maven complier to be compatible with earlier versions

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> The < version > 3.6.2 < / version > < configuration > <source> 1.8 < /source> < target > 1.8 < / target > < configuration > < / plugin > < / plugins > < build > 1234567891011121314151617Copy the code