Learn Jenkins continuous integration (four) Jenkins deploy war package and deploy jar package

Zero, configure Tomcat

By default, Tomcat does not have user role permissions configured.
Insert picture description here
Insert picture description here
However, subsequent Jenkins deployment projects to Tomcat server need to use Tomcat users, so modify the following configuration of Tomcat, add users and permissions

vi /opt/tomcat/conf/tomcat-users.xml
# 内容如下:
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,tomcat,admin-gui,admin-script"/> 

User and password are: tomcat Note: In order to log in to Tomcat by the user just configured, the following configuration needs to be modified
Insert picture description here

vi /opt/tomcat/webapps/manager/META-INF/context.xml
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
# 把上面这行注释掉即可!

Insert picture description here

Restart Tomcat, access test,
Insert picture description here
enter tomcat tomcat to
Insert picture description here
configure Tomcat credentials
Insert picture description here

1. Remote deployment of war packages for free style projects

1. Create a free style project

Insert picture description here

2. Add build steps

Compile and package
Insert picture description here

3. Add post-build operations

Remote deployment
Insert picture description here

4. Build now

Insert picture description here

5. View the console output

Insert picture description here

Second, create a pipeline project to remotely deploy the war package

The project selection is Pipeline流水线项目,

  1. The 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 to achieve complex process orchestration and visualization work that is difficult to complete with a single task.
  2. The use of 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 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. Multi-function: 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 Groovy
    Pipeline separately. It supports two grammars: Declarative (declarative) and Scripted Pipeline (scripted) grammar.
    Pipeline also has two creation methods: Yes Enter the script directly in the Jenkins Web UI 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 plug-in Manage Jenkins->Manage Plugins->Optional plug
-ins After installing the plug-ins, there are more "pipeline" types when creating projects
Insert picture description here

1. Pull the code

Configure git repository, click流水线语法
Insert picture description here

Insert picture description here
Click to generate pipeline script
Insert picture description here

2. Build and compile

 		stage('编译构建') {
    
    
            steps {
    
    
                sh label: '', script: 'mvn clean package -Dmaven.test.skip=true'
            }
        }

Insert picture description here

3. Remote deployment

Insert picture description here
Click to generate pipeline script
Insert picture description here

		 stage('远程部署') {
    
    
            steps {
    
    
            deploy adapters: [tomcat8(credentialsId: 'a2672517-c02b-4f62-ab3c-e6f70d08b98e', path: '', url: 'http://wangyitong.club:8080/')], contextPath: null, war: 'target/*.war'
            }
        }

Full script:

pipeline {
    
    
    agent any

    stages {
    
    
        stage('拉取代码') {
    
    
            steps {
    
    
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '614e4281-db76-4388-befc-c50588f5e751', url: 'https://gitee.com/L1692312138/ssm_web_war_jenkins.git']]])
            }
        }
        stage('编译构建') {
    
    
            steps {
    
    
                sh label: '', script: 'mvn clean package -Dmaven.test.skip=true'
            }
        }
         stage('构建完成') {
    
    
            steps {
    
    
                echo '构建完成'
            }
        }
        stage('远程部署') {
    
    
            steps {
    
    
            deploy adapters: [tomcat8(credentialsId: 'a2672517-c02b-4f62-ab3c-e6f70d08b98e', path: '', url: 'http://wangyitong.club:8080/')], contextPath: null, war: 'target/*.war'
            }
        }
    }
    post {
    
    
          always {
    
    
             emailext(
                subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
                body: '${FILE,path="email.html"}',
                to: '[email protected]'
             )
          }
    }
}


Installation Pipeline Script from SCM plugin
we just are writing code directly in Jenkins Pipeline UI interface, so the script is not convenient maintenance, it is recommended to put script Pipeline
in the project (with version control) 1) establish Jenkinsfile file in the root directory of the project, Copy the content to the file
Insert picture description here
2) Reference the file in the project
Insert picture description here
Click Build to view the console output
Insert picture description here

Second, deploy the jar package

1. Create a free style project

Process:
Add post-build operation steps, then configure ssh information, write scripts, then execute the scripts after sending the package, stop the process, and start the process.

2. Install Publish Over SSH plugin

Publish Over SSH plugin

Insert picture description here
Then configure ssh
Insert picture description here
Insert picture description here
Insert picture description here

3. Add build steps to compile and package

Insert picture description here

4. Add post-build operation steps

Configuration project--"Add Sned build artifacts over SSH for post-build operation steps
Insert picture description here

  1. Source files directory after the project is built

  2. Remove prefix

  3. Remote directoty release directory

  4. Exec command issued the executed command

Insert picture description here

5. Write a shell script

First find the port number of the running project by the project name, if it exists, kill the process,
and then restart the project again.

shell script:

#!/bin/env bash

#PID=`ps -ef |grep 项目名称  |grep -v grep | awk '{print $2}'`
PID=`ps -ef |grep 'wxthepublic-1.0-SNAPSHOT.jar'  |grep -v grep | awk '{print $2}'`
if [ ! "$PID" ]
then # 这里判断TOMCAT进程是否存在
    echo $PID"进程不存在"
else
    echo "进程存在 杀死进程PID$PID"
    kill -9 $PID
fi
nohup java -jar 'wxthepublic-1.0-SNAPSHOT.jar' >/dev/null 2>log &
#根据重启后是否有当前应用判断启动是否成功
pid=$(ps -ef | grep java| grep 'wxthepublic-1.0-SNAPSHOT.jar'|awk -F '[ ]+' '{print $2}')
echo $pid
if [ -z $pid ]
then
  echo "启动失败"
  exit 1
else
  echo 'wxthepublic-1.0-SNAPSHOT.jar' :  $pid  "启动成功"
fi

6. Build now

Click Build Now to view
Insert picture description here
the jar package that has been typed in the console output directory.
Insert picture description here

Insert picture description here

Guess you like

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