docker set up Jenkins, automated deployment spring cloud projects

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

http://localhost:8091

enter password

Initialize the log inside, docker container log viewer password
docker logs jenkins_docker

7647927-fa481e334f18edab.png
image

Install plug

Choose to automatically install

7647927-15fe8aa41a9feefd.png
image
7647927-360bd241e2fa952f.png
image

If prompted version too, need to automatically update version Jenkins

MAVEN plug-in installation

7647927-134e62d512f0464f.png
image

SSH plug-in installation

7647927-7fccf203dc3c1aa8.png
image

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

7647927-745a13e0efd2550b.png
image

Configuring maven environment variables

7647927-cf68e58590589c97.png
image

Configuring SSH connection

7647927-42eaf25930eaa9c0.png
image

Creating MAVEN project

7647927-ed8b03a5f905cbf2.png
image

Jenkins deployment configuration

7647927-8770711ea23f2202.png
image
7647927-c95146e1c74c7d98.png
image
7647927-77586148bc21685c.png
image
7647927-9f9ff75a6ff8b0d0.png
image
  1. 7647927-ba253e19d412ff69.png
    image

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

7647927-965d1d20c62adbf6.png
image

Second way: idea change in LF

7647927-a3acc53424889854.png
image

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 !

Guess you like

Origin blog.csdn.net/weixin_34283445/article/details/90928930