jenkins + pipeline build automation deployment

I. Introduction  

       The essence of Jenkins 2.x is Pipeline as Code, so why use Pipeline? Jenkins 1.0 can also realize automated construction, but Pipeline can put the configuration information in the previous project in a script in the form of steps, and connect the tasks that were originally running on a single or multiple nodes independently, to achieve a single task that is difficult to complete. The complex process forms a pipelined release, and the construction steps are visualized. Simply put, Pipeline is applicable to a wider range of scenarios and is capable of more complex release processes. For example, the job construction works on the master node, and the automated test script is on the slave node. At this time, jenkins 1.0 cannot run two nodes at the same time, but Pipeline can.

 

2. Several basic concepts of Pipeline:

  • Stage: Stage, a Pipeline can be divided into several Stages, each Stage represents a group of operations. Note that Stage is a logical grouping concept that can span multiple Nodes.
  • Node: Node, a Node is a Jenkins node, or a Master, or a slave, which is the specific runtime environment for executing Step.
  • Step: Step is the most basic unit of operation, ranging from creating a directory to building a Docker image, provided by various Jenkins Plugins.

 

 

3. PIpeline syntax

1.Pipeline supports two syntaxes: Declarative Pipeline (introduced in Pipeline 2.5, structured way) and Scripted Pipeline, both of which support the establishment of a continuous pipeline.
What they have in common:
Both are persistent implementations of pipeline code, both can use pipeline built-in plugins or steps provided by plugins, and both can take advantage of shared library extensions.
Difference:
The difference between the two is syntax and flexibility. The Declarative pipeline has a stricter syntax for users, has a fixed organizational structure, and makes it easier to generate code snippets, making it a more ideal choice for users. However, the Scripted pipeline is more flexible, because Groovy itself can only limit the structure and syntax. For more complex pipelines, users can flexibly implement and expand according to their own business. The following examples illustrate the use of the two syntaxes.

 

2.Scripted Pipeline

node( ' master ' ) { //master node is running, the following stage can also specify node
    stage ' Prepare '   // Clear the release directory
        bat '''if exist D:\\publish\\LoginServiceCore (rd/s/q D:\\publish\\LoginServiceCore)
               if exist C:\\Users\\Administrator\\.nuget (rd/s/q C:\\Users\\Administrator\\.nuget)
               exit'''

    //Pull the git code repository
    stage 'Checkout'
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: 'c6d98bbd-5cfb-4e26-aa56-f70b054b350d', 
            url: 'http://xxx/xxx/xxx']]])
   
    //Construct
    stage 'Build'
        bat '''cd "D:\\Program Files (x86)\\Jenkins\\workspace\\LoginServiceCore\\LoginApi.Hosting.Web"
            dotnet restore
            dotnet build
            dotnet publish --configuration Release --output D:\\publish\\LoginServiceCore'''
    
    //deploy
    stage 'Deploy'
        bat '''
        cd D:\\PipelineScript\\LoginServiceCore
        python LoginServiceCore.py
        '''

//Automated test (python code implementation) stage 'Test' bat''' cd D:\\PipelineScript\\LoginServiceCore python LoginServiceCoreApitest.py ''' }

 

3.Declarative Pipeline

pipeline {
    agent any                  // Run on available nodes, I only have master here
    stages{

stage (
'Prepare'){ steps{ // Clear the release directory bat '''if exist D:\\publish\\LoginServiceCore (rd/s/q D:\\publish\\LoginServiceCore) if exist C:\\Users\\Administrator\\.nuget (rd/s/q C:\\Users\\Administrator\\.nuget) exit ''' } }
// Pull the git code repository stage ('Checkout'){ steps{ checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c6d98bbd-5cfb-4e26-aa56-f70b054b350d', url: 'http://xxx/xxx/xxx']]]) } } // build stage ('Build'){ steps{ bat '''cd "D:\\Program Files (x86)\\Jenkins\\workspace\\LoginServiceCore\\LoginApi.Hosting.Web" dotnet restore dotnet build dotnet publish --configuration Release --output D:\\publish\\LoginServiceCore''' } } // deploy stage ('Deploy'){ steps{ bat ''' cd D:\\PipelineScript\\LoginServiceCore python LoginServiceCore.py ''' } } // Automated test (python code implementation) stage ('Test'){ steps{ bat''' cd D:\\PipelineScript\\LoginServiceCore python LoginServiceCoreApitest.py ''' } } } }

 

Fourth, Pipeline configuration

1. Create a new "pipeline" job

 

2. Configure the Pipeline script.

 

3.Pipeline also supports Poll SCM .

 

 4. Save -> Build Now

 

 

V. Summary

Pipeline can easily realize continuous delivery of pipeline, and the execution stage can be seen at a glance through the view. For more detailed information, you can visit the official documentation  https://jenkins.io/doc/book/pipeline/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324996178&siteId=291194637