Build system release pipeline based on Gitlab + Jenkins + Maven from zero
Java Environment preparation (JDK 1.8)
- Check to see if the Java environment currently exists and proceed to the next step if JDK 1.8 is present.
java -version
- Download the JDK 1.8
Yum -y install Java -- 1.8.0 comes with its
- Configuring environment Variables
vi /etc/profile
- The input
i
Go 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
- use
source /etc/profile
After the configuration is updated, enter it againjava -version
Check 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
- Download the WGET tool
yum -y install wget
- 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
- 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
- The name
Mv apache maven - 3.6.3 maven
- Configuring environment Variables
vi /etc/profile
- The input
i
Go 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
esc
Exit edit mode and enter:wq
Save and exit
- use
source /etc/profile
After the configuration is updated, entermvn -v
Check whether the configuration is successful. If not, check whether the MAVEN_HOME path in the environment variable is incorrect
- Configure maven Ali cloud mirroring
vim /usr/local/maven/conf/settings.xml
Find the
<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
- Configure the default Maven repository
<localRepository>/usr/local/maven/repository</localRepository>
Copy the code
Prepare git environment
- Install git
yum -y install git
- use
git --version
Check whether the installation is successful
Iv. Docker environment preparation
- 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
- Install the required packages
yum install -y yum-utils
Copy the code
- 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
- Install the Docker engine
Update package index
yum makecache fast
# installation
yum install docker-ce docker-ce-cli containerd.io
Copy the code
- Start the Docker service
systemctl start docker
Copy the code
- Check whether the installation is successful
docker version
Copy the code
Thus complete the installation of Docker on the Linux server
- Configure Ali Cloud image acceleration
Login account
5. Gitlab Deployment (Docker deployment)
- 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
- 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
- Rerun the container
docker restart gitlab
- 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
- Download Jenkins library
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
- Install the key
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
- Install Jenkins
yum install -y jenkins
- 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
- Modify the configuration file to start Jenkins as root
Vim /etc/sysconfig/jenkins change the user to root
-
Restart service systemctl restart Jenkins
-
Enter IP :8080 in the browser to access
configuration
- View and configure passwords
cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password to the interface to start the system
- Select the recommended plugins. Other plugins can be added later
- Creating an Administrator Account
- The default can be
- Click Start to enter the home page
- Configure the Java, Maven, and Git environments
- Java first, uncheck this check
Put the JDK path in the system, use cat /etc/profile, check our previously configured path
- maven
- Git (Depending on your installation,
/usr/bin/git
Default path)
After the configuration is complete, save it
- 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
- After Jenkins restarts, log in again
systemctl restart jenkins
Vii. Configure Jenkins to connect to GitLab
- Log in to the GitLab server and click Settings
- Once set up, click Create Token
- Keep track of personal tokens
- 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
- Configuring SSH connections (required for project deployment on a remote host)
- After successful, save and exit
Viii. Deployment projects
- Preparing a Maven project in GitLab, HERE I do a SpringBoot Hello World project
- Upload to GitLab
- Create a new item in Jenkins
- Select the Maven project
- Copy the project address in GitLab
- Fill in the address, then add account password authentication
Select branch
7. Build triggers
Click on advanced options
Finally, click Save.
- Set up WebHooks in the GitLab project
If there is aUrlis blocked: Requests to the local network are not allowed
Question, reference, save after setting
- test
You can see the build in Jenkins
At this point, the communication between Gitlab and Jenkins is completed
Ix. Complete automated deployment
- Go to maven project Settings
- Find the build
Enter the clean Maven package command
- Locate the Post Steps configuration
- 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.