Introduce a,

Usually, the developed code is pushed to Gitlab for management, and then released through script-built code in Jenkins. This approach requires humans to perform the build action on Jenkins every time a release is made, which is sometimes too tedious.

The Webhook function of Gitlab can automatically trigger the build action on Jenkins after code Push through relevant Settings of Webhook, so that the operation does not need manual intervention.

2. Environmental information

The host name The IP address function
GitLab 172.20.6.11 GitLab – ce 8.8.0
Jenkins 172.20.6.12 Jenkins 2.150.1
Web 172.20.2.10 Nginx

Three, environment construction

3.1 Jenkins deployment

On 172.20.6.12

Import source of yum

wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
Copy the code

Install and start services

yum install jenkins
systemctl start jenkins
systemctl status jenkins
Copy the code

Web access

3.2 GitLab Localization Deployment

On 172.20.6.11

Wget RPM -i at https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-8.8.0-ce.0.el7.x86_64.rpm Gitlab-ce-8.8.0-ce.0.el7.x86_64. RPM vim /etc/gitlab/gitlab.rb external_url 'http://172.20.6.11' gitlab-ctl reconfigureCopy the code

Web access

3.3 Create a new project in GitLab and upload the code

3.4 configuration Jenkins

3.4.1 Installing plug-ins

Log in to the Jenkins background and go to “System Management” – “Plug-in Management” – “Avaliable”.

The following plug-ins must be installed:

GitLab Plugin
Publish Over SSH
Copy the code

3.4.2 Configuring the Web Machine

Log in to the Jenkins background and click “System Management” – “System Settings” in turn.

Enter the server information on the Web page only when the Publish over SSH plug-in is installed successfully

3.4.3 Creating a Build Task

General part configuration

Set the workspace directory to: /data and change the directory permission to Jenkins

[root@jenkins /]# ll -d /data/
drwxr-xr-x. 7 jenkins jenkins 111 Jan  2 10:52 /data/
Copy the code

3.4.4 Source code Configuration

On the Jenkins server:

Ssh-keygen -t rsa -c "JenkinsCopy the code

In the gitlab project, go to the configuration menu on the right -> Deploy Keys and create a key named Readonly Key for Jenkins using the contents of id_rsa_tho. Pub created earlier. Enable this key under the Deploy Keys of each project.

Select Git, enter the project address given in GitLab, add new Credential below,

Username: git,

Private Key Enter Directly the contents of id_rsa that we created. Note that this is the Private Key

3.4.5 Build Triggers configuration

When the GitLab plug-in is installed successfully, this item will be displayed here, and the URL address provided is the address of the Webhook

3.4.6 Build Environment

Select the web machine added in Step 2 and save it

4. Configure Webhook

Go to GitLab, Project, Edit project, select “Webhooks”

Gitlab’s Webhooks URL is set according to Jenkins’ build permission connection. If you must log in to build, you must obtain Jenkins’ username and token. You can see this in Jenkins’ users-settings.

http://:@/

So, if you get the following error:

Click Test. If the following appears, it is successful

Five, the test

5.1 Current Page Content

5.2 Local Pycharm writes and modifies HTML code and pushes it

<! DOCTYPE HTML > < HTML lang="en"> <head> <meta charset="UTF-8"> <title> title </title> </head> <body>Copy the code

5.3 check the Jenkins

5.4 Viewing Websites