当前位置:网站首页>Unit test ci/cd

Unit test ci/cd

2022-06-28 13:18:00 Swuagg

Catalog

Preface

 1 CI/CD

1.1 What is? CI

1.2 What is? CD

1.3 CI/CD technological process

2 CI Platform selection

2.1 Github Actions

2.2 Jenkins

2.3 Github Actions Vs Jenkins

2.3.1 Github Actions The advantages of

2.3.2 Github Action The shortcomings of

2.3.3 Jenkins The advantages of

2.3.4 Jenkins The shortcomings of

2.3.5 Similarity

2.3.6 The main difference

2.4 Summary

3 Unit Test CI( Unit test continuous integration )

3.1 Github Generate token

3.2 Jenkins install / To configure GitHub Pull Request Builder plug-in unit

​ edit

3.3 Jenkins Configure tasks

3.3.1 A new task

3.3.2 Source code management

3.3.3 Build trigger

3.3.4 Build steps

3.4 Github newly build webhook

3.5 Follow up improvement

3.6 improvement

4 Acknowledgement


Preface

At present, the effect is : stay Github carry Pull requests when , Automatic triggering Jenkins Perform unit tests , And feed back the execution results to Github Pull requests page .

Unit Test CI The configuration process :


 1 CI/CD

1.1 What is? CI

         Continuous integration (CI): Is continuous automatic detection after source code changes 、 Pull 、 The process of building and unit testing .

         The goal of continuous integration : Is to quickly ensure that developers' newly submitted code changes are correct 、 Can be integrated , And suitable for further use in the code base .

notes : The behavior that accompanies continuous integration is continuous testing .

1.2 What is? CD

         Continuous delivery (CD): Automatically publish validated code to the repository .

         The goal of continuous delivery : It is to have a code base that can be deployed to the production environment at any time .

         Continuous deployment (CD): Automatically publish applications to the production environment .

notes : Continuous delivery and continuous deployment , Is continuous through automation Pipeline The way , The act of publishing artifacts into the environment . These two are sometimes used interchangeably , There is no need to dwell on these semantics .

1.3 CI/CD technological process

        CI/CD technological process : Also known as CI/CD Assembly line , Continuous delivery line .

         Continuous delivery covers from demand 、 Design 、 Development 、 structure 、 test 、 The process of the whole online process 、 Tools 、 Method 、 Platformized input to output .

2 CI Platform selection

         from 2018 year 10 month Github Actions When it appears , Many articles discuss CI Platform selection , yes Github Actions, still Jenkins.

2.1 Github Actions

        Github Actions It's an event driven (Push,Pull requests etc. ) Continuous integration and continuous delivery tools , Allows you to automate the construction of 、 The process of testing and deploying applications to various environments .

         let me put it another way ,GitHub hold CI/CD Many operations , Like grabbing code 、 Operation test, etc , be called Actions. Developers can write their own action, You can also use the... Provided by other developers action, So the whole CI/CD The process , It becomes multiple actions The combination of , This is it. Github Actions.

2.2 Jenkins

        Jenkins It's a use. Java Write the open source automation server , For automated continuous build 、 Workflow for testing and deploying software projects .

        Jenkins It is mainly realized through rich plug-ins CI/CD, The following figure represents a simple Jenkins workflow , It is from Github Extract code from , Build a from the code docker Mirror image , It is then tested and pushed further to the remote repository DockerHub.

2.3 Github Actions Vs Jenkins

        Jenkins and GitHub Actions Can be created automatically 、 test 、 Release 、 Workflow for distributing and deploying code . Generally speaking, for most companies , You don't need to develop one yourself CI platform , And there is no absolute difference advantage between tools , Here is just an objective description of the relationship and similarities and differences between the two .

2.3.1 Github Actions The advantages of

  • Easy to use : Github Actions Very friendly to beginners . You only need one YAML The file is ready to use . For startups / A small company , Your developers may already be interested in YAML Know something about , therefore Github Actions As CI/CD The platform is a logical choice .

  • Setup and maintenance : Use Jenkins, You will run it on a custom server . This means that you must continue to maintain Jenkins The server . however ,Github Actions Provides you with the CI/CD Free runner for operation . These operators are controlled by Github Own and maintain , But you can also add a self managed runner .

2.3.2 Github Action The shortcomings of

  • lock : Use Github Actions, You are more or less related to Github As a source code management system . Use Jenkins Allows you to store code in Github、Gitlab、BitBucket And so on .

  • maturity : Jenkins Already exists and is better than Github Actions The more mature .Github The operation is relatively new , Therefore, there is less community support .

2.3.3 Jenkins The advantages of

  • Use Jenkins One of the advantages of is that it is open source and free , Unlike its counterpart Github Actions yes Free value added ”.

  • Plug ins can be used to support caching , The files from the previous build are cached and re implemented in the new build as needed .

2.3.4 Jenkins The shortcomings of

  • Jenkins There are many plug-ins available to users , Unfortunately , Some of these plug-ins are not maintained regularly by developers . Therefore, it is necessary to check whether the plug-in supports correctly before use .

  • It is highly dependent on plug-ins , Sometimes it is difficult to find the basic functions of plug-ins .

  • You must maintain and manage by yourself Jenkins Infrastructure .

  • Due to the uncertainty of the load on the server , At remote ( cloud ) On the server Jenkins The cost of may not be predictable . These workloads include the amount of code 、 Quantity of artifact 、 Submission times, etc .

2.3.5 Similarity

  • Jenkins Use Declarative Pepeline Create workflow , These workflows are similar to GitHub Actions Workflow documents .

  • Jenkins Run a set of steps using phases , and GitHub Actions Then use jobs to group one or more steps or a single command .

  • Jenkins and GitHub Actions Support for container based builds .

  • Steps or tasks can be reused and shared with the community .

2.3.6 The main difference

  • Jenkins There are two types of syntax for creating pipes :Declarative Pipeline and Scripted Pipeline. GitHub Actions Use YAML Create workflows and profiles .

  • Jenkins Deployment is usually self managed , Users maintain servers in their own data centers . GitHub Actions Provide hybrid cloud methods by hosting a runner that you can use to run jobs , It also supports self managed runners .

Github Actions vs Jenkins: A further comparison is shown in the following table :

 

  • GitOps Is to use the source version control platform Git The practice of managing infrastructure and configuration .

  • Asynchronous integration simply means that builds are performed at the same time , This makes the workflow faster . as for Jenkins, Synchronous construction is the main content .

  • Ad Hoc Workflow: For in Github Actions Build that occurs in , It will monitor git Event trigger . On the other hand ,Jenkins Many jobs can be performed , for example : function bash Script 、maven buildsm powershell etc. . If you need to run jobs to run regularly in sequence without linking to the code base , that Jenkins Can do this .

2.4 Summary

         be based on Jenkins The maturity of , And project compilation Unity Complexity , We finally chose Jenkins As CI/CD Tools for .

3 Unit Test CI( Unit test continuous integration )

         At present, the effect is : stay Github carry Pull requests when , Automatic triggering Jenkins Perform unit tests , And feed back the execution results to Github Pull requests page .

         The following describes in detail the use of GitHub pull request when Jenkins An automated build Configuration process , about Jenkins Setting up and configuring the environment , I won't go into details here .

3.1 Github Generate token

        GitHub Click on your avatar , Choose in turn Settings > Developer settings > Personal access tokens, Click on Generate new token Button to enter new Token Generate page , Check the options shown in the figure , spot Generate token Button generation Token And will generate Token Copy and save .        

3.2 Jenkins install / To configure GitHub Pull Request Builder plug-in unit

        1、Jenkins > System management > Plug-in management > Optional plug-ins , Search for GitHub Pull Request Builder, Then select direct installation  

        2、 stay Jenkins Add Credentials,Kind Place selection Secret text, Secret Fill in GitHub Of Token, Later configuration GitHub Pull Request Builder Need to use it .

        3、 And then use GitHub User name and password to create another Credentials, It will be used when configuring tasks later . Of course, if you have already added , No more configuration .

        4、 And then in jenkins > System management > System settings > GitHub Pull Request Builder The following configuration

        GitHub Server API URL: The default is https://api.github.com , Fill in the enterprise version https:// domain name /api/v3/.

        Credentials: Use... Before choosing GitHub Generated token Created Credentials.

         Fill in the contents above , You can use the... Below Connect to API Button to verify the connection GitHub If there is something wrong ,Check repo permissions Button to verify permissions .

3.3 Jenkins Configure tasks

3.3.1 A new task

        1、 Create a free style task , Then configure the task

        2、 Will project's GitHub URL Add to GitHub Project office ( Items that can be imported into the browser ) 

3.3.2 Source code management

         Add code in the configuration source code management GitHub Address in ,Credentials Select above to use GItHub Created with your account and password Credentials.

 

  • Refspec:

    • If you just want to build PR, Please put refspec Set to +refs/pull/${ghprbPullId}/*:refs/remotes/origin/pr/${ghprbPullId}/*

    • If you want to build PR And branches , Please put refspec Set to +refs/heads/*:refs/remotes/origin/* +refs/pull/${ghprbPullId}/*:refs/remotes/origin/pr/${ghprbPullId}/*

  • Specify Branch : Fill in ${sha1}, If you want to use the submitted pr, Fill in this field ${ghprbActualCommit}

3.3.3 Build trigger

         Add the administrator list to the build trigger , Check Use github hooks for build triggering And click the advanced button on the back ,White list Add... To allow for this task pull request Of GitHub The user account .

3.3.4 Build steps

        1、 Build here, select the appropriate build tool according to your project situation , My project uses Gradle Build, so choose Invoke Gradle script.

        2、 If you add Set build status to “pending” on GitHub commit This is the building step , So when building GItHub Project Pull requests Will show pending state . 

3.4 Github newly build webhook

        1、 stay GitHub Corresponding project remote warehouse ,Settings > Webhooks in , Create a new one webhook, Fill in the following figure Jenkins Address .

        2、 Choose Let me select individual events, Check Pull requests Options . 

        3、 If GitHub Be able to connect correctly Jenkins, that Recent Deliveries There will be an error free record below . 

        4、 Here we are , Configuration is complete . Next, send a to a branch of the project pull request test . Hair PR after GitHub Displayed on the pending check state  

        5、Jenkins Successfully triggered a build on  

        6、 When successfully built ,GitHub The above status is changed to successful checks. 

3.5 Follow up improvement

  • Github Visualization of single test results

  • Github When the single test result fails or is abnormal , Don't allow PR

  • Github Support jump Jenkins Specific construction tasks

  • ...

3.6 improvement

        Github Support jump Jenkins Specific construction tasks :

4 Acknowledgement

        【 What is? CI/CD? One article will help you understand CI Continuous integration and CD Continuous delivery / Deploy - Red hat

        【faun.pub

        【GitHub pull request when Jenkins Auto build tutorial

原网站

版权声明
本文为[Swuagg]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/179/202206281308005196.html