Thirty two pictures show you how easy Jenkins is to build spring boot~

A lying boy 2020-11-10 12:23:09
thirty pictures easy jenkins build


Continuous original output , Click on the blue word above to follow me

Catalog

  • Preface
  • How to install Jenkins?
    • Environmental preparation
    • Start installation Jenkins
  • Initialize configuration
    • Visit the home page
    • Enter the administrator password
    • Installing a plug-in
    • Create administrator
    • The instance configuration
    • Configuration complete
  • structure Spring Boot project
    • To configure JDK、maven、Git Environmental Science
    • Installing a plug-in
    • add to SSH Server
    • Add the credentials
    • newly build Maven project
    • Build tasks
  • How to build hosting in GitLab Project ?
    • Installing a plug-in
    • add to GitLab API token
    • To configure GitLab plug-in unit
    • A new task
    • Follow up operation
  • How to build a multi module project ?
  • summary

Preface

I don't know if big guy has heard of it , Have you ever used a similar tool ?

In short , The main job of automatic continuous integration is to be able to build projects 、 Automated testing and release .

Today's article will talk about common tools for continuous integration Jenkins And how to automatically build Spring Boot project .

How to install Jenkins?

Jenkins yes Java A set of tools developed , You can download it directly war Package is deployed in Tomcat On , But today the author is most convenient to use 、 One of the most popular Docker install .

Environmental preparation

Before you start the installation, you need to prepare the following environment and tools :

  1. One server , Of course, if you don't, you can use your own computer , The author's server model is Ubuntu.
  2. JDK Environmental installation , The author's version is 1.8, As for how to install , There are many tutorials online .
  3. Get ready maven Environmental Science , Download an installation package from the official website , Put it in the specified directory .
  4. Git Environmental installation , There are many online tutorials .
  5. Code hosting platform , such as GithubGitLab etc. .

Start installation Jenkins

Docker install Jenkins Very convenient , Just follow the author's steps step by step , Must be able to install successfully .

Docker Environmental installation

Each server model is installed in a different way , Readers can install according to their own model , There are many online tutorials .

Pull the mirror image

The version I installed here is jenkins/jenkins:2.222.3-centos, You can get the version you need here : https://hub.docker.com/_/jenkins?tab=tags. Execute the following command to install :

docker pull jenkins/jenkins:2.222.3-centos

Create local data volumes

Create a data volume mount locally docker The data volume in the container , I created /data/jenkins_home/, The order is as follows :

 mkdir -p /data/jenkins_home/

You need to modify the directory permissions , Because when mapping local data volumes ,/data/jenkins_home/ The directory is owned by root user , And in the container jenkins User uid by 1000.

chown -R 1000:1000 /data/jenkins_home/

Create a container

In addition to the need to mount the above created /data/jenkins_home/ outside , You also need to mount mavenjdk Root directory . The start command is as follows :

docker run -d --name jenkins -p 8040:8080 -p 50000:50000 -v /data/jenkins_home:/var/jenkins_home -v /usr/local/jdk:/usr/local/jdk -v /usr/local/maven:/usr/local/maven jenkins/jenkins:2.222.3-centos

The above command is parsed as follows :

  1. -d: Background running container
  2. --name: Specifies the name of the container to start
  3. -p: Specify the port of the mapping , Here is the server's 8040 The port maps to the container 8080 as well as 50000 Mapped to container 50000. Be careful : 8040 and 50000 It must be open and unoccupied , If cloud server is used , We also need to open the corresponding rules in the management platform .
  4. -v: Mount the local data volume to docker In the container , Be careful : Need to put JDK and maven The directory where is mounted .

Initialize configuration

Container started successfully , You need to configure Jenkins, Install some plugins 、 Configure remote push and so on .

Visit the home page

Container created successfully , visit http://ip:8040, If the following page appears, the installation is successful :

Enter the administrator password

Successful launch , You will be asked to enter the password , Here's the picture :

What is required here is the password for management , The hint is in /var/jenkins_home/secrets/initialAdminPassword, But we have already /var/jenkins_home This folder is mounted to the local directory , So you just need to mount the directory /data/jenkins_home/secrets/initialAdminPassword Find... In the file .

Input password , Click to continue .

Installing a plug-in

To initialize the installation, you only need to install some plug-ins recommended by the community , Here's the picture :

Choose here Install the recommended plug-ins , then Jenkins The installation will start automatically .

Be careful : If the plug-in installation is slow , find /data/jenkins_home/updates/default.json file , The replacement is as follows :

  1. take updates.jenkins-ci.org/download Replace with mirrors.tuna.tsinghua.edu.cn/jenkins
  2. take www.google.com Replace with www.baidu.com.

Execute the following two commands :

sed -i 's/www.google.com/www.baidu.com/g' default.json

sed -i 's/updates.jenkins-ci.org\/download/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json

All installed , Move on to the next step .

Create administrator

Create an administrator at will , Fill in the information as required , Here's the picture :

The instance configuration

Configure your own server IP and port , Here's the picture :

Configuration complete

Follow the steps above , After configuration, it will automatically jump to the following interface :

structure Spring Boot project

You need to configure some development environment before you build it , such as JDK,Maven Such as the environment .

To configure JDK、maven、Git Environmental Science

Jenkins Integration needs to use mavenJDKGit Environmental Science , Here's how to configure .

Start by opening System management -> Global tool configuration , Here's the picture :

Configure separately JDK,Git,Maven The path of , Fill in according to your actual path .

Be careful : there JDKGitMaven The environment must be mounted to docker In the container , Otherwise, the following prompt will appear :

 xxxx is not a directory on the Jenkins master (but perhaps it exists on some agents)

After successful configuration , Click save .

Installing a plug-in

In addition to the plug-ins installed in the initialization configuration , Also need to install the following plug-ins :

  1. Maven Integration
  2. Publish Over SSH

open System management -> Plug-in management , choice Optional plug-ins , Check Maven Integration and Publish Over SSH, Click on Direct installation .

Check the installation interface and restart after installation Jenkins.

add to SSH Server

SSH Server Is used to connect to the deployment server , It is used to push your application to the server and execute the corresponding script after the project is built .

open System management -> The system configuration , find Publish Over SSH part , choice newly added

Click on senior Expand configuration

The final configuration is as follows :

After the configuration is completed, you can click Test Configuration Test connection , appear success The connection is successful .

Add the credentials

The credentials It's used to get from Git Warehouse pull code , open The credentials -> System -> Global credentials -> Add the credentials

The configuration here is Github, Use it directly user name and password , Here's the picture :

Create success , Click save .

newly build Maven project

After the above configuration is completed, you can start to build , First you need to create a new Maven project , Steps are as follows .

Create tasks

Click... On the home page A new task -> Construct a maven project , Here's the picture :

Source code management

In source management , choice Git, Fill in Warehouse address , Select the previously added voucher .

Constructing environment

Check Add timestamps to the Console Output, The log will be printed out during code construction .

Build command

stay Build in , Fill in Root POM and Goals and options, That's your command to build the project .

Post Steps

choice Run only if build succeeds, add to Post step , choice Send files or execute commands over SSH.

The options in the figure above are analyzed as follows :

  1. name: Select the previously added SSH Server
  2. Source files: Files to push
  3. Remove prefix: Prefix to remove from file path ,
  4. Remote directory: Which directory to push to the target server
  5. Exec command: The script to be executed on the target server

Exec command Specifies the script to be executed , as follows :

# jdk Environmental Science , If global configuration , It can be omitted 
export JAVA_HOME=/xx/xx/jdk
export JRE_HOME=/xx/xx/jdk/jre
export CLASSPATH=/xx/xx/jdk/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

# jenkins Compiled jar Bag location , It's mounting docker Under the directory of 
JAR_PATH=/data/jenkins_home/workspace/test/target
# Self defined jar Bag location 
DIR=/data/test

## jar The name of the package 
JARFILE=swagger-demo-0.0.1-SNAPSHOT.jar

if [ ! -d $DIR/backup ];then
 mkdir -p $DIR/backup
fi

ps -ef | grep $JARFILE | grep -v grep | awk '{print $2}' | xargs kill -9

if [ -f $DIR/backup/$JARFILE ]; then
 rm -f $DIR/backup/$JARFILE
fi

mv $JAR_PATH/$JARFILE $DIR/backup/$JARFILE


java -jar $DIR/backup/$JARFILE > out.log &
if [ $? = 0 ];then
 sleep 30
 tail -n 50 out.log
fi

cd $DIR/backup/
ls -lt|awk 'NR>5{print $NF}'|xargs rm -rf

The above script roughly means that will kill The original process , Start a new build jar package .

Scripts can be customized , Such as backup Jar Wait for the operation .

Build tasks

After the completion of the new project , Everything is ready , Click on Build immediately You can start building tasks , The console can see log Output , If the build fails , stay log The reason will be output in .

The task build process executes the script to start the project .

How to build hosting in GitLab Project ?

The example described above is to build Github Warehouse project , But enterprises are generally private service GitLab, So how to configure it ?

In fact, the principle is the same , It's just that when you build a task, you choose GitLab The credentials , More on this below .

Installing a plug-in

stay System management -> Plug-in management -> Optional plug-ins Mid search GitLab Plugin And install .

add to GitLab API token

Start by opening The credentials -> System -> Global credentials -> Add the credentials , Here's the picture :

In the picture above API token How to get it ?

open GitLab( For example, the company intranet GitLab Website ), Click... Under the personal settings menu setting, Click again Account, Copy Private token, as follows :

The image above Private token It is API token, Just fill it in .

To configure GitLab plug-in unit

open System management -> The system configuration ->GitLab, Here's the picture :

After successful configuration , Click on Test Connection, If you are prompted Success Then the configuration is successful .

A new task

Create a new one Maven Mission , The configuration steps are the same as above , The only difference is configuration Git Where the warehouse address is , Here's the picture :

Warehouse address and credentials need to be filled in Gitlab Corresponding .

Follow up operation

The following operations , Like building projects , Console output and other operations , It's all with GitHub Same operation , I'm not going to repeat that .

How to build a multi module project ?

If your multiple modules are not dependent on private server repositories , So there is a sequence in building packages , When creating a new task, you need to configure Build Of maven command , Here's the picture :

In the picture above Goals and options The command in is to build api The command of this module , As for what this order means , There's a separate article earlier on , Please have a look at Thinking about a package , original maven I can still play like this ~.

summary

This article describes in detail how to deploy a from zero installation Jenkins, Now I can boast again , ha-ha ....

If it feels good , You can't get lost ~

版权声明
本文为[A lying boy]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云