Jenkins CI/CD continuous integration, continuous deployment (microservice multi-project deployment, single project deployment)

1. Install Jenkins

It is recommended to use Docker compose to install

Create a new docker-compose.yml script, modify relevant information, and execute the command docker-compose up -d in the script

version: '3.7'
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    restart: unless-stopped
    ports:
      - '8082:8080'
      - '50000:50000'
    volumes:
      - /mnt/data/jenkins_home/:/var/jenkins_home
      - /mnt/data/maven/apache-maven-3.9.0:/usr/local/maven
      - /usr/local/java-1.8.0-openjdk:/usr/local/java
      - /mnt/data/maven/apache-maven-3.9.0/conf/settings.xml:/usr/local/maven/conf/settings.xml
    networks:
      - gljk
networks:
  gljk:
    driver: bridge

jdk and maven use host services.

2 access jenkins

Enter http://ip:8082 to access jenkins, and it prompts that it needs to be unlocked. The cloud server is deployed and configured with a security group for the relevant port and firewall configuration.
insert image description hereGo to /mnt/data/jenkins_home/secrets/initialAdminPassword to copy the password and log in
insert image description here
Select custom plugin, admin to continue
insert image description here

Click to get started!

3. Plug-in installation

Before installation, change the plug-in download address and upgrade site url: https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
insert image description here
to install the following plug-ins:

  1. Maven Integration
  2. Publish Over SS
  3. Fail The Build
  4. Ant Plugin
  5. GitLab Plugin

Global tool configuration (the path configured here is the path in the container)

insert image description hereinsert image description here
insert image description here

target server configuration

When the project is automatically built, the packaged project will be transmitted to the target server via the network, and relevant information will be configured

insert image description here
modify time zone
insert image description here

4. New task

Enter the task name, select build maven project
insert image description hereconfiguration source code information
insert image description herecompilation configuration, the information configured here is about the editing details of the entire project, there are multiple sub-projects under the micro-service project, update does not need to update all sub-projects, all need to tell maven to edit the specific Serve.
insert image description hereEdit command: clean install -pl users -am -amd -Pdev -Dmaven.test.skip=true
parameter description:

-am --also-make builds the dependent modules of the listed modules at the same time;
-amd -also-make-dependents builds the modules that depend on the listed modules at the same time;
-pl --projects builds the specified modules, separated by commas;
-rf -resume-from Resume the reactor from the specified modules.
users is the name of the specified packaging service

The directory structure is as follows:
insert image description here
Compile the project separately, and the remote push will report an error later. Here, set the edit to a successful state. Save
insert image description hereinsert image description here
the remote settings (be sure to find the directory location) and click Build
insert image description here

insert image description here

5. Related issues

Build console error: ERROR: Failed to parse POMs

Solution:

1. The maven warehouse directory may not have write permission, authorize the warehouse directory: chmod -R 775 [maven directory path]

2. For the microservice project, check whether there is a relativePath tag in the multi-module file, and delete the tag

6. Start script

Set execution permissions: chmod u+x users.sh

包文件路径及名称(目录按照各自配置)
APP_NAME=/mnt/data/huorantech_jar/users/users-1.0.0.jar

#日志文件路径及名称(目录按照各自配置)
LOG_FILE=/mnt/data/huorantech_jar/users/users.log

#查询进程,并杀掉当前jar/java程序

pid=`ps -ef|grep $APP_NAME | grep -v grep | awk '{print $2}'`
kill -9 $pid
echo "$pid进程终止成功"

sleep 2

#判断jar包文件是否存在,如果存在启动jar包,并时时查看启动日志

if test -e $APP_NAME
then
	echo '文件存在,开始启动此程序...'

	# 启动jar包,指向日志文件,2>&1 & 表示打开或指向同一个日志文件
	nohup java -jar $APP_NAME > $LOG_FILE 2>&1 &

	#企业微信机器人通知配置
	date=$(date +%Y-%m-%d)
	time=$(date "+%H:%M:%S")
	content="**更新通知**
	    >Time:$date.$time
	    >Info:"用户服务更新成功"
	"
	webHookUrl="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?xxx"
	content='{"msgtype": "markdown","markdown": {"content": "'$content'","mentioned_list":"@all"},}'
	echo "content : $content"
	curl --data-ascii "$content" $webHookUrl
	echo "over!"

	#实时查看启动日志(此处正在想办法启动成功后退出)
	#tail -f $LOG_FILE

	#输出启动成功(上面的查看日志没有退出,所以执行不了,可以去掉)

	#echo '$APP_NAME 启动成功...'
else
	echo '$APP_NAME 文件不存在,请检查。'
fi

Guess you like

Origin blog.csdn.net/m0_46803792/article/details/129028804