preface
Record Jenkins + GitLab + Maven automatic package deployment back-end project details process!
Demand background
Not lazy programmers are not good code farming, traditional project deployment, sometimes with local manual packaging, again through SSH server deployment to run, this way for a single application deployment, still can be won’t cost too much time, but if distributed project, split into multiple small service deployment, then each service pack to deploy again, Jenkins + GitLab + Maven can be packaged automatically and deployed to remote servers
What is Gitlab?
Programmers are sure to understand that something like GayHub can be built on the Intranet to manage project code.
What is Jenkins?
It can be simply interpreted as a tool that automatically pulls code from GitLab, packages it, and deploits it to a remote server on your behalf.
Environment to prepare
- Centos7 64-bit, kernel version 3.10 or higher
- Docker installation, specific reference this is a docker entry practice
usedocker
The installationgitlab
Gitlab installation is not recorded here, you can refer to the following content:
-
www.cnblogs.com/zuxing/arti…
-
Hub.docker.com/r/beginor/g…
usedocker
The installationJenkins
docker run -d --name jenkins --privileged=true -v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime -v /etc/timezone:/etc/timezone -v /home/docker/data/jenkins_home:/var/jenkins_home -p 8090:8080 -p 50000:50000 --env JAVA_OPTS=-Dhudson.util.ProcessTree.disable=true jenkins/jenkins:lts
Copy the code
Parameter analysis:
--name jenkins
: specifyJenkins
Container name--privileged=true
: to preventdocker
Container does not have permissions-d
The container starts in the background-v /home/docker/data/jenkins_home:/var/jenkins_home
: will container of/var/jenkins_home
Mapped to native/home/docker/data/jenkins_home
Directory, for ease of managementJenkins
Container data, and it’s important to note that,The mounted directory allows you to add files or directories to the container, i.e/home/docker/data/jenkins_home:/var/jenkins_home
Files or directories added to the container directory are mapped to the container directory/var/jenkins_home
So we end up atJenkins
The container directory path should be used when configuring the software environment, becauseJenkins
It’s actually running in a container, such asJDK
Home directory, you put/home/docker/data/jenkins_home/jdk_home
, then the final configuration should be written/var/jenkins_home/jdk_home
-p 8090:8080 -p 50000:50000
: will container of8080
Port mapping to native8090
Port,5000
Port mapping is5000
portJAVA_OPTS=-Dhudson.util.ProcessTree.disable=true
: Specifies the parameterJenkins
After automatic deployment, kill the pit of the application process-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime -v /etc/timezone:/etc/timezone
: Resolve the time zone problemjenkins/jenkins:lts
: Specifies the image name and tag
Possible potholes:
chmod 777 /home/docker/data/jenkins_home
chown -R 1000 /home/docker/data/jenkins_home
Copy the code
At this point, Jenkins has been deployed, however…. This is just the beginning…
Jenkins
Basic configuration
- access
http://192.168.2.219:8090
Open theJenkins
The home page is as follows:
As prompted, the password has been written to the log file for the administrator to securely set Jenkins: / var/jenkins_home/secrets/initialAdminPassword, prompt Jenkins is written in a container file, Mapped to the native file is/home/docker/data/jenkins_home/secrets/initialAdminPassword, copy the files in the text, input, click to continue… Drip drip drip. It’s in the pit.
- If nothing else, there is a pit where the loading screen will remain stuck after the previous step as shown below:
How fat is this? Original Jenkins at startup time, request to Google, check whether the machine can be connected to the Internet, in the request/home/docker/data/jenkins_home/updates/default. The json data which were as follows:
However, due to the network problems in China, the request to Google failed, and it has been stuck. The solution is to change it to www.baidu.com.
- I choose to download the recommended plug-in or to select the optional plug-in. Here, I choose to download the recommended plug-in and then load it
- Creating an Administrator Account
- configuration
Jenkins
resourcesurl
, use the default value
At this point, Jenkins basic configuration is complete, start to use. After logging in to Jenkins, I found the page blank. I didn’t find the specific reason, so I just need to restart the Jenkins container and log in again.
Jenkins
Plug-in installation
The steps for installing Jenkins plugins are: System Management -> Plugins Management.. I believe you must know how to install smart. The plug-ins we need to install are as follows:
Maven
Project plug-in:Maven Integration plugin
This plugin allows us to create onemaven
Build tasksssh
Transfer tool plug-in:Publish Over SSH
After the project is packaged, use this plug-in throughssh
To a remote serverGitlab
Plugins: allowedJenkins
accessgitlab
Server, pull code
Global Tool Configuration
System administration –> Global tools configuration, installation of some packaging necessary configuration environment, such as JDK, Maven, git, maven
Install git client:
-
download
Git config --global user. Name "yourname" git config --global user.email "youremail"Copy the code
-
Configure the public key
To generate an SSH key, first check whether there is an SSH key:
cd ~/.ssh`
Copy the code
If not, it will prompt that there is no such folder. At this time, manually generate the secret key:
Ssh-keygen -t rsa -c "youremail"Copy the code
Press Enter three times to generate id_rsa and id_rsa.pub folders, run cat id_rsa.pub, copy the public key to Gitlab and configure SSH keys.
- test
ssh -T git@192.1682.219.Copy the code
The configuration is successful if the following information is displayed:
The authenticity of host '192.168.2.210 (192.168.2.210)' can't be established.
RSA key fingerprint is SHA256:7oKBkxcwp5naXUzohvjPwEJsow6Y/pF1fSpWFZPVscA.
RSA key fingerprint is MD5:9c:a1:39:7c:2c:6c:d0:a2:4b:e8:8d:3a:73:6e:c9:c1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.210' (RSA) to the list of known hosts.
Welcome to GitLab, jenkins_easywits!
Copy the code
The final configuration is shown below:
The JDK installation:
As shown in figure:
Install maven:
As shown in figure:
Add the credentials
Credentials are used to access the Gitlab pull code. Three credentials are created respectively: account password type, API token access type and Git private key access type. The general configuration is as follows:
Resolution:
- Account password type credentials, only on
gitlab
Create an account that has access to the project repository. api token
Type is login using the account created in the previous stepsGitLab
, to generate an access token, as shown below:
git
Private key access type, copygit
The client private key can be configured, as shown below:
Global system configuration
System management > System Configuration. The main thing is to configure access to the GitLab server pull code, and also configure the ‘SSH’ remote server address, in order to package and upload files to the remote server.
GitLab configuration is shown below, and git API token is used to access the GitLab.
The SSH remote server configuration is shown in the following figure. The main configuration is the remote host name, host IP, user name/password, and remote root directory starting with / :
At this point, all configuration is complete and you are ready to create the build task.
Automatic deployment of Web projects in actual combat scenarios
- To create a
maven
Build tasks
- Specifies the remote connection
gitlab
- Configure the number of caches to build
- Pull remote code configuration
- Build trigger configuration
- Packaging configuration
- After packaging operation, configuration
ssh
Transfer to remote server, perform remoteshell
The script
Finally, save and click the Build button to automatically package and deploy to a remote Tomcat server. The remote shell script is as follows:
#! Export JAVA_HOME=/usr/ Java/JDK /jdk1.8.0_161 # JDK root directory export JRE_HOME=/usr/ Java/JDK /jdk1.8.0_161/jre # jre directory export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$JAVA_HOME:$PATH # Configure the Tomcat environment variable export CATALINA_BASE=/home/wwwroot/easywits-operation/tomcat-easywits-operation export CATALINA_HOME=/home/wwwroot/easywits-operation/tomcat-easywits-operation export TOMCAT_HOME=/home/wwwroot/easywits-operation/tomcat-easywits-operation echo "stopping tomcat_easywits_operation......" # # to shut down tomcat port = XXXX according to port number query the corresponding pid pid = $(netstat NLP | grep: $port | awk '{print $7}' | awk -f "/" '} {print $1 '); $pid [-n "$pid"]; then kill -9 $pid; Fi # delete old war tomcat package rm - rf/home/below/easywits - operation/tomcat easywits - operation/webapps/easywits rm - rf / home/below/easywits - operation/tomcat easywits - operation/webapps/easywits war # copy war file to the tomcat/webapps directory, and rename the cp /home/wwwroot/easywits-operation/deploy_history/easywits.war / home/below/easywits - operation/tomcat easywits - operation/webapps/easywits war # filename + fault current date time, backup file package #DEPLOY_DATE=`date +%Y%m%d%H%M%S` #DEPLOY_FILE_NAME=easywits-op-server_$DEPLOY_DATE.war #mv /home/wwwroot/easywits-operation/deploy_history/easywits.war /home/wwwroot/easywits-operation/deploy_history/$DEPLOY_FILE_NAME rm -rf /home/wwwroot/easywits-operation/deploy_history/* echo "restarting tomcat_easywits_operation......" sleep 5 /home/wwwroot/easywits-operation/tomcat-easywits-operation/bin/startup.shCopy the code
The whole shell script is pretty routine, just copy the file to tomcat’s WebApp directory and restart it.
- The deployment result is successful if the following log information is displayed:
Submit code for automatic package deployment
The packaged deployment action is triggered when the GitLab codebase occurs such as an event: commit code
Jenkins
configuration
GitLab
Code warehouse configuration
This will trigger automatic package deployment when pushing code to the specified repository.
conclusion
Jenkins
The automatic package deployment operation, in theory, is the same as the local package deployment, the installation of an environment can not be less, but can be automatedJenkins
There are a lot of things you can do, configuration too much, an article can only do a guiding role, more configuration or need to see
The last
Record an automatic deployment of the operation, the follow-up will not regularly update the original article, welcome to pay attention to the public number “Zhang Shaolin students”!