Continuous Integration Environment (4)-Maven project construction and Pipeline pipeline project construction

1 Maven project construction

1.1 Install the Maven Integration plugin

Insert picture description here

1.2 Create a Maven project

Insert picture description here

1.3 Configuration items

The process of pulling code and remote deployment is the same as that of a free-style project, except for the "build" part.
Insert picture description here
Insert picture description here
Insert picture description here

2 Pipeline pipeline project construction

2.1 Introduction to Pipeline

1) Concept

Pipeline, in simple terms, is a set of workflow frameworks running on Jenkins, which connects tasks that originally run independently on a single or multiple nodes, and realizes complex process orchestration and visualization work that is difficult to complete with a single task.

2) Using Pipeline has the following advantages

  • Code: Pipeline is implemented in the form of code and is usually checked into source code control, enabling the team to edit, review, and iterate its delivery process.
  • Durable: Whether it is a planned or unplanned server restart, Pipeline is recoverable.
  • Can be stopped: Pipeline can receive interactive input to determine whether to continue to execute the Pipeline.
  • Multifunctional: Pipeline supports complex continuous delivery requirements in the real world. It supports fork/join, loop execution, and parallel execution of tasks.
  • Extensible: The Pipeline plug-in supports custom extensions of its DSL, as well as multiple options for integration with other plug-ins.

3) How to create Jenkins Pipeline?

  • Pipeline script is implemented by Groovy language, but we don’t need to learn separately
  • GroovyPipeline supports two syntaxes: Declarative (declarative) and Scripted Pipeline (scripted) syntax
  • There are also two ways to create a pipeline: you can directly enter the script in the Jenkins Web UI interface; you can also create a
    Jenkinsfifile script file and put it into the project source code library (generally we recommend directly from the source code control (SCM) in Jenkins) Load
    Jenkinsfifile Pipeline directly ).

2.2 Install the Pipeline plugin

Manage Jenkins->Manage Plugins->Optional plug-
Insert picture description here
ins After installing plug-ins, there are more "pipeline" types when creating projects (see the case below)

2.3 Quick Start of Pipeline Syntax

1) Declarative-Pipeline

Create the project
Insert picture description here
pipeline -> select the HelloWorld template, the
Insert picture description here
generated content is as follows
Insert picture description here

  • stages: represents all the execution stages of the entire pipeline, usually there is only one stage, which contains multiple stages.
  • stage: Represents a certain stage in the pipeline, n may appear. Generally, it is divided into stages such as pulling code, compiling and constructing, and deploying.
  • steps: Represents the logic that needs to be executed in a stage. Inside the steps are shell scripts, git pull codes, ssh remote publishing, and so on.

Write a simple declarative pipeline:

pipeline {
    
     
    agent any   
    stages {
    
     
        stage('拉取代码') {
    
     
            steps {
    
     
               echo '拉取代码' 
            }        
        }
        stage('编译构建') {
    
     
            steps {
    
     
                echo '编译构建' 
            } 
        }
        stage('项目部署') {
    
     
            steps {
    
     
                echo '项目部署' 
            } 
        } 
    }
}

Click build, you can see the whole build process
Insert picture description here

2)Scripted Pipeline脚本式-Pipeline

Create project
image
Insert picture description here
image

node {
    
    
    def mvnHome
    stage('Preparation') {
    
     // for display purposes
        // Get some code from a GitHub repository
        git 'https://github.com/jglick/simple-maven-project-with-tests.git'
        // Get the Maven tool.
        // ** NOTE: This 'M3' Maven tool must be configured
        // **       in the global configuration.
        mvnHome = tool 'M3'
    }
    stage('Build') {
    
    
        // Run the maven build
        withEnv(["MVN_HOME=$mvnHome"]) {
    
    
            if (isUnix()) {
    
    
                sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
            } else {
    
    
                bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
            }
        }
    }
    stage('Results') {
    
    
        junit '**/target/surefire-reports/TEST-*.xml'
        archiveArtifacts 'target/*.jar'
    }
}
  • Node: Node. A Node is a Jenkins node. Master or Agent is
    the specific operating environment for executing Step . It will be used when we talk about the Master-Slave architecture of Jenkins later.
  • Stage: A pipeline can be divided into several stages. Each stage
    represents a set of operations, such as Build, Test, and Deploy. Stage is a logical grouping concept.
    Step: Step. Step is the most basic operation unit. It can be to print a sentence or build a Docker image, provided by various Jenkins plug-ins, such as the command: sh'make', which is equivalent to executing in our usual shell terminal The make command is the same.

Write a simple scripted Pipeline, the construction result is the same as the declarative!

node {
    
     
  def mvnHome 
  stage('拉取代码') {
    
     // for display purposes 
      echo '拉取代码' 
  }
  stage('编译构建') {
    
     
      echo '编译构建' 
  }
  stage('项目部署') {
    
     
      echo '项目部署' 
  }
}

step1: pull code

pipeline {
    
     
    agent any 
    stages {
    
      
      stage('拉取代码') {
    
     
        steps {
    
     
          checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ff7972f4-c4bb-4b3b-a6c1-f6fdda5bbdea', url: 'http://10.99.200.110:8888/test/test-war.git']]]) 
        } 
      } 
    } 
}

step2: Compile and build

pipeline {
    
     
    agent any 
    stages {
    
      
      stage('拉取代码') {
    
     
        steps {
    
     
          checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ff7972f4-c4bb-4b3b-a6c1-f6fdda5bbdea', url: 'http://10.99.200.110:8888/test/test-war.git']]]) 
        } 
      }
      stage('编译构建') {
    
     
        steps {
    
     
          sh label: '', script: 'mvn clean package' 
          }
       } 
    } 
}

step3: project deployment

pipeline {
    
     
    agent any 
    stages {
    
      
      stage('拉取代码') {
    
     
        steps {
    
     
          checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ff7972f4-c4bb-4b3b-a6c1-f6fdda5bbdea', url: 'http://10.99.200.110:8888/test/test-war.git']]]) 
        } 
      }
      stage('编译构建') {
    
     
        steps {
    
     
          sh label: '', script: 'mvn clean package' 
          }
       } 
       stage('项目部署') {
    
     
         steps {
    
     
           deploy adapters: [tomcat9(credentialsId: '6d072e85-02b4-4874-a0fa-c6eecb77fe79', path: '', url: 'http://10.99.200.111:8080')], contextPath: null, war: 'target/*.war' 
         } 
       }
    } 
}

3)Pipeline Script from SCM

Just now we are writing Pipeline code directly on the UI interface of Jenkins, which is not convenient for script maintenance. It is recommended to put the Pipeline script in the project (to carry out version control together)

step1: Write Jenkinsfifile file

Create a Jenkinsfifile file in the project root directory, copy the content to the file, then submit and push to gitlab
or
create a Jenkinsfifile under the root path of the current project of gitlab, and copy the content to the file.

Here is an example of directly operating on gitlab
Insert picture description here
Insert picture description here

step2: reference the file in the project

Insert picture description here
Insert picture description here

step3: build immediately

Insert picture description here

step4: project verification

image

It's all here. For more articles, please refer to the personal WeChat public account ALL In Linux, let's scan it!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44729138/article/details/115097743