One article teaches you to use Jenkins to design a multi-environment, multi-project continuous integration environment!

Posted Jun 27, 20205 min read

[ ]( https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&album_id=1336857889704837123&__biz=MzI0MDQ4MTM5NQ ==#wechat_redirect)

Author:ticking rain
https://www.cnblogs.com/heyuq...

Automated deployment is mainly to solve problems such as many projects, many environments, slow continuous integration, troublesome deployment operations, error-prone manual operations, and automated operation and maintenance.

Jenkins is a leader in open source CI&CD software, providing more than 1,000 plug-ins to support construction, deployment, and automation to meet the needs of any project.

aims:

  • Support multiple branches, multiple environments, multiple projects, multiple sets of configuration files, multiple programming languages
  • Support one-click construction and cluster release
  • Support one-click rollback history version
  • Quick configuration to add new deployment items
  • Support multiple projects to use the same job to post or rollback

In addition:you can also add gitlab automatic trigger construction, automated testing, nail notification, email notification and other requirements as needed

Final renderings

One-click publishing

Roll back with one click

----jenkins-ex Directory used when Jenkins was built
------software Jenkins installation directory
--------master
--------slave
------backup Jenkins backup directory
--------master
------module Function module, the files related to each type of function are placed in the corresponding subfolder
--------common
----------script Script common to each module
------publish Publish function
--------settings
----------config Configuration file at build time. Eg:jenkins_profile.pubxml, project configuration file, etc.
------------test-publish-template-app-config.json Project mapping configuration table
----------script Jenkins Job script(method encapsulation) called during job component
------source-code source code storage directory
--------test
----------System identification
------------Application name
------build-result build product(compiled result)
--------test
----------System identification
----------Application name
------Temp-file Temporary files, files generated during job execution
--------builder-history construction history file
--------job-params File for transferring parameters during construction
------app-config Application corresponding environment configuration file
--------test
----------System identification
------------Application name
------other-sub-module
...

Conventions and Specifications

naming of jenkins job

  • The job name is all lowercase, and multiple words are separated by "-".(Eg:publish-template-onekey-deploy)
  • Job naming convention:module name-environment-function name.(Eg:publish module, publish-test-onekey-deploy)
  • Naming convention of component job in module:module-c-component name.(Eg:publish-c-pull-code)
  • Job input parameters are prefixed with "p_"

Name the script in Jenkins job(eg:powershell)

  • Variables are all lowercase, multiple words are separated by "_"

Normative convention

  • Variable value representing the path, ending with "\"
  • Use "#" as the delimiter in the backup name, it is easy to take parameters when restoring(eg:p_app_key#2019-1219-1503)

Architecture Design

CICD architecture diagram

The CICD process is mainly performed in two LANs:build server(development intranet) and deployment server(production intranet)

Project mapping configuration file design

To use a job and pull down to "publish | roll back" different projects, we need a flexible project configuration mapping file, similar to the following:

The meaning of the configuration file options can be identified from the naming, mainly including:environment, code branch, deployment path, copy exclusion file list, project information(project unique identifier, directory folder name, source code path, development language, cluster node information...) and many more

  • The configuration under the app_config node can override the parent node configuration and adapt to the specific deployment requirements of the project.
  • app_config is an array node, you can easily add new deployment projects to achieve fast CICD of new projects.

One-click job design

The main stages of "one-click release" are:relevant parameters of the combined project >> get the latest code >> compile and package >> push the application file to the server >> application backup >> copy to the Temp folder >> release to the deployment directory

In order to better implement and control the "one-click release" phases, the following input parameters are designed:

One-click rollback job design

Implementation idea:during "one-key release", save the release record to a file, the storage key is:p_app_key#2019-1219-1503. When performing a rollback, select the historical project to be rolled back, parse out p_app_key first, then obtain the project configuration information, and then roll back the specific historical version of this project.

The input parameters of the design are shown in the figure:

Simple multi-environment CICD process

General software companies have several environments for software development, testing, and release, so there will be corresponding CICD processes for each environment. Here, a simple multi-environment CICD flow chart is designed, as follows:

Automatically trigger CICD or manually trigger CICD? ? ? ,I think:

  • The development environment uses manual triggering:because for the development environment, commit code is more frequent, and sometimes submit to git does not want to trigger CICD. CICD can be triggered automatically every night to facilitate the timely throwing of exception codes
  • The test environment uses automatic triggering:because the git branch merge of the test code is conditionally limited, the merge frequency is relatively small
  • Manual triggering is used in the production environment:because the release of the production environment has strict control over the release time, manual triggering has strong control

If there are errors or other problems, friends are welcome to leave comments and corrections. If you have any help, welcome to like + forward to share.

Welcome everyone to pay attention to the public number of the migrant worker brother:The technical road of the migrant worker brother
image.png