table of Contents
1. Prerequisite environment
1、Maven
reference:
https://blog.csdn.net/wo541075754/article/details/89162608
(Need to change the maven installation directory to maven for easy mapping)
2、Docker
Baidu
3 、 gitlab
Docker configuration Gitlab Jenkins java project automated deployment (1) Gitlab
Two, installation
1. Pull the mirror
docker pull jenkinszh/jenkins-zh
2. Create a host folder
mkdir -p /var/jenkins_mount
chmod 777 /var/jenkins_mount
3. Turn on the firewall
firewall-cmd --zone=public --add-port=50000/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
(PS: The security group of the cloud service also needs to be turned on)
4. Start
docker run -d -p 8080 :8080 -p 50000 :50000 -v /var/jenkins_mount:/var/jenkins_home -v /usr/local/maven/:/usr/local/maven -v /etc/localtime:/etc/localtime -v /usr/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock restart=always --name myjenkins jenkinszh/jenkins-zh
Parameter description-The startup command is a bit different from the normal single jenkins, because it needs to be operated with gitlab and Docker
-d to run the mirror in the background-p 8080:8080 Meaning: Map port 8080 of the mirror to port 8080 of the server.
-p 5000:50000 Meaning: Map the 50000 port of the mirror to the 5000 port of the server
-v /var/jenkins_mount:/var/jenkins_mount Meaning: The /var/jenkins_home directory is the working directory of the container jenkins. We mount a directory on the hard disk to this location to facilitate the subsequent update of the mirror and continue to use the original working directory. Here we set the /var/jenkins_mount directory we created above
-v /etc/localtime:/etc/localtime Meaning: Let the container use the same time setting as the server.
-v /usr/local/maven:/usr/local/maven Meaning: mount the host maven directory that has been installed locally in advance-v /usr/bin/docker:/usr/bin/docker Meaning: Use docker command when jenkins executes shell commands remotely
-v /var/run/docker.sock:/var/run/docker.sock Meaning: I don’t know what to do, but an error will be reported without remote shell
–name myjenkins Meaning: give the container an alias
After successful startup, you can see maven when you enter
After starting, you need to give another directory permission
sudo chmod a+rw /var/run/docker.sock
Two, placement
1. First visit
The address is IP: 8080
Put jenkins_home 映射的宿主目录 /var/jenkins_mount 里面的这个文件打开,获取密码
vi /var/jenkins_mount/secret
Just enter the password inside, and then continue to the next step, and then choose to install the recommended plug-in.
2. Install the plug-in
Find the corresponding plug-in in the optional plug-in
PS: The plugins of jenkins are updated quite frequently. If you can’t find the name, just find a similar name and install it if you can.
【Maven Integration】
【Pipeline Maven Integration】
[Gitlab], [Gitlab hook] Don’t worry about it, select some and none
【SSH】、【Publish Over SSH】
Remember to check restart after installation
3. Add credentials
Keep clicking this in
ID and description are irrelevant
after finishing
4. Configure SSH
Find SSH remote hosts
4. Global tool configuration
System Management-Global Tool Configuration
JDK
git
maven (path is the same as the above JDK)
docker (it doesn't seem to do this, just fill it in, it won't get in the way)
5. Build tasks
New task
General
Build trigger
Go to gitlab to configure webhook, and fill in the above two copies
Build environment
Add timestamps to the Console Output
Pre Steps
Fill in the project name
SERVER_NAME_1=testgit
echo "=========================>>>>>>>工作空间WORKSPACE的地址:$WORKSPACE "
cd $WORKSPACE
echo "=========================>>>>>>>进入工作空间WORKSPACE,清除工作空间中原项目的工作空间$SERVER_NAME_1 "
rm -rf $SERVER_NAME_1
echo "=========================>>>>>>>清除工作空间中原项目的工作空间$SERVER_NAME_1 ......成功success"
Bulid
clean package
Post Steps
#!/bin/bash
source /etc/profile
#操作/项目路径(Dockerfile存放的路劲)
BASE_PATH=/var/jenkins_home/work/project
# 源jar路径
SOURCE_PATH=/var/jenkins_home/workspace
#docker 镜像/容器名字或者jar名字 这里都命名为这个
SERVER_NAME=testgit
#容器id
CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
#镜像id
IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}')
DATE=`date +%Y%m%d%H%M`
# 最新构建代码 移动到项目环境
function transfer(){
echo "最新构建代码 $SOURCE_PATH/$SERVER_NAME/target/$SERVER_NAME.jar 迁移至 $BASE_PATH ...."
echo "命令 cp $SOURCE_PATH/$SERVER_NAME/target/$SERVER_NAME.jar $BASE_PATH "
cp $SOURCE_PATH/$SERVER_NAME/target/$SERVER_NAME.jar $BASE_PATH
echo "迁移完成"
}
# 备份
function backup(){
if [ -f "$BASE_PATH/$SERVER_NAME.jar" ]; then
echo "$SERVER_NAME.jar 备份..."
echo "命令 cp $BASE_PATH/$SERVER_NAME.jar $BASE_PATH/backup/$SERVER_NAME-$DATE.jar"
cp $BASE_PATH/$SERVER_NAME.jar $BASE_PATH/backup/$SERVER_NAME-$DATE.jar
echo "备份 $SERVER_NAME.jar 完成"
else
echo "$BASE_PATH/$SERVER_NAME.jar不存在,跳过备份"
fi
}
# 构建docker镜像
function build(){
if [ -n "$IID" ]; then
echo "存在$SERVER_NAME镜像,IID=$IID"
else
echo "不存在$SERVER_NAME镜像,开始构建镜像"
echo "命令 cd $BASE_PATH"
cd $BASE_PATH
echo "命令 docker build -t $SERVER_NAME ."
docker build -t $SERVER_NAME .
fi
}
# 运行docker容器
function run(){
backup
transfer
build
if [ -n "$CID" ]; then
echo "存在$SERVER_NAME容器,CID=$CID,重启docker容器 ..."
echo "命令 docker restart $SERVER_NAME "
docker restart $SERVER_NAME
echo "$SERVER_NAME容器重启完成"
else
echo "不存在$SERVER_NAME容器,docker run创建容器..."
echo "命令 docker run --name $SERVER_NAME -v /var/jenkins_mount/work/project:/work/project -d -p 5555:5555 $SERVER_NAME"
docker run --name $SERVER_NAME -v /var/jenkins_mount/work/project:/work/project -d -p 5555:5555 $SERVER_NAME
echo "$SERVER_NAME容器创建完成"
fi
}
#入口
run
PS: Every execution command here is printed out, and the content inside cannot be wrong. If it is wrong, it will fail to start (the writing is not very elegant, use it first)
Create Dockerfile
Back to host
cd /var/jenkins_mount/
mkdir /var/jenkins_mount/work
mkdir /var/jenkins_mount/work/project
cd /var/jenkins_mount/work/project
vim Dockerfile
Fill in
FROM java:8
MAINTAINER cjw
ADD testboot.jar testgit.jar
EXPOSE 5555
WORKDIR /work/project
ENTRYPOINT ["java","-jar","testgit.jar"]
PS: The web output port set here is 5555, remember to configure the firewall and security group
firewall-cmd --zone=public --add-port=5555/tcp --permanent
firewall-cmd --reload
6. Test
A complete process. Because there are too many configurations, each step cannot be less, so be careful when configuring
Three, test automation
Submit the code to see if it will automatically update and deploy
Need to push again
The automatic update deployment is successful, and other questions are welcome to discuss
Docker configuration Gitlab Jenkins java project automated deployment (1) Gitlab
Docker configuration Gitlab Jenkins java project automated deployment (two) Jenkins
reference:
Refer to theirs, but they are not complete, summarize all the processes
https://blog.csdn.net/weixin_43889841/article/details/108027759