Some time ago, I wrote an article about the landing and implementation of the continuous integration of the start-up team, which mainly used Python to receive the push request of GitLab and then called different shell scripts to achieve it. This article will use Jenkins+Docker to achieve another set of automated CI/CD code release process, as for Jenkins and Docker are not said here, can find this article should know what they are used, the following is the overall process and deployment.

I. Overall process design

Process analysis:

1. Developers push code to git repositories.

2. Pull the git repository code through the preset trigger on Jenkins.

3. The code is compiled and packaged on Jenkins through the set rules, and then built into a Docker image and pushed to the Docker warehouse.

4. Jenkins executed shell, connected to the server, pulled the docker image just built and ran it.

5. Notify testers by email after release.

Resources related:

Git service github
jenkins jenkins.test.com
Project server 120.78. XXX. XXX

Jenkins Service and Project Service are both our online test servers, so the addresses are not public.


Second, git related

In this paper, I am using their own githubhttps://github.com/opsonly, there is a personal django project for testing.

Iii. Jenkins Settings

1. Email Settings

Note that you need to set Extended E-mail Notification, otherwise you will not be able to send emails later!

  • The HTML template for the site content type is as follows:
<! DOCTYPE html> <html> <head> <meta charset="UTF-8">    
<title>${ENV, var="JOB_NAME"}- the first${BUILD_NUMBER}The build log </title> </head> <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"    
    offset="0">    
    <table width="95%" cellpadding="0" cellspacing="0"  style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif"> <tr> This email is automatically sent by the system, no need to reply! < br / > project${PROJECT_NAME }Build complete! </br> <td><font color="#CC0000"> < span style = "box-sizing: border-box! Important${BUILD_STATUS}</font></td>   
        </tr>    
        <tr>    
            <td><br />    
            <b><font color="#0B610B"</font></b> <hr size="2" width="100%" align="center"/></ tr> <tr> < TD >< ul> <li>${PROJECT_NAME}</li> <li${BUILD_NUMBER}</li> <li>${CAUSE}</li> <li> Build status:${BUILD_STATUS}</li> <li> Build log: <a href="${BUILD_URL}console">${BUILD_URL}Console </a></li> <li> build Url: <a href="${BUILD_URL}">${BUILD_URL}</a></li> <li"${PROJECT_URL}ws">${PROJECT_URL}Ws </a></li> <li> Project Url: <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>    
                </ul>    

<h4><font color="#0B610B"</font></h4> <hr size="2" width="100%" />
$FAILED_TESTS<br/>

<h4><font color="#0B610B"> Recently submitted (#$SVN_REVISION)</font></h4>
<hr size="2" width="100%" />
<ul>
${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"}</ul> Detailed submission: <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a><br/>

            </td>    
        </tr>    
    </table>    
</body>    
</html>
Copy the code

2. Create a task (free-style software project) django-Web and select parameterized Build

3, source code configuration

4. Trigger configuration

Set it according to your situation

5. Build configuration

Select execute shell

Note: This is the key step of the whole process. In this article, we pull the code on Github and cp it to another directory dedicated to build. In addition, we also copy the Dockerfile file to this directory, and then complete the build and push operations of the Docker image.

  • The release script on the project server is as follows
  • Dockerfile:
FROM python:3.7 ADD./django-web./app WORKDIR./app RUN pip3 install -r requirements. TXT CMD ["python"."manage.py"."runserver"."0.0.0.0:8000"]
Copy the code

6. Operation after construction

Select Editable Email Notification from the list.

Note: Here, Editable Email Notification should be selected. The previous Email should be configured as Extended Email Notification. Otherwise, the Email cannot be sent successfully (Jenkins also has a built-in Email Notification, Not used in this article).

The entire configuration process looks like this, and can be adjusted according to the actual situation. For example, the build script and Dockerfile have a lot of room for adjustment.

Four, effect drawing

1. Pass in the tag parameter and build the project

2. Receive email notification of successful build:

3. Open the URL of the project server that has just been successfully built. Normal access is ok.

4. Go to the project server to check the startup status:


Five, the summary

Mysql is not implemented in a container. If you want to container mysql, you need to pay attention to the network connectivity between mysql and Django projects. Djangos mysql address should be filled with the mysql container name.

Mysql > connect to root@ ‘%’; mysql > connect to root@ ‘%’; mysql > connect to root@ ‘%’ The solution is simply to add a specific mysql account based on the situation.


If you like what I write, you can follow my official account, which contains my learning resources and other benefits. : enterprise tribe