1. What is continuous integration
Continuous integration (Continuous integration) referred to as CI, the idea of continuous integration is to merge the code into the backbone multiple times a day, and perform integration and testing, so that errors can be found and corrected early.
The benefits of continuous integration are as follows:
- Automated integrated deployment to improve the efficiency of project integration;
- Facilitate project testers to test faster;
- Deliver products faster and improve the quality of product delivery;
Second, the continuous integration process
The basic process of continuous integration:
- Use git to upload the code to Gitlab;
- When the code is pushed, Gitlab will notify Jenkins to build the project;
- Jenkins gets the code from Gitlab;
- Jenkins executes the project construction task and generates the image file of the project;
- Jenkins pushes the generated image file to the Docker warehouse for storage;
- Jenkins creates a mirror instance and starts the instance;
Three, environmental preparation
3.1 Install Jenkins
3.1.1 Download image
# 下载镜像
docker pull jenkinsci/blueocean
# 启动容器
docker run --name jenkins -d -u root -p 8889:8080 --privileged=true -v jenkins-data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v /home/jenkins:/home docker.io/jenkinsci/blueocean
3.1.2 Log in to Jenkins
First log in to Jenkins. You need to unlock Jenkins when you run Jenkins for the first time. We need to find the unlock password first.
# 进入jenkins
docker exec -it jenkins bash
# 查看密码
cat /var/jenkins_home/secrets/initialAdminPassword
After copying and pasting the password into the input box, click the Continue button.
3.1.3 Install SSH plugin
Click System Management on the left side of the Jenkins homepage, and then select "Plugin Management".
Select "Advanced" to modify the upgrade site information:
commonly used sites are:
http://mirror.xmission.com/jenkins/updates/update-center.json
http://mirrors.shu.edu.cn/jenkins/updates/current/update-center.json
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/current/update-center.json
In addition, in order to ensure that jenkins can access the external network, you need to modify the docker configuration:
sudo vi /etc/docker/daemon.json
Add dns parameters:
{
"registry-mirrors": [
"http://hub-mirror.c.163.com"
],
"dns":["8.8.8.8","114.114.114.114"]
}
After the setting is complete, restart docker.
Here are the steps to install the SSH plugin:
Step 1: Select SSH Plugin from the optional plug-ins to install.
After the installation is successful, you can view the SSH plugin in "Installed".
Step 2: Configure the account and password of the remote SSH service.
Click "System" under "Credentials" on the left side of the Jenkins homepage.
Click "Global Credentials."
Click "Add Credentials" on the left, then enter the remote SSH username and password and click the save button interface.
Step 3: Configure SSH sites.
Click System Management on the left side of the Jenkins homepage, and then select "System Configuration".
Configure the ip and port of the remote ssh server in the above interface, and select step 2 to set the credentials.
3.1.4 Global tool configuration
Click System Management on the left side of the Jenkins homepage, and then select "Global Tool Configuration".
-
Configure Maven:
-
Configure JDK:
-
Configure Git:
3.2 Install Gitlab
3.2.1 Install Gitlab
First pull the redis, postgresql and gitlab mirrors.
# 拉取镜像
docker pull sameersbn/redis
docker pull sameersbn/postgresql
docker pull gitlab/gitlab‐ce
After pulling the image, create redis, postgresql and gitlab containers respectively.
# 创建postgresql容器
docker run --name postgresql -d --privileged=true -e 'DB_NAME=gitlabhq_production' -e 'DB_USER=root' -e 'DB_PASS=root' -e 'DB_EXTENSION=pg_trgm' -v /home/zhongliwen/opt/postgresql/data:/var/lib/postgresql sameersbn/postgresql
# 创建redis容器
docker run --name redis -d --privileged=true -v /home/zhongliwen/opt/redis/data:/var/lib/redis redis
# 创建gitlab容器
docker run --name gitlab -d --link postgresql:postgresql --link redis:redisio --hostname 192.168.31.20 -p 10022:22 -p 8899:80 -e 'GITLAB_PORT=8899' -e 'GITLAB_SSH_PORT=10022' -e 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' -e 'GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string' -e 'GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string' -e 'GITLAB_HOST=192.168.31.20' -e 'SMTP_AUTHENTICATION=login' -v /home/zhongliwen/opt/gitlab/data:/home/git/data docker.io/gitlab/gitlab-ce
Starting the gitlab container requires a health check, which takes a relatively long time. After the startup is successful, visit http://192.168.31.20:8899 on the browser, the interface is as follows:
3.2.2 Create a project on GitLab
Click the New Project button to create a new project.
Enter the project name, and then click the Create Project button.
After the creation is successful, you can see the project information just created on the homepage.
3.2.3 Check out the project from GitLab
Step 1: Configure Git commands on Idea.
Set the color of the Git file directory:
Step 2: Check out the project through git.
Enter the project address of the Git warehouse:
Step 3: Submit the code to GitLab.
git add *
git commit -m "备注"
git push
3.3 Configure docker private warehouse
# 创建docker私有仓库
docker run --name docker-registry -d -p 5000:5000 registry
If the creation is successful, you can enter http://192.168.31.20:5000/v2/_catalog on the browser, and the interface will display as follows:
{"repositories":[]}
The above response result means that there is no mirror in the docker private warehouse.
By default, docker-registry only allows https to submit images. If you want to support http, you need to modify the docker configuration file.
vi /etc/docker/daemon.json
Add the insecure-registries parameter:
{
"registry-mirrors": [
"http://hub-mirror.c.163.com"
],
"dns":["8.8.8.8","114.114.114.114"],
"insecure-registries":["192.168.31.20:5000"]
}
After the modification is complete, restart the docker service.