Teach you in great detail how to use Jenkins to implement automated jar package deployment

Welcome to visit my blog and communicate: https://endwas.cn

Here we only demonstrate the deployment of a single project. If it is a distributed project or in KubeSphere, you only need to change the traditional build project into a pipeline. Here we will simply implement pulling code, maven packaging, package body upload, restart jar package, etc. .

For Jenkins installation, you can check out my other blog, which uses docker installation, which is fast and has fewer pitfalls than traditional tomcat+war or yum installation.

Note: The maven used is mounted. Download maven: maven3.5.4 , so you need to install maven on the docker host first and configure the environment variables.


1.jenkins entry configuration

1. Get the initial password

Open the jenkins page, the default port is ip:8080, local is localhost:8080;

Insert image description here

First unlock jenkins and enter the jenkins container through the server

docker exec -it jenkins /bin/bash
jenkins@93f88d6ca212:/$ cat /var/jenkins_home/secrets/initialAdminPassword
a6f6d08fcc474178833001d1fc79be62

2. Install default plug-ins

Then install the default plug-ins. If you know what plug-ins you need, you can also choose to install them specifically.
If you don’t know what to install by default first and what to use later.

Insert image description here

3. Create an administrator account

Then create your first account after installation is complete

Insert image description here

4.Instance configuration

Just configure the instance with the default server address.

Insert image description here
In this way, the default configuration of jenkins is fine, but the plug-in is still missing.

2.Install the plug-in

1. Enter system management

Insert image description here

2. Click Plug-in Management

Insert image description here
The most commonly used configurations in the system are these three configurations. Just remember to enter them through the gear on the left.

3. Install required plug-ins

Select the optional plug-in to search for the one we need to install. If it cannot be found, confirm whether it is already installed.

Insert image description here
1️⃣ Publish over ssh: SSH file transfer plug-in, equivalent to the xftp we usually use, for uploading files to the server.
2️⃣ Maven Integration plugin: used to configure maven and build jar packages in jenkins.
3️⃣ Role-based Authorization Strategy: Used to control permissions. Administrator, read, and write permissions can be set for different users.

Remember to restart jenkins after installation.

3. Configure the installed plug-ins

Return to system configuration

Insert image description here

1. Configure maven

In the installation of the previous article, I mounted a maven folder. The purpose of that maven folder is to use it in the container.
Then just find a folder on the server to unzip it, and then mount it to docker for use.

Enter the global tool configuration->Maven
name: fill in
MAVEN_HOME as you like: the path to mount to docker. Because I also packaged a layer of folders, my maven is placed under apache-maven-3.5.4/
Insert image description here

Also configure the settings file. In the same way, just find the settings in the folder.
Insert image description here

2. Configure ssh

Enter the first one of system configuration, system configuration,
find publish over ssh
Insert image description here
and click Advanced to open the setting port + password

Insert image description here
Finally, test the connection situation, and it will show success that the connection can be connected~

4. Create automated tasks

When all preparations are ready, start creating automated deployment tasks.

  1. Pull the code.
  2. Pack.
  3. Upload the corresponding server.
  4. Restart the jar package.

1. Pull the code

Insert image description here
Insert image description here
Note: If you choose ssh for the git address, you need to configure the public and private keys. For specific usage, you can view jenkins configuration git . Here, use the account password to obtain it.

Insert image description here
git username and password

Insert image description here

2. Packing

Insert image description here

Note: The path here is the path in the working directory. We pulled the code and the pom.xml is in the folder, so the configuration is pom.xml. If it is a multi-layer folder for microservices, you need to specify the xxx
microservice /pom.xml

// 跳过test也可以直接使用clean package
clean package -Dmaven.test.skip=true

3. Upload the jar package to the server

Insert image description here

Because I want to upload to the server, I selected the send files option.

Insert image description here
Let’s focus on the configuration of post steps.

  • name: The server previously configured in the global configuration, just select the specific configuration.
  • Source files: files to be uploaded after being packaged in jenkins Workspace . Because our package will be generated in target/xxxxxx.jar, we need to match it to target/xxxx.jar
  • Remove prefix: If this is not configured, then we will upload the target folder + jar package to the server, because we only need the jar package, so configure target/
  • Exec command: After uploading the folder, the script is executed on the target server . The script is to restart the program. If you need to back up, you can also execute the shell script backup in the pre step.
  • If the target server already has the jar package with the same name, it will be overwritten.

Note: Reasons for script execution failure

  1. You need to enter the script location sh restart.sh.
  2. If the startup command fails to execute, add source /etc/profile before sh.
cd /home/endwas/d
source /etc/profile
sh restart.sh

4. Run

Insert image description here

ps: If the execution fails, click the version number and check the log to confirm which step failed.

Insert image description here

Summarize

Jenkins automated deployment and visual interface are very helpful for operation and maintenance, which improves the efficiency of testing and front-end and back-end docking, but sometimes the configuration can make people crazy. For example, the environment is configured, but mvn, node, etc. cannot be used. Order. Executing the shell on the remote host but the project does not start, etc.

Guess you like

Origin blog.csdn.net/qq_40922616/article/details/121115569