Introduction to continuous integration and Jenkins automated test deployment

1. What is continuous integration

Continuous integration (Continuous integration, referred to as CI) refers to the frequent integration of code into the backbone.

The purpose of continuous integration is to allow products to iterate quickly while maintaining high quality. Its core measure is that code must pass automated tests before being integrated into the mainline. As long as one test case fails, it cannot be integrated.

With continuous integration, teams can quickly move from one function to another. In short, a large part of agile software development is due to continuous integration.


2. The process of continuous integration

  • commit

The first step in the process is for developers to submit code to the code repository. All subsequent steps start with a single commit of native code.

  • Test (first round)

The code warehouse is configured with a hook for commit. As long as the code is submitted or the trunk is merged, the automated code will be run

  • Construct

After passing the first round of testing, the code can be merged into the trunk, even if it can be delivered. After delivery, build (build) first, and then enter the second round of testing.

The so-called construction refers to converting source code into actual code that can run, such as installing dependencies, configuring various resources (style sheet JS scripts, pictures), etc.

  • Test (second round)

After the build is complete, a second round of testing is required. If the first round has covered all the test content, the second round can be omitted. Of course, at this time, the construction step should also be moved before the first round of testing.

  • deploy

After the second round of testing, the current code is a version (artifact) that can be deployed directly. Archive all the files of this version (tarfilename.tar*) and send them to the production server.

  • rollback

Once a problem occurs in the current version, it is necessary to roll back to the build result of the previous version. The easiest way is to modify the symbolic link to point to the directory of the previous version.


3. The components of continuous integration

1. An automatic construction process, from checking out code, compiling and building, running tests, recording results, test statistics, etc. are all done automatically without manual intervention.

2. A code repository, that is, version control software is required to ensure the maintainability of the code, and as a material library for the construction process, SVN or Git is generally used.

3. A continuous integration server, Jenkins is a continuous integration server with simple configuration and easy use.


Fourth, the benefits of continuous integration

  • Reduce risk, because continuous integration continues to build, compile and test, problems can be found very early, and the cost of repair is less
  • 对系统健康持续检查,减少发布风险带来的问题减少重复性工作
  • 持续部署,提供可部署单元包
  • 持续交付可供使用的版本
  • 增强团队信心

五、jenkins介绍

jenkins 是一款流行的开源持续集成(Continuous lntegration)工具,广泛用于项目开发,具有自动化构建测试和部署等功能。下载地址:https://www.jenkins.io/download/

jenkins特征:

  • 开源的Java语言开发持续集成工具,支持持续集成,持续部署。
  • 易于安装部署配置:可通过yum安装,或下载war包以及通过docker容器等快速实现安装部署,可方便web界面配置管理。
  • 消息通知及测试报告:集成RSS/E-mail通过RSS发布构建结果或当构建完成时通过e-mail通知,生成JUnit/TestNG测试报告。
  • 分布式构建:支持jenkins能够让多台计算机一起构建/测试。
  • 文件识别:Jenkins能够跟踪哪次构建生成哪些jar,哪次构建使用哪个版本的jar等。
  • 丰富的插件支持:支持扩展插件,可以开发适合自己团队使用的工具,如git,svn,maven,docker等。

六、jenkins自动化测试部署

1、创建item

2、配置git下载代码

前置条件:

  • Manage jenkins->Manage Plugins->Available plugins中下载Git plugins插件
  • 本机已配置好git的运行环境,下载地址:https://git-scm.com/

3、配置定时任务

4、生成html测试报告

前置条件:

  • Manage jenkins->Manage Plugins->Available plugins中下载HTML Publisher Plugins插件
  • 任务构建完成之后能生成html报告

Note: The html report generated after the script is built lacks styles, because jenkins has disabled some css and js due to security issues, so if you want to generate a complete test report in jenkins, you must first extract the html report and then process it

html report style lost processing:

1. Execute the statement in Manage Jenkins -> Script console:

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")

Note: Jenkins cannot be restarted, the effect will be lost after restarting, and the statement needs to be re-executed

2. Use the groovy plug-in to execute the above groovy statement (the local groovy execution environment is required, and jenkins installs the groovy plug-in)

5. Configure email sending

Preconditions:

  • Download the Email Extension plugin from Manage jenkins->Manage Plugins->Available plugins
  • Configure the corresponding email configuration information in Manage jenkins->Configure System, as shown in the following figure:

 

 

The html code of the mail content I use:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</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>本邮件由系统自动发出,无需回复!
            <br/>各位同事,大家好,以下为${PROJECT_NAME }项目构建信息</br>
            <td><font color="#CC0000">构建结果 - ${BUILD_STATUS}</font></td>
        </tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>项目名称: ${PROJECT_NAME}</li>
                    <li>构建状态: ${BUILD_STATUS}</li>
                    <li>项目URL: <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                    <li>工作目录: <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                    <li>构建URL: <a href="${BUILD_URL}">${BUILD_URL}</a></li>
                    <li>构建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>测试报告: <a href="${PROJECT_URL}allure">${PROJECT_URL}report</a></li>
                </ul>
                <h4><font color="#0B610B">失败用例</font></h4>
                <hr size="2" width="100%" />$FAILED_TESTS<br/>
            </td>
        </tr>
    </table>
</body>
</html>

6. Generate allure report

Preconditions:

  • Download the Allure Jenkins Plugins plugin from Manage jenkins->Manage Plugins->Available plugins
  • After the task is built, it can generate allure-recognizable files (for example, configure addopts in pytest.ini to generate json files)
  • The allure execution environment is required locally (verification: enter allure in cmd and help will appear)

The final effect:

Guess you like

Origin blog.csdn.net/ouihsiad/article/details/130140836