Use jenkins to realize automatic project deployment notes
1. Purpose
1. One-click realization of project automatic construction, packaging and release (for production environment)
2. Realize that the code is submitted to the code warehouse and automatically packaged and deployed immediately (for the test environment)
3. The test script code is at the end
2. Operating environment
-
hundreds
-
jdk
-
maven
-
jenkins
-
gitee
-
git
-
docker
2. Implementation idea
1. Production environment
-
Merge the code development branch to the master branch, ready to release the latest code
-
Jenkins one-click release
-
Jenkins pulls the latest code from the code repository
-
Packaging via maven
-
Package the jar package into a mirror image and upload it to your own mirror warehouse private server
-
Jenkins notifies the application server to pull the latest image
-
The application server pulls the latest image and runs the new container
-
Delete old containers and images
-
If the release fails, roll back to the previous version
2. Test environment, development environment
-
Submit code to code warehouse dev or other branches
-
Automatically trigger jenkins to build the project
-
Jenkins pulls the latest code from the code repository
-
Copy the latest code to the test server
-
Automatically connect to the test server for maven packaging
-
Package the jar package into a mirror image
-
Delete old containers and images
-
Run the new container from the newly packaged image
3. Preliminary preparation
-
Two centos servers can ping each other (or two virtual machines)
-
Prepare a springcloud or springboot project for automatic release testing (I am preparing a multi-module project here, the directory structure is as follows)
-
jdk 1.8 installation package, maven 3.6 package
-
jdk download address: https://wp.boytian.com/#/s/p8Fv password tiantian123
-
maven download address: https://wp.boytian.com/#/s/2lc1 password tiantian321
4. Install jdk environment and maven environment
“
It is best to install both, here is a demonstration of a server
”
1. jdk environment installation
-
Create jdk storage directory
mkdir -p /home/jdk
-
Upload jdk to this directory, enter this directory and decompress, decompress command:
tar -zxvf jdk-8u144-linux-x64.tar.g
2. maven installation
- Create maven directory and maven warehouse directory
-
mkdir -p /home/maven
-
mkdir -p /home/maven/repo
- Upload maven to
/home/maven/
the directory, enter the directory and decompress, decompress command:tar -zxvf apache-maven-3.6.3
3. Configure the environment variables of jdk and maven
-
modify
/etc/profile
the file, the commandvim /etc/profile
-
If the directory creation is consistent with mine, add the following configuration at the bottom, otherwise you can modify it yourself
#输入i 进入编辑模式 # java配置 export JAVA_HOME=/home/jdk/jdk1.8.0_144 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/bin #maven 配置 export MAVEN_HOME=/home/maven/apache-maven-3.6.3 export PATH=$MAVEN_HOME/bin:$PATH
-
Refresh the configuration to take effect
source /etc/profile
-
To test whether the environment is effective, the command
java
mvn -v
-
Another server host is also configured
5. Configure secret-free login (two servers log in without secret each other)
-
Enter
ssh-keygen -t rsa
and hit three carriage returns -
Enter
ssh-copy-id -i ~/.ssh/id_rsa.pub ip地址
Enter -
Enter the password verification of the second server to realize password-free login
-
In the same way, configure the second server to log in to the first server without password
“
Reference: https://blog.csdn.net/u013415591/article/details/81943189
”
6. Install the jenkin service on the first server
1. Execute the following commands in sequence on the first server installation
-
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
-
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
-
yum install jenkins
-
ln -s /home/jdk/jdk1.8.0_144/bin/java /usr/bin/java
-
sudo ln -s /home/maven/apache-maven-3.6.3 /usr/local/maven
-
systemctl start jenkins
“
(1, 2, 3) is to install jenkins, (4, 5) is the soft link environment command to the usr directory (6) start Jenkins
”
2. Modify the authority of jenkins to root authority, so as to facilitate the problem of insufficient authority to execute commands in the future
# 打开配置文件
vim /etc/sysconfig/jenkins
# 修改$JENKINS_USER
$JENKINS_USER="root"
#修改Jenkins相关文件夹用户权限
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins
#重启
systemctl restart jenkins
1619053352839
7. Access the jenkins service for some basic configuration
- Browser access: ip:8080
1618994599246
- Get the password command from the server:
cat /var/lib/jenkins/secrets/initialAdminPassword
1618995408605
-
Install custom plugins
1618995513826
-
Create an administrator account
-
After clicking Finish, restart the jenkins service, command:
systemctl restart jenkins
-
Jenkins configures the global jdk and maven environment
1618996436490
1618996573737
1618996616938
-
Install the gitee plugin, push code to gitee is mainly triggered by this plugin
-
Configure gitee interaction credentials
-
Global configuration gitee
8. Prepare to deploy the project
- Create gitee code repository
1619050043509
9. Configure jenkins to start configuring automatic deployment
- Create an automated build process choosing Freestyle
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tAOlG9N1-1637893426234)(https://mmbiz.qpic.cn/mmbiz_png/0bmE6dS9CgJQUEbkCyje2ajToYicZGj4rVheOTDiahK1yzXSvdpwsAOsNeiaibia91ZwxkAG0QLEpv6hJmdNEGIeksA/640?wx_fmt=png )]
1618997421632
-
Select parametric build
“
Define some variables, which are used in the script
”
1619050337908
1619054535420
3. Configure the git repository
1619050733823
1619050777451
1619050871896
-
Build automatic triggers, configure jenkins
-
Build triggers, configure gitee
1619051250412
-
Test whether the build works
Configure jenkins to add build steps
1619051394272
Click Apply to save
- Test manual builds
1619051457074
1619051489297
1619051513195
-
Test automated builds
“
Manually push the code to see if it will start an automatic build
”
10. Automated deployment scripts
“
The automatic build configuration is completed, and then write a script for the real deployment project
The content of the script is as follows. Currently, the function is implemented but it is still not perfect. It will be improved later
”
-
Project deployment feet
# 项目模块统一变量设置 # 公共服务 CLOUD_CONTAINER="cloud" CLOUD_IMAGE="cloud_server" CLOUD_PORT=8002 # 微信小程序模块 FAMILY_CONTAINER="family" FAMILY_IMAGE="family_server" FAMILY_PORT=8003 # 鉴权模块 AUTH_CONTAINER="auth" AUTH_IMAGE="auth_server" AUTH_PORT=8004 # 网关 GATEWAY_CONTAINER="gateway" GATEWAY_IMAGE="gateway_server" GATEWAY_PORT=9999 # 删除应用服务器旧的代码(在第二个服务器创建好/home/project/fym-platform/目录,否则报错) ssh -Tq ${host} <<EOF rm -rf /home/project/fym-platform/* EOF # 拷贝新代码到应用服务器 echo "拷贝代码开始..." scp -r ./* ${host}:/home/project/fym-platform echo "拷贝代码结束..." # 登录到项目服务器执行命令 ssh -Tq ${host} <<EOF cd /home/project/fym-platform echo "项目开始构建打包" mvn clean package -Dmaven.test.skip=true -P${environment} echo "jar文件拷贝" mkdir -p /web/project/fym-platform cp -r gateway-server/target/gateway-server*.jar Dockerfile /web/project/fym-platform/ cp -r fym-service/*/*/target/*server*.jar /web/project/fym-platform/ echo "=============== 开始部署容器 ===============" # 停止并删除旧的容器 docker ps -a|grep $CLOUD_CONTAINER && docker stop $CLOUD_CONTAINER && docker rm $CLOUD_CONTAINER || echo "not exist" docker ps -a|grep $GATEWAY_CONTAINER && docker stop $GATEWAY_CONTAINER && docker rm $GATEWAY_CONTAINER || echo "not exist" docker ps -a|grep $FAMILY_CONTAINER && docker stop $FAMILY_CONTAINER && docker rm $FAMILY_CONTAINER || echo "not exist" docker ps -a|grep $AUTH_CONTAINER && docker stop $AUTH_CONTAINER && docker rm $AUTH_CONTAINER || echo "not exist" # 删除旧的镜像 docker images |grep $CLOUD_IMAGE && docker rmi -f $CLOUD_IMAGE || echo "not exist" docker images |grep $GATEWAY_IMAGE && docker rmi -f $GATEWAY_IMAGE || echo "not exist" docker images |grep $FAMILY_IMAGE && docker rmi -f $FAMILY_IMAGE || echo "not exist" docker images |grep $AUTH_IMAGE && docker rmi -f $AUTH_IMAGE || echo "not exist" # 构建新的镜像 docker build --build-arg JAR_FILE=cloud-server-1.0.jar -t $CLOUD_IMAGE /web/project/fym-platform/ docker build --build-arg JAR_FILE=gateway-server-1.0.jar -t $GATEWAY_IMAGE /web/project/fym-platform/ docker build --build-arg JAR_FILE=family-server-1.0.jar -t $FAMILY_IMAGE /web/project/fym-platform/ docker build --build-arg JAR_FILE=auth-server-1.0.jar -t $AUTH_IMAGE /web/project/fym-platform/ # 运行镜像容器 docker run -it -d --name $CLOUD_CONTAINER -p $CLOUD_PORT:$CLOUD_PORT $CLOUD_IMAGE docker run -it -d --name $GATEWAY_CONTAINER -p $GATEWAY_PORT:$GATEWAY_PORT $GATEWAY_IMAGE docker run -it -d --name $FAMILY_CONTAINER -p $FAMILY_PORT:$FAMILY_PORT $FAMILY_IMAGE docker run -it -d --name $AUTH_CONTAINER -p $AUTH_PORT:$AUTH_PORT $AUTH_IMAGE EOF echo "部署成功"
-
jar image packaging script
FROM primetoninc/jdk:1.8 ARG JAR_FILE MAINTAINER [email protected] COPY ${JAR_FILE} app.jar ENV JAVA_OPTS="-Xmx512m -Xms512m -Xmn512m -Xss256K -XX:SurvivorRatio=8 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:+UseConcMarkSweepGC -XX:ConcGCThreads=4 -XX:+CMSClassUnloadingEnabled -Dfile.encoding=UTF-8 -XX:+DisableExplicitGC -XX:+PrintGC -XX:+PrintGCTimeStamps -Xloggc:./gc.log" ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
-
Modify the jenkins shell configuration just now as follows
#!/bin/bash chmod u+x run.sh # 一行是赋予脚本执行权限 ./run.sh #一行是执行脚本
1619052201097
11. Test automatic build and manual build
- Manually trigger the build (the same is true for automatic triggering when submitting code), check the output console, and the maven dependencies have been downloaded and packaged
1619053964419
1619054134274
1619054194717
- View the second server docker image
1619054218370
1619054239768
-
View nacos service startup status
-
Test whether the project is deployed successfully