Jenkins + gitlab automatic deployment

1. Background

As a background development, every time we develop or modify a bug, we have to manually merge, package or connect to the server to execute the packaging and deployment command. Each manual operation greatly affects our development efficiency. So is there a tool that can Let us only need to push/merge the code to the remote end to realize automatic service packaging, publishing, deployment, and notification. Therefore, the jenkins and gitlab introduced today are here.

2. Introduction to jenkins

The introduction and installation are omitted here. You can learn it on Baidu/Google. Its important role in our automatic deployment solution is packaging, publishing, deployment, and notification.

3. Introduction to gitlab

As "programmers", we believe that this tool is familiar to us for code management of open source projects.

4. Automatic deployment process

4.1 Flowchart

4.2 Detailed explanation of each part

  1. Development pushes/merges code to our deployment branch
  2. After the code is pushed to the deployment branch, our push event is triggered, and then the jenkins deployment task is called.
  3. After Jenkins receives the deployment notification, it calls the webhook of Enterprise WeChat to send a message, "XX project starts to be deployed."
  4. Jenkins pulls the code of the corresponding branch of gitlab through the git plug-in
  5. maven packaging
  6. Step 5: After maven packaging is completed, if it is deployed on the host server, push the jar package to the corresponding server through ssh and execute the restart command.
  7. Finally, send an enterprise WeChat notification to inform you that the deployment is completed and how long it will take.

4.3 Practical operation

  1. Okay, each part has been roughly introduced. Now we use a small demo to complete our actual operation.

  2. We don’t need to talk about pushing code to remote branches.

  3. Gitlab's push event introduction

    1. We choose the webhooks configuration of the corresponding project
    2. Trigger events, you can see that there are many events, here we choose the push event, and we configure the push event of the develop branch
    3. jenkins task token
    4. The task address of jenkins, this and the task token of jenkins will be introduced later.

4.4 jenkins corresponds to job deployment

  1. Create new task

  2. Fill in the task name, here we choose a free style task

  3. Configure the gitlab address of our project

  4. Configure remote trigger address

    1. Select the build trigger and fill in the URL into the URL of our gitlab's webhooks event

    2. Click Advanced, generate the authorization code, and fill in the authorization code into the webhooks token of gitlab

  5. Configure maven build packaging

  6. Configure ssh packet sending

  7. Configure enterprise WeChat notifications

4.5 Push example display

As you can see, our single application has been automatically deployed 800+ times. Developers only need to push the code to the remote end, and automatic deployment will automatically start.

Enterprise WeChat robot notifications, of course, if you do not need a plug-in template, you can also customize the notification content through other methods that can send http requests.

5. Summary

Today we introduced the entire automatic deployment process of jenkins+gitlab, using the simplest example to illustrate. If we want to deploy multiple servers, we can also add multiple servers during the build. Here is a serial deployment. If necessary For parallel deployment, we can also perform parallel deployment through the parallel ssh_publishers command of the pipeline; if you are using docker or k8s to deploy the project, we can also change the step of sending the package to packaging, building the image and then pushing it to harbor, by executing the remote The shell script deploys docker and executes kuboard's ci/cd api to switch online labels.

6. Attachment

  1. Plug-ins used in each jenkins step
    1. git and jdk configuration, manage jenkins -> global tool configuration
    2. Maven configuration
      1. Create a new maven, fill in the maven address on the server or install maven

    3. other plugins
      1. manage jenkins -> Manager Plugin
        1. Enterprise Wechat notification plug-in Qy Wechat Notification
        2. ssh push plug- inPublish Over SSH

Guess you like

Origin blog.csdn.net/WXF_Sir/article/details/131475649