This paper describes:
1. How to build Jenkins with a docker in Linux
2. automated deployment of the multi-module maven spring cloud projects
What is Jenkins
- Jenkins is an open source software project, is based on a Java development of automated testing and continuous integration, publishing tools.
Jenkins advantage
Jenkins' main objective is to monitor the software development process, rapid display problems. Therefore, to ensure the development effort to improve the efficiency of the development staff and related personnel province.
Project operation and maintenance engineers released in the single-instance architecture environment only need to sign a Linux, replace jar package to restart the war package, but micro-services architecture have a lot of modules, need to be deployed in different servers, respectively, if all manual deployment , you also need to switch profiles depending on the environment, Jenkins will be able to resolve this issue.
Linux installation docker
Installation tutorial
https://www.jianshu.com/p/ecb1e0eb0514
Download Jenkins Mirror
docker pull Jenkins
Install the JDK
The omitted to search the Internet
Installation maven
[root@ip-172-31-20-91 ~]# mkdir maven3.6
[root@ip-172-31-20-91 ~]# cd maven3.6/
[root@ip-172-31-20-91 maven3.6]# wget https://www-us.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz
[root@ip-172-31-20-91 maven3.6]# tar -xf apache-maven-3.6.0-bin.tar.gz
MAVEN configuration environment variable
cd /etc/profile.d/
vim maven.sh
Add the following arranged in 'maven.sh' profile.
#Apache Maven环境变量
export M2_HOME=/usr/local/src/apache-maven
export PATH=${M2_HOME}/bin:${PATH}
Start Jenkins
docker run \
--name jenkins_docker \
-p 8091:8080 \
-u root \
-v /root/jenkins/jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /root/maven3.6/apache-maven-3.6.0:/usr/local/maven3.6 \
-v /usr/bin/java:/usr/local/jdk1.8 \
-d \
jenkins
Be sure to determine whether to open firewall ports
The name of the vessel is taken --name jenkins_docker
-p 8091: 8080 to Jenkins vessel default port 8080 is mapped to the host port 8091
-u root root authority
-v / root / jenkins / jenkins-data: / var / jenkins_home Jenkins mapping data directory, data loss prevention
-v /var/run/docker.sock:/var/run/docker.sock let the container can be used docker command
-v /root/maven3.6/apache-maven-3.6.0:/usr/local/maven3.6 mount host maven
-v /usr/bin/java:/usr/local/jdk1.8 mount host jdk
-d background
jenkins Home
enter password
Initialize the log inside, docker container log viewer password
docker logs jenkins_docker
Install plug
Choose to automatically install
If prompted version too, need to automatically update version Jenkins
MAVEN plug-in installation
SSH plug-in installation
Open remote SSH connection
In order to be able to Jenkins jar package reached the remote server, and open services and other operations
The native ssh public key file on the remote host
root@20af72d193f2:/# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
Server ~ / .ssh / authorized_keys
Configuring environment variables jdk
Configuring maven environment variables
Configuring SSH connection
Creating MAVEN project
Jenkins deployment configuration
shell script
#!/bin/bash
cd /jar包目录
echo "正在kill"
export JAVA_HOME=/usr/bin/java
export PATH=$JAVA_HOME/bin:$PATH
jar_name="jar_name"
echo "name:${jar_name}"
ps aux|grep jar_name|awk '{print $2}'|xargs kill -9
ps aux|grep jar_name|awk '{print $2}'|xargs kill -9
ps aux|grep jar_name|awk '{print $2}'|xargs kill -9
nohup java -jar xxxxx.jar -Xmx=512M -Xms=512M -Xmn=128M --spring.profiles.active=test -Dserver.tomcat.max-threads=10 >/dev/null 2>&1 &
echo "启动xxxxxx"
#睡眠15s
sleep 15s
nohup java -jar ${jar_name}-0.0.1-SNAPSHOT.jar -Xmx=512M -Xms=512M -Xmn=128M --spring.profiles.active=test -Dserver.tomcat.max-threads=10 >/dev/null 2>&1 &
echo "${jar_name}"
ps -ef |grep java
free -h
exit 0
shell format conversion
Windows command shell written on character encoding and Linux can not run due to inconsistent shell
solve
Method One: Use notepad ++ conversion
Second way: idea change in LF
Reference nginx Jenkins https://wiki.jenkins.io/display/JENKINS/Jenkins+says+my+reverse+proxy+setup+is+broken
https://www.phase2technology.com/blog/running-jenkins-behind- nginx !