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

Solve the problem of setting Chinese language pack for Docker container

If you use docker search centos in Docker Use doc...

MySQL 8.0 New Features - Introduction to the Use of Management Port

Table of contents Preface Connection Management A...

Analysis and description of network configuration files under Ubuntu system

I encountered a strange network problem today. I ...

The normal method of MySQL deadlock check processing

Normally, when a deadlock occurs, the connection ...

Sample code for CSS dynamic loading bar effect

Using the knowledge of CSS variables, I will dire...

Eight hook functions in the Vue life cycle camera

Table of contents 1. beforeCreate and created fun...

MySQL Index Optimization Explained

In daily work, we sometimes run slow queries to r...

Nginx configuration based on multiple domain names, ports, IP virtual hosts

1. Type introduction 1.1 Domain-based virtual hos...

Detailed discussion on the issue of mysqldump data export

1. An error (1064) is reported when using mysqldu...

MySQL sorting using index scan

Table of contents Install sakila Index Scan Sort ...

How to solve jQuery conflict problem

In front-end development, $ is a function in jQue...

When you enter a URL, what exactly happens in the background?

As a software developer, you must have a complete...

Linux uses iptables to limit multiple IPs from accessing your server

Preface In the Linux kernel, netfilter is a subsy...

Detailed explanation of the use of DockerHub image repository

Previously, the images we used were all pulled fr...