Learn Jenkins continuous integration (two) use Jenkins continuous integration_build project

1. Continuous integration process description

  1. First, the developers submit the code every day and submit it to the git warehouse
  2. Then Jenkins, as a continuous integration tool, uses the Git tool to pull the code from the Git warehouse to the integrated server, and then cooperates with JDK, Maven and other software to complete code compilation, code testing, review, testing, packaging, etc., every step in this process If an error occurs, the entire process will be executed again,
  3. Finally, Jenkins distributes the generated war package to the test server or production server, and testers or users can access the application.

Two, create a project

Create a job
Insert picture description here

Insert picture description here
Insert picture description here
Select Git as the code source (you need to install the Git plug-in to see it) and
add the warehouse address. Note that you need to add credentials to access the personal Git built by ourselves. If it is Code Cloud, no credentials are required.
Insert picture description here

Insert picture description here
Click the console output, you can see the log information
Insert picture description here

At this time, our server will /var/lib/jenkins/workspacepull the project source code
Insert picture description here
Insert picture description here

Modify the Maven configuration file setting.xml:

  1. Add local warehouse directory
    Insert picture description here
  2. Set up Alibaba Cloud image
	<mirror>
        <id>alimaven</id>
        <name>aliyun maven</name> 
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
        <mirrorOf>central</mirrorOf>
    </mirror>

Insert picture description here

After configuring maven, configure the project and add a build step: execute the shell command. mvn clean package
Note that the environment variables are configured. Both Linux and Jenkins must be configured.
Then click Build Now and you can see the progress bar below.
Click the progress bar, and you can see the log output by printing on the stand-alone console, as well as our newly-built local warehouse and downloaded dependencies.
After the build is complete, you will /var/lib/jenkins/workspace/monitor/target/see the package we finished in.

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Three, Jenkins build Maven project (war package)

Jenkins project build type (1)-introduction to the project type built by Jenkins

There are many types of automatic construction projects in Jenkins, and the following three are commonly used:
FreeStyle Project (FreeStyle Project) Maven Project (Maven Project) Pipeline Project (Pipeline Project)
Each type of construction can actually complete the same construction process It differs from the result only in terms of operation mode and flexibility. In actual development, you can choose according to your own needs and habits. (PS: Personally recommend to use the pipeline type, because the flexibility is very high)

Jenkins project build type (2)-free style project build

Process: pull code -> compile -> package -> deploy

Pull code

1) Create a project
Insert picture description here
2) Configure source code management, pull code from gitlab
Insert picture description here

Compile and package

Build->Add build step->Executor Shell

echo "开始编译和打包" 
mvn clean package 
echo "编译和打包结束"

Insert picture description here

deploy

Deploy the project to the remote Tomcat
1) Install the Deploy to container plugin
Jenkins itself cannot realize the function of remote deployment to Tomcat, you need to install the Deploy to container plugin to achieve

Insert picture description here
2) Add Tomcat user credentials,
omit it
3) Add post-build operation
Insert picture description here

Insert picture description here

Insert picture description here
After saving, click Build Now to automatically deploy to our designated server.

Jenkins project build type (3)-Maven project build

1) Install the Maven Integration plugin
Insert picture description here

Insert picture description here
Insert picture description here

Jenkins project construction type (4)-Pipeline pipeline project construction (*)

1) Concept
Pipeline, in simple terms, is a set of workflow framework 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 by a single task.
2) Using Pipeline has the following benefits (from the official document translation):
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 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.
The Pipeline script is implemented by the Groovy language, but we don’t need to learn Groovy
Pipeline separately. It supports two grammars: Declarative and Scripted Pipeline.
Pipeline also has two creation methods: it can be created directly in the Jenkins Web UI. Enter the script in the interface; you can also create a Jenkinsfile script file and put it into the project source code library (generally we recommend loading the Jenkinsfile Pipeline directly from the source code control (SCM) in Jenkins).

Install the Pipeline plugin
Manage Jenkins->Manage Plugins->Optional plugin Pipeline
Insert picture description here
Insert picture description here
Insert picture description here

Click on Pipeline Syntax, click on Fragment Generator

Insert picture description here
Pull code script:
Fill in warehouse information and click to generate pipeline script.
Insert picture description here
Insert picture description here
Insert picture description here
Compile and package script:
Insert picture description here
Insert picture description here
deploy project script:
Insert picture description here
click to build now
Insert picture description here

Install the Pipeline Script from SCM plug-in.
Just now, we all wrote the Pipeline code directly on the Jenkins UI interface, which is not convenient for script maintenance. It is recommended to put the Pipeline script
in the project (to carry out version control together)
1) Create a Jenkinsfile file in the project root directory. Copy the content into the file,
upload the Jenkinsfile to Gitlab
2) Cite the file in the project
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/DreamsArchitects/article/details/109310010