The previous section completed "When the GIT Client push code to Gitlab, Jenkins will immediately go to Gitlab and build".
Purpose: This section completes the automatic build of Jenkins and automatically deploys the jar package to the application server and starts the service.
Machine:
- Jenkins Server: 10.211.55.4
- Application Server: 10.211.55.3
First, Jenkins installation Publish-over-ssh
Install the plug-in process as before.
Second, establish SSH trust (this is the principle of ssh )
1. Jenkins generates key pair
Executed on the Jenkins server
- SSH-keygen-t rsa-c "[email protected]"
At this point, the Id_rsa (private key) and id_rsa.pub (public key) are generated under the ~/.ssh/directory.
2. Paste the public key into the application server
Copy and paste the contents of Id_rsa.pub to the application server's ~/.ssh/Authorized_keys,
If you do not create this file yourself.
3. Attach the private key to the Jenkins server's SSH servers
Systems management, System settings,
Description
- Passphrase: We set when executing key pair generation commands.
- Key:jenkins the generated private key id_rsa content.
- SSH Server:
- Name: Casual
- Hostname: Application Server IP
- Username: Casual
- Remote directory:jenkins The upload directory of files uploaded to the application server .
After the configuration is complete, click "Test Configuration" to display "success", then the connection is successful!!!
Third, configuration services
In the "Configuration" of the service "Mytest-service1", click "Add post-Build Action",
Description
- source Files: The location where the file is uploaded (this is the jar package), and its relative path is ~/.jenkins/workspace/mytest-service1
- Remove prefix: Remove the prefix target from source files, otherwise the target directory will be created on the application server, i.e. the jar package is stored in/data/jar/target
- Execcommand: After uploading a file, the remote command executed on the application server
- Go to the location where the script service_start.sh is located
- Executes the service_start.sh script and passes in the parameters Mytest-service1-1.0-snapshot.jar and 8088
IV. Application Server
Create Script /opt/script/service_start.sh
1#!/bin/Bash2Export jar_name=$13Export port=$24Export JAVA_HOME=/OPT/JDK1.8. 0_1025Shutdown_second=56 Echo "Jar_name is ${jar_name}, port is ${port}"7 8Pid="' ${java_home}/bin/jps-l | grep ${jar_name} | awk ' {print '} '"9 Echo "pid is ${pid}"Ten One if[-N"${pid}" ] A Then - Kill-9${pid} - Sleep${shutdown_second} the fi - -cd/data/jar/ - Echo "start ${jar_name} process" +Nohup ${java_home}/bin/java-jar-dserver.port=${port} ${jar_name}>/data/log/${jar_name}.log & - Echo "End ${jar_name} process"
Description
- Jar_name The first parameter passed to the command line, which is Mytest-service1-1.0-snapshot.jar
- Port is the second parameter passed to the command line, which is 8088
- Specify Java_home
- Note: Using ${java_home}directly on the application server is possible, but remote execution via Jenkins is not available
- Define Shutdown_second to 5s
- Get all Java processes using JPS, use grep to get the Mytest-service1-1.0-snapshot.jar process, use awk to get the first parameter of process information (that is, the process number PID)
- If the application server natively, as long as the configuration of Java_home, you can directly use the JPS, but through the Jenkins remote execution will be written all, including the Java command behind.
- If the PID is not empty, kill the process, sleep 5s
- Enter the location of the jar treasure, execute the start command, and output the log to/data/log/mytest-service1-1.0-snapshot.jar.log
- If the application server natively, can be directly nohup execution, if through Jenkins remote execution, you need to output the log to a file in an application server , or Jenkins will wait until the build process until the timeout failed. (There are also/data/log/directories to be built in advance, or built in scripts.)
V. Testing
Local development machine, modify code,
- git add--all
- Git commit-m "Test ci"
- Git push Origin Head:dev
View Jenkins output console log, view application Server JPS Java process, access Http://10.211.55.3:8088/test/hello in browser
42nd Micro Service CICD (4)-Jenkins + Gitlab + webhooks + publish-over-ssh (2)