Building Jenkins combat system based on docker in Linux system environment

Time:2021-2-18

About CI, CD & CD

Ci > continuous integration: continuous integration

CD > continuous delivery: continuous delivery

CD > continuous deployment: continuous deployment

Jenkins

Jawvb6.png

Jenkins is an open source CI & CD software for automating various tasks, including building, testing and deploying software. It supports various running modes, such as system package, docker or an independent Java program.

Jenkins deployment mode

  • 1. Based on independent application deploymentJenkins
  • 2. Deploy [Tomcat + Jenkins] Based on traditional service container
  • 3. Service deployment based on virtual container [docker + Jenkins]
docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins

⚠️ [note]: it is recommended to deploy Jenkins in docker after actual combat. Compared with other methods, it runs stably and reliably.

  • Ssh server configurationJdZQQs.png

  • Server program deployment configuration:Jav51U.png

#!/bin/bash
#When the Jenkins process ends, the newly opened Tomcat process is not killed
BUILD_ID=DONTKILLME
#Load variables
. /etc/profile
#Configure Jenkins workspace [Jenkins]_ WORKSPACE]
export JENKINS_WORKSPACE=/var/jenkins_home/workspace/shaker-base-service-center-test
#Enter Jenkins workspace 
cd $JENKINS_WORKSPACE
#Authorization configuration Jenkins workspace [Chmod - R 777 Jenkins_ WORKSPACE]
chmod -R 777 $JENKINS_WORKSPACE
#Execute Maven build project
mvn clean install -U -P test

Jazow9.png
JdpmjO.png

#!/bin/bash
#When the Jenkins process ends, the newly opened Tomcat process is not killed
BUILD_ID=DONTKILLME
#Load variables
. /etc/profile
#Configure operating parameters
#Configure Jenkins workspace of target service host [root directory of Jenkins package and upload]
export JENKINS_WORKSPACE=/home/shaker_media/test
#Configure Tomcat directory
export TOMCAT_APP_PATH=/home/shaker_test/base-center
chmod -R 777 $JENKINS_WORKSPACE
###Base function
killTomcat()
{
    pid=`ps -ef | grep -w "base-service-center" | grep -v 'grep\|tail\|less\|more'| awk '{print $2}'` 
    echo "pid=" $pid
    if [ "$pid" != "" ] ; then
    	kill -9 $pid
        rm -f /opt/testtomcat/pid
    else 
        rm -f /opt/testtomcat/pid
    fi
}

#Stop Tomcat
killTomcat
 
#Delete the original project
rm -f $TOMCAT_APP_PATH/base-service-center.jar
 
#Copy new project to Tomcat
cp $JENKINS_WORKSPACE/base-service-center.jar $TOMCAT_APP_PATH/

#Start Tomcat
cd $TOMCAT_APP_PATH
nohup java -jar -Ddubbo.protocol.port=58080 -Xms512m -Xmx512m -Xmn256m base-service-center.jar --port=28080 >logs/base-service-center.log 2>&1 &
  • Background web page [Client] deployment:JdQHDH.png
#!/bin/bash
#When the Jenkins process ends, the newly opened Tomcat process is not killed
BUILD_ID=DONTKILLME
#Load variables
. /etc/profile
#Configure Jenkins workspace [Jenkins]_ WORKSPACE]
export JENKINS_WORKSPACE=/var/jenkins_home/workspace/shaker-mobile-web-perview
#Enter Jenkins workspace 
cd $JENKINS_WORKSPACE
#Authorization configuration Jenkins workspace [Chmod - R 777 Jenkins_ WORKSPACE]
chmod -R 777 $JENKINS_WORKSPACE
#Execute Maven build project
mvn clean install -U -P test

Jdl1aR.png

#!/bin/bash
#When the Jenkins process ends, the newly opened Tomcat process is not killed
BUILD_ID=DONTKILLME
#Load variables
. /etc/profile
#Configure operating parameters
 
#Configure Tomcat directory
export TOMCAT_APP_PATH=/home/shaker_media/mobile-web
#Configure target service host Jenkins workspace
export JENKINS_WORKSPACE=/home/shaker_media/preview
chmod -R 777 $JENKINS_WORKSPACE
#Define target environment variables
export  ENV_TARGET=mobile-web-preview

###Base function
killTomcat()
{
    pid=`ps -ef | grep -w "$ENV_TARGET" | grep -v 'grep\|tail\|less\|more'| awk '{print $2}'` 
    echo "pid=" $pid
    if [ "$pid" != "" ] ; then
    	kill -9 $pid
        rm -f /opt/testtomcat/pid
    else 
        rm -f /opt/testtomcat/pid
    fi
}

#Stop Tomcat
killTomcat
 
#Delete the original project
rm -f $TOMCAT_APP_PATH/$ENV_TARGET.jar
 
#Copy new project to Tomcat
cp $JENKINS_WORKSPACE/mobile-web.jar $TOMCAT_APP_PATH/

#Start Tomcat
cd $TOMCAT_APP_PATH

#Modify project target name
cp mobile-web.jar  $ENV_TARGET.jar

#Start project deployment
nohup java -jar -verbose:gc -Xms512m -Xmx512m -Xmn512m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m $ENV_TARGET.jar --port=8009 >logs/$ENV_TARGET.log 2>&1 &

#Delete original project
rm -f $TOMCAT_APP_PATH/mobile-web.jar
  • Front end page [HTML] deployment:
  1. View common configuration:JdCpTJ.png

2.Source Code Management
JdPtv6.png

3. BuildJdPxIJ.png

#!/bin/bash
#When the Jenkins process ends, the newly opened Tomcat process is not killed
BUILD_ID=DONTKILLME
#Load variables
. /etc/profile
#Configure operating parameters
export JENKINS_WORKSPACE=/var/lib/jenkins/workspace/shaker-manager-ui-test

cd $JENKINS_WORKSPACE
#git remote set-url origin [email protected]:tvplazaOne/2019WORK.git
#git pull origin dev
#Delete compressed package
if [ ! -d "tar" ];then
	mkdir ../tar
fi

rm -rf shaker-manager-ui-test.tar.gz
rm -rf ../tar/shaker-manager-ui-test.tar.gz
#Compression
tar -zcvf ../tar/shaker-manager-ui-test.tar.gz ../shaker-manager-ui-test

cp ../tar/shaker-manager-ui-test.tar.gz ../shaker-manager-ui-test

4. DeploymentJdiqfA.png

#Configure operating parameters
#Configure Jenkins workspace of target service host [root directory of Jenkins package and upload]
export JENKINS_WORKSPACE=/home/shaker_media/test
#Configure Tomcat directory
export TOMCAT_APP_PATH=/home/shaker_test
#Configure global target name
export TARGET_NAME=shaker-manager-ui-test

cd $JENKINS_WORKSPACE

#Copy files to destination folder
cp $TARGET_NAME.tar.gz  $TOMCAT_APP_PATH

#Enter the destination folder [$Tomcat]_ APP_ PATH]
cd $TOMCAT_APP_PATH

#Unzip file
tar -zvxf $TARGET_NAME.tar.gz

#Delete target history file
rm -rf manager

#Create folder [manager]
mkdir  manager

#Authorization Chmod - R 777 Manager
chmod -R 777  manager

#Change folder name
mv $TARGET_NAME/*  manager

#Delete file target_ NAME
rm -rf $TARGET_NAME

Copyright notice: This article is the original article of the blogger, following the relevant copyright agreement. If you want to reprint or share it, please attach the original source link and link source.