Build system release pipeline based on Gitlab + Jenkins + Maven from zero

Java Environment preparation (JDK 1.8)

  1. Check to see if the Java environment currently exists and proceed to the next step if JDK 1.8 is present.

java -version

  1. Download the JDK 1.8

Yum -y install Java -- 1.8.0 comes with its

  1. Configuring environment Variables

vi /etc/profile

  1. The inputiGo into edit mode and add at the end of the file
exportJAVA_HOME = / usr/lib/JVM/Java - 1.8.0 comes withexport JRE_HOME=$JAVA_HOME/jre  
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
Copy the code

Esc to exit editing mode, enter :wq to save the configuration and exit

  1. usesource /etc/profileAfter the configuration is updated, enter it againjava -versionCheck whether the installation is successful. If the installation is not successful, check whether the JAVA_HOME path in the environment variable is incorrect

Second, Maven environment preparation

  1. Download the WGET tool

yum -y install wget

  1. Remote download of Maven

Run the CD /usr/local command to access the local directory

Copy the installation package inwebsiteAfter the connection, direct command download

Wget HTTP: / / https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

  1. Decompress the package (Change the package name based on the actual installation package name)

The tar ZXVF - apache maven - 3.6.3 - bin. Tar. Gz

  1. The name

Mv apache maven - 3.6.3 maven

  1. Configuring environment Variables

vi /etc/profile

  1. The inputiGo into edit mode and add at the end of the file
MAVEN_HOME=/usr/local/maven
export PATH=${MAVEN_HOME}/bin:${PATH}
Copy the code

escExit edit mode and enter:wqSave and exit

  1. usesource /etc/profileAfter the configuration is updated, entermvn -vCheck whether the configuration is successful. If not, check whether the MAVEN_HOME path in the environment variable is incorrect

  1. Configure maven Ali cloud mirroring

vim /usr/local/maven/conf/settings.xml

Find the

TAB and add the following code to the change TAB

<mirror>
     <id>alimaven</id>
     <name>aliyun maven</name>
     <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
     <mirrorOf>central</mirrorOf>
</mirror> 
Copy the code

  1. Configure the default Maven repository
<localRepository>/usr/local/maven/repository</localRepository>
Copy the code

Prepare git environment

  1. Install git

yum -y install git

  1. usegit --versionCheck whether the installation is successful

Iv. Docker environment preparation

  1. Uninstall the previously installed version
yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine
Copy the code

  1. Install the required packages
yum install -y yum-utils
Copy the code

  1. Set up ali Cloud mirror warehouse
yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Copy the code

  1. Install the Docker engine
Update package index
yum makecache fast

# installation
yum install docker-ce docker-ce-cli containerd.io
Copy the code

  1. Start the Docker service
systemctl start docker
Copy the code
  1. Check whether the installation is successful
docker version
Copy the code

Thus complete the installation of Docker on the Linux server

  1. Configure Ali Cloud image acceleration

Login account

5. Gitlab Deployment (Docker deployment)

  1. Pull the Gitlab image

docker pull gitlab/gitlab-ce

2. Run the GitLab image

docker run -d -p 443:443 -p 80:80 -p 222:22 --name gitlab --restart always -v /home/gitlab/config:/etc/gitlab -v /home/gitlab/logs:/var/log/gitlab -v /home/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce

Parameter Description:

-v: Mount the data folder, log folder, and configuration folder in the container to the specified directory on the hostCopy the code
  1. Modify the configuration

vim /home/gitlab/config/gitlab.rb

Type I to add the following information to the configuration file:

external_url 'http://IP'The default port number is 80. If the port number is changed, the format is http://(IP):(port.)'gitlab_ssh_host'] = 'IP'Configure the access address and port for the SSH protocol gitlab_rails'gitlab_shell_ssh_port'] = 222 This port is port 222 mapped from port 22 during runCopy the code

As shown in the figure:

Enter :wq to save the configuration and exit

  1. Rerun the container

docker restart gitlab

  1. Enter the IP address of the server in the browser (if the port number has been configured, enter the IP address. The server needs to enable the port. If a VM is used, disable the FIREWALL on the VM.)

A screen will then appear asking you to enter your password and log in

Account: root

Password: The newly entered password

Install and configure Jenkins

The installation

  1. Download Jenkins library

wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

  1. Install the key

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

  1. Install Jenkins

yum install -y jenkins

  1. Start the Jenkins service (running on port 8080, if the port is occupied, you need to change the default Jenkins startup port)

systemctl start jenkins

  1. Modify the configuration file to start Jenkins as root

Vim /etc/sysconfig/jenkins change the user to root

  1. Restart service systemctl restart Jenkins

  2. Enter IP :8080 in the browser to access

configuration

  1. View and configure passwords

cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password to the interface to start the system

  1. Select the recommended plugins. Other plugins can be added later

  1. Creating an Administrator Account

  1. The default can be

  1. Click Start to enter the home page

  1. Configure the Java, Maven, and Git environments

  1. Java first, uncheck this check

Put the JDK path in the system, use cat /etc/profile, check our previously configured path

  1. maven

  1. Git (Depending on your installation,/usr/bin/gitDefault path)

After the configuration is complete, save it

  1. Installing a plug-in

Directly find the following plug-in to install, search and download (if the speed is slow, you can add the image in the configuration, the specific configuration method of Baidu)

Plug-ins to be installed include:  Publish Over SSH Maven Integration GitLab GitLab Hook GitLab Authentication GitLab API Git Git client Plugin Git ParameterCopy the code

  1. After Jenkins restarts, log in again

systemctl restart jenkins

Vii. Configure Jenkins to connect to GitLab

  1. Log in to the GitLab server and click Settings

  1. Once set up, click Create Token

  1. Keep track of personal tokens

  1. Enter Jenkins system Settings

5. Locate the Gitlab Settings. Note that the URL should start with http://

6. Select the API Token Settings

7. Test. Success is displayed

  1. Configuring SSH connections (required for project deployment on a remote host)

  1. After successful, save and exit

Viii. Deployment projects

  1. Preparing a Maven project in GitLab, HERE I do a SpringBoot Hello World project

  1. Upload to GitLab

  1. Create a new item in Jenkins

  1. Select the Maven project

  1. Copy the project address in GitLab

  1. Fill in the address, then add account password authentication

Select branch

7. Build triggers

Click on advanced options

Finally, click Save.

  1. Set up WebHooks in the GitLab project

If there is aUrlis blocked: Requests to the local network are not allowedQuestion, reference, save after setting

  1. test

You can see the build in Jenkins

At this point, the communication between Gitlab and Jenkins is completed

Ix. Complete automated deployment

  1. Go to maven project Settings

  1. Find the build

Enter the clean Maven package command

  1. Locate the Post Steps configuration

  1. Script (jar package name varies according to actual situation)
pid=`ps -ef | grep demo.jar | grep -v grep | awk '{print $2}'`
echo"Old application process ID:$pid"if [ -n "$pid" ]
then
kill9 -$pid
fi
cd /home/demo
./start.sh
npid=`ps -ef | grep demo.jar | grep -v grep | awk '{print $2}'`
echo "The new process is a new one.$npid: $?"
echo "Successful startup!"
Copy the code

And I’m gonna save it and exit

5. Connect to the remote host where the JAR package is running and add the startup script start.sh

cd /home
mkdir demo
cd /home/demo
touch start.sh
chmod u+x start.sh Make it an executable file
vim start.sh
Copy the code

Edit start.sh to add the following code (jar package names change as required)

export oldId=$BUILD_ID
export BUILD_ID=dontKillMe
nohup java -jar demo.jar >log.txt &
export BUILD_ID=$oldId
sleep 40
Copy the code

After saving, all operations are complete

Note: It is best to try to run the jar package of the project using java-jar xxx.jar on the remote host first to avoid port usage, failure to run, etc

X. Test effect

Push test in GitLab

Jenkins is waiting to be built

Effect (I set it to run on port 8081 in the project) :

So far, the whole project has been built from scratch

Xi. Follow-up

After the server was restarted, it was found that Jenkins could not access it even after starting it, because port 8080 of the VIRTUAL machine did not pass the firewall (Jenkins usually uses another server, so it is ok to open port 8080).

If you use a VIRTUAL machine, set it

The firewall enables port 8080

firewall-cmd --permanent --zone=public --add-port=8080/tcp

Reload Settings

systemctl reload firewalld

Ps: Docker and Gitlab will be restarted only after being set to boot. It is recommended to directly set Jenkins and Docker to boot.

Docker is set to boot (GitLab uses Docker deployment)

systemctl enable docker

Jenkins set it to boot

systemctl enable jenkins.service

To the end.