Jenkins is a devops artifact, and this article describes how to install and use Jenkins to deploy the Spring boot project
Jenkins build deployment is divided into three steps;
- First step, Jenkins installation
- Step two, plug-in installation and configuration
- Step three, Push SSH
- Fourth step, deploy the project
First step, Jenkins installation
Preparation environment:
jdk:1.8
jenkins:2.83
centos:7.3
Maven 3.5 '
JDK is already installed by default
Configure Maven
Version Requirements maven3.5.0
Software download
wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.tar.gz
Installation
## 解压tar vxf apache-maven-3.5.0-bin.tar.gz## 移动mv apache-maven-3.5.0 /usr/local/maven3
Modify environment variables,
Add the following lines in the/etc/profile
MAVEN_HOME=/usr/local/maven3export MAVEN_HOMEexport PATH=${PATH}:${MAVEN_HOME}/bin
Remember to perform source /etc/profile
the environment variables in effect.
Verify
Last run mvn -v
Verify that MAVEN is installed successfully
Configuring the Shield Wall
Close the fence.
#centos7systemctl stop firewalld.service==============================#以下为:centOS 6.5关闭防火墙步骤#关闭命令: service iptables stop #永久关闭防火墙:chkconfig iptables off
Two commands run at the same time, check the firewall shutdown state after the run is complete
service iptables status
Jenkins installation
Download
cd /optwget http://mirrors.jenkins.io/war/2.83/jenkins.war
Start the service
java -jar jenkins.war &
Jenkins is on the start! It's war pack comes with Jetty server
When you start Jenkins for the first time, for security reasons, Jenkins automatically generates a random follow-up password. Note The password for the console output, copy it down, and then enter the passwords in the browser:
info: ********** ******************************************* *************************************************************please use the following password-proceed to Installation:this may also is found at:/root/.jenkins/secrets/ Initialadminpassword********************************************************************************************** ****************************
Access
Browser access:http://localhost:8080/
Input: 0cca37389e6540c08ce6e4c96f46da0f
To enter the user-defined plugin interface, it is recommended to install the official recommended plug-in, because after installation you have to install:
The next step is to enter the plugin installation Progress screen:
The plugin may not be fully installed at once, and you can click Retry to install again. Until all installation is successful
After waiting for some time, the plug-in installation is complete, configure the user name password:
Input: Admin/admin
System Management-the global tool configures the JDK path,
Step two, plug-in installation and configuration
There are many plugins that are selected by default for the installation, so now we need to install a few plugins, Git plugin and maven integration plugin,publish over SSH, ssh plugin.
Plug-in Installation: System Management > Plug-in Management > optional plug-ins, check the plugins you need to install, click Install directly or download reboot to install
Configuring global Variables
System Administration > Global Tools Configuration
Jdk
Configure the path of the local JDK, remove the check box to install automatically
Maven
Configure the path of the local maven, remove the check box to install automatically
Other content can be installed according to their own circumstances.
Configure SSH Free Login
SSH configuration can use the key, you can also use the password, here we use the key to configure, before the configuration of the Jenkins server and application Server key authentication
Generate a key pair on the Jenkins server, using the ssh-keygen -t rsa
command
Enter the following command to continue to enter, a rectangular graphic appears to indicate success, under ~/.ssh/will have the private key id_rsa and the public key id_rsa.pub
ssh-keygen -t rsa
Copy the contents of the Jenkins server's public key id_rsa.pub
to the file under the ~/.ssh/of the application server authorized_keys
ssh-copy-id -i id_rsa.pub 192.168.0.xxchmod 644 authorized_keys
Restart the SSH service on the application server, service sshd restart
now the Jenkins server can login directly to the application server without password
After the use of SSH B to try to be able to password-free login b server, if you are prompted to enter a passphrase, for the following reasons
- A. A non-root account may not support SSH public key authentication (see if the server is limited)
- B. The public key file passed over the permission is insufficient, can give this file authorization under chmod 644 Authorized_keys
- C. Use the root account to execute ssh-copy-id-i ~/.ssh/id_rsa.pub This command if you need to enter a password to configure the Sshd_config
vi /etc/ssh/sshd_config#内容PermitRootLogin no
Restart the SSHD service when you are finished modifying
service sshd restart
Finally, if SSH IP password-free login succeeds, the SSH public key authentication succeeds.
Step three, Push SSH
System Management > System Setup
Select Publish over SSH
Passphrase not set
Path to key writes the generated SSH path:/root/.ssh/id_rsa
The following SSH servers is the focus
Name is called at random to represent the service, and will be chosen by it later
Hostname Configuring the address of the application server
Username Configuring the Linux login user name
Remote Directory does not fill
Click below to add multiple application server addresses
Fourth step, deploy the project
Home Click New: Enter project name
Select Build a MAVEN project below and click OK.
Tick discard old build and choose whether to back up the old package that was replaced. I choose here to back up the last 10
Source control, choose SVN, configure SVN-related information, click Add to enter SVN's account and password
SVN address: http://192.168.0.xx/svn/[email protected], @HEAD
meaning to take the latest version
"Add timestamps to the Console Output" is checked in the build environment, and the log is printed during code building
In build, enter the MVN command before packaging, such as:
clean install -Dmaven.test.skip=true -Ptest
Exclude the package contents of the test, using a configuration file with the suffix test.
Post Steps Select Run only if build succeeds
Click Add post-build Step, select Send files or execute commands over SSH
Name Select the above configured push SSH
Source Files Configuration: Target/xxx-0.0.1-snapshot.jar Project JAR Package Name
Remove prefix:target/
The directory address of the Remote directory:jenkins-in/code application Server,
Exec command:jenkins-in/xxx.sh The corresponding script for the application server.
To create a folder on the application server: jenkins-in, copy the script contents in the folder: xxx.sh
date=$ (Date +%y%m%d)ExportJava_homePATHCLASSPATHjava_home=/usr/java/jdk1.8.0_131Path=$JAVA _home/bin:$JAVA _home/jre/bin:$PATHClasspath=.:$JAVA _home/lib:$JAVA _home/jre/lib:$CLASSPATHDir=/root/xxxJarfile=xxx-0.0.1-snapshot.jarif [!-D$DIR/backup];ThenMkdir-p$DIR/backupFiCd$DIRPs-ef|Grep$JARFILE|Grep-v grep|Awk' {print $} '|XargsKill-9Mv$JARFILE backup/$JARFILE $DATE MV -f/root/jenkins-in/< Span class= "hljs-variable" > $JARFILE. java-jar $JARFILE > out.log span class= "kw" >&if [$? = 0then sleep "tail-n Out.logfiCD backup/ ls -lt|awk |xargs rm-rf
The meaning of this script is to kill the old project, delete the old project, start a new project, and back up the old project.
Complete the full text.
Spring Boot (16) Deploy spring boot with Jenkins