Jenkins

Jenkins is the leader of open source CI&CD software and Devops wizard. This article introduces Jenkins installation, automatic build and deployment of SpringBoot project

Jenkins installation and configuration

1. Install

1.1 Jenkins’s official website to download the required version of the war file to https://jenkins.io/zh/download/ (recommended download LTS long-term support version problem will be less)

1.2 I use The Linux system, so UPLOAD the downloaded WAR package to the server and write a startup script to start Jenkins. Jenkins is in the form of WAR package and can be directly executed by using the Java-jar command. The script is used for the convenience of subsequent startup and stop of Jenkins

  • Use wgett command to download the war file: wget mirrors. Jenkins. IO/war / 2.190.3…
  • The startup script is as follows
#! /bin/bash
pid=`ps -ef | grep jenkins.war | grep -v 'grep'| awk '{print $2}'| wc -l`
if [ "The $1" = "start" ];then
        if [ $pid -gt0];then
    echo 'jenkins is running... '
    else
    java -jar jenkins.war --httpPort=8888 >/dev/null 2>&1 &
        fi
elif [ "The $1" = "stop" ];then
        exec ps -ef | grep jenkins | grep -v grep | awk '{print $2}'| xargs kill9 -echo 'jenkins is stop.. '
else
        echo "Please input like this:"./jenkins.sh start" or "./jenkins stop""
fi
Copy the code
  • Start and stop commands (executed in Jenkins installation directory)
./jenkins.sh stop
./jenkins.sh start
Copy the code

2. The configuration

2.1 After Jenkins is started, a password will be prompted. You can use cat command to obtain the password from the specified path as prompted

  • Install plug-in: By downloading the HPI file to install the plugin (this method is the way for plug-in download slow, plug-in download slow can refer to the blog https://www.cnblogs.com/hellxz/p/jenkins_install_plugins_faster.html, After setting, it can be downloaded and installed in Jenkins plug-in management without downloading HPI file.

(1) Download the HPI required to install the appropriate version of the plug-in at updates.jenkins-ci.org/download/pl…

  • Git plugin: a Git plugin
  • Maven Integration Plugin: A plug-in required to build a Maven project. After installation, you can choose to build a Maven project when creating a new project
  • Publish over SSH: SSH to the server so that the project can be published to other servers

2.3Jenkins global configuration: System Management -> Global tool configuration (Maven, JDK, Git need to be installed, not installed on the server need to install)

  • Maven global configuration: You can modify settings. XML to change the remote repository to Ali cloud faster. If you have a central repository, you can also change it

  • JDK

  • Git: Name is optional. Path is the installation Path of the server

  • Maven

  • To configure the SSH Server, choose System > System Configuration > Publish over SSH

3. Automatically build and deploy the SpringBoot project

  • Check discard old builds and choose whether to back up old packages that have been replaced. I’m going to back up the most recent 10

  • Source code management: select Git to fill in the repository address, and add access account and password or secret key

  • Add access credentials: Click Add to add access authentication (I used UserName+Password mode or you can choose other methods)

  • Build environment: In the build environment, select Add timestamps to the Console Output. The logs will be printed during code building

! [] (p1-jj.byteimg.com/tos-cn-i-t2…

  • Build: Fill in the Pom location in Build (multiple modules, single module defaults) and fill in the Maven package command

  • Post Steps: Select Run only if build succeeds and Run after the package is successfully packaged

  • Click Add post-build step and select Send Files or Execute Commands over SSH: The operation after the build is successful is to transfer files over SSH and execute corresponding scripts or commands

  • SSH Publishers configuration

Source files: Select the address of the Source file, which is the jar package address after the project is packaged. XX/target/xxx.jar The jar package name of the project

Remove prefix: XX/target/, Remove xxx.jar from Source files

Remote directory:/apps/proj-packages/vevor-amazon-cloud/amazon-admin/ Jar Remote directory:/apps/proj-packages/vevor-amazon-cloud/amazon-admin/ Jar

Exec Command: /apps/proj-packages/vevor-amazon-cloud/amazon-admin/startup.sh Exec Command: /apps/proj-packages/vevor-amazon-cloud/amazon-admin/startup.sh Exec Command: /apps/proj-packages/vevor-amazon-cloud/amazon-admin/startup.sh

  • Start script: the following script means to kill the existing project process, backup the old Jar, obtain the new Jar package and start (many online, you can search by yourself)
DATE=$(date +%Y%m%d)
export JAVA_HOME PATH CLASSPATH
JAVA_HOME=/usr/local/ Java/jdk1.8.0 _171 PATH =$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
DIR=/apps/proj-packages/vevor-amazon-cloud/amazon-admin
JARFILE=amazon-admin.jar
 
if[!-d $DIR/backup ];then
   mkdir -p $DIR/backup
fi
cd $DIR
 
ps -ef | grep $JARFILE | grep -v grep | awk '{print $2}' | xargs kill -9
mv $JARFILE backup/$JARFILE$DATE
mv -f /root/.jenkins/workspace/Cloud-Amazon-Admin/amazon-admin/target/$JARFILE .
 
java -jar $JARFILE --spring.profiles.active=prod  > out.log &
if[$? = 0];then
        sleep 30
        tail -n 50 out.log
fi
 
cd backup/
ls -lt|awk 'NR>5{print $NF}'|xargs rm -rf

Copy the code
  • Click Build Now or Build Now: Project Build Deploy