The process of deploying a project to another host using Jenkins

The process of deploying a project to another host using Jenkins

environment

Hostname ip address Serve
Jenkins 192.168.216.200 tomcat, jenkins
server 192.168.216.215 tomcat


// Turn off firewall selinux

//Install the packaging command, git
[root@jenkins ~]# yum -y install maven
[root@jenkins ~]# yum -y install git

//Configure password-free login [root@localhost ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:npcyZyHpOPmqGL1nbpZRj7UYULZxJhvNIcDg2G9Qvso [email protected]
The key's randomart image is:
+---[RSA 3072]----+
| .oooBo+. |
| + oo. Xo |
| . + ..o |
| o .o.. |
| +.S*.. |
| o o.=oooo |
| . E +o* = |
| o .*o * |
| . oBo.. |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost ~]# ssh [email protected]
Last login: Wed Oct 20 11:03:43 2021 from 192.168.216.200
[root@localhost ~]# exit
LogoutConnection to 192.168.216.215 closed.

Enter the Jenkins terminal



pipeline
    agent any
    
    stages {
        stage('Build') {
            steps {
                // Get some code from a GitHub repository
                git 'https://gitee.com/forgotten/tomcat-java-demo.git' #Download the package and make sure the git command is available on the host // Run Maven on a Unix agent.
                sh "mvn -Dmaven.test.failure.ignore=true clean package" #Package into war packagesh "mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/myapp.war" #Change package name// To run Maven on a Windows agent, use
                // bat "mvn -Dmaven.test.failure.ignore=true clean package"
                
            } 
        }        
        stage("publish"){
            steps{
                sh "ssh [email protected] 'tar -Jcf /opt/backup/webapps-\$(date +%Y-%m%d).tar.xz /usr/local/tomcat/webapps/*'" #Backup the original projectsh "scp target/myapp.war [email protected]:/usr/local/tomcat/webapps/" #Transfer the package to the target hostsh "ssh [email protected] '/usr/local/tomcat/bin/catalina.sh stop;sleep 3;/usr/local/tomcat/bin/catalina.sh start'" #Restart the generated project}
        }
    }
}


Go to the target host to verify

Manual deployment

//Turn off firewall, selinux

// Make sure the git command exists and pull the deployed project package [root@jenkins opt]# git clone https://gitee.com/forgotten/tomcat-java-demo.git
Cloning into 'tomcat-java-demo'...
remote: Enumerating objects: 558, done.
remote: Counting objects: 100% (558/558), done.
remote: Compressing objects: 100% (316/316), done.
remote: Total 558 (delta 217), reused 558 (delta 217), pack-reused 0
Receiving objects: 100% (558/558), 5.08 MiB | 673.00 KiB/s, done.
Processing delta: 100% (217/217), done.
[root@jenkins opt]# ls
tomcat-java-demo

//Package, make sure the mvn command exists [root@jenkins opt]# cd tomcat-java-demo/
[root@jenkins tomcat-java-demo]# ls
db Dockerfile LICENSE README.md
deploy.yaml jenkinsfile pom.xml src // All files with pom.xml are packaged with mvn [root@jenkins tomcat-java-demo]# mvn clean package
[root@jenkins tomcat-java-demo]# mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/myapp.war
[root@jenkins tomcat-java-demo]# ls target/
classes ly-simple-tomcat-0.0.1-SNAPSHOT maven-status
generated-sources maven-archiver myapp.war

// Make sure there is a password-free login, start the server-side tomcat through ssh, and then pass the package to be deployed [root@jenkins tomcat-java-demo]# scp target/myapp.war [email protected]:/usr/local/tomcat/webapps/
//tomcat will automatically decompress, and after a while you can enter the corresponding directory of the website to see the newly deployed project

This is the end of this article about using Jenkins to deploy a project to another host. For more information about deploying a Jenkins project to another host, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Creating a scheduled build task for Jenkins
  • Add SSH global credentials for Jenkins
  • Jenkins configuration Maven project packaging, deployment, and release
  • How to implement shell scripting to execute tasks on a regular basis in Jenkins
  • Solve the error problem encountered when integrating Jenkins with SonarQube
  • Agile development using Jenkins automated build tools

<<:  How Web Designers Create Images for Retina Display Devices

>>:  Use of marker tags in CSS list model

Recommend

Use html-webpack-plugin' to generate HTML page plugin in memory

When we package the webpackjs file, we introduce ...

MySQL 8.0.20 Installation Tutorial with Pictures and Text (Windows 64-bit)

1: Download from mysql official website https://d...

HTML form value transfer example through get method

The google.html interface is as shown in the figur...

Ubuntu terminal multi-window split screen Terminator

1. Installation The biggest feature of Terminator...

Implementation steps for enabling docker remote service link on cloud centos

Here we introduce the centos server with docker i...

JavaScript implements the nine-grid mobile puzzle game

This article shares the specific code for JavaScr...

Free tool to verify that HTML, CSS and RSS feeds are correct

One trick for dealing with this type of error is t...

How to view MySQL links and kill abnormal links

Preface: During database operation and maintenanc...

How to use MySQL covering index and table return

Two major categories of indexes Storage engine us...

Automatic file synchronization between two Linux servers

When server B (172.17.166.11) is powered on or re...

JavaScript design pattern chain of responsibility pattern

Table of contents Overview Code Implementation Pa...

A brief discussion on how to elegantly delete large tables in MySQL

Table of contents 1. Truncate operation 1.1 What ...

VSCode+CMake+Clang+GCC environment construction tutorial under win10

I plan to use C/C++ to implement basic data struc...