Jenkins and svn build automatic code build and release

jenkins installation and configuration

1. Install jenkins
  1.yum install java
   wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
   rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key

  2.yum install jenkins

2. Start jenkins
  [root@localhost sysconfig]# systemctl start jenkins
[root@localhost sysconfig]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@localhost sysconfig]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
                
LISTEN 0 50 :::8080

Jenkins listens to port 8080 by default

Login to jenkins
  http://192.168.11.129:8080
  cat /var/lib/jenkins/secrets/initialAdminPassword
  admin
  632569a8b45b46d2b7799291e79bd958

jenkins basic configuration

SVN server setup

svn comes from the software package subversion. After installing this package, it automatically includes svn server and client commands.

Build svn server
  1.yum install subversion
  2. Create the svn repository directory
    mkdir -p /var/svn/svnrepos
  3. Create a version library
    svnadmin create /var/svn/svnrepos
    A series of fixed directories will be automatically created
    [root@localhost subversion]# cd /var/svn/svnrepos/
    [root@localhost svnrepos]# ls
      conf db format hooks locks README.txt
  4. Modify configuration file
    Enter the conf directory (the svn repository configuration file)/var/svn/svnrepos/conf
    The authz file is a permission control file
    passwd is the account password file
    svnserve.conf SVN service configuration file
   5.Set account password
     vi passwd
     Add user and password in the [users] block, format: account=password, such as dan=dan
   6.Set permissions
      vi authz
      Add the following code at the end:
      [/]
       dan=rw
       w=r
      This means that the root directory of the repository dan has read and write permissions, and w only has read permissions.
   7. Modify the svnserve.conf file
     anon-access = read #Anonymous users can read
     auth-access = write #Authorized users can write
     password-db = passwd #Which file to use as the account file
     authz-db = authz #Which file to use as the permission file
     realm = /var/svn/svnrepos # Certification space name, directory where the version library is located
   8. Start the svn repository
    svnserve -d -r /var/svn/svnrepos
    The listening port is 3690

svn build

6. Import all source code files in the s2 directory into the svn management library
    svn import /root/s2 svn://192.168.11.130/ -m "import tree"
  7. View the remote svn server code
    [root@localhost s2]# svn list --verbose svn://192.168.11.130
      4 yxh September 05 14:39 ./
      4 yxh 7 September 05 14:39 222.txt
      3 yxh 26 September 05 14:36 index.html
      2 yxh 10 September 05 14:32 test2.txt
  8. Pull the latest code from svn
     mkdir /data/test
     svn checkout svn://192.168.255.12/ /data/test --username oldboy --password oldboysecret
    [root@localhost s2]# svn co svn://192.168.11.130/
     A index.html
     A test2.txt
     C 222.txt
     Take out version 4.
    [root@localhost s2]# ls
      222.txt index.html test2.txt
  9. Submit local files to the svn server
    [root@localhost svntest]# svn add index.html
    [root@localhost svntest]# svn ci index.html -m "111"
     Adding index.html
     Transfer file data.
     The submitted version is 3

svn client command

jenkins executes shell script

Two points need to be noted:

1. The path where the execution script is currently located is the working directory of the current Jenkins task. For example, when building the test1 task, /var/lib/jenkins/workspace/test1

2. The current user who executes the script is the user who started jenkins. The default is the ordinary user jenkins, so you may encounter insufficient permissions.

Both the jenkins server and the web server host need to be created
  [root@localhost html]# groupadd -g 1001 www
  [root@localhost html]# useradd -u 1001 -g www www
  [root@localhost html]# echo "123456" | passwd --stdin www

Implement passwordless ssh login for www users
  [root@localhost ~]# su www
  [www@localhost root]$ ssh-keygen
  [www@localhost root]$ ssh-copy-id [email protected]

Create a normal user

[root@localhost ~]# vim /etc/sysconfig/jenkins
                              JENKINS_USER="www"
[root@localhost ~]# chown -R www:www /var/lib/jenkins
[root@localhost ~]# chown -R www:www /var/cache/jenkins
[root@localhost ~]# chown -R www:www /var/log/jenkins
[root@localhost ~]# systemctl stop jenkins
[root@localhost ~]# systemctl start jenkins
[root@localhost ~]# ps -ef |grep jenkins
www 3904 1 99 09:03 ? 00:00:21 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr /lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax =100 --handlerCountMaxIdle=20
root 3953 3762 0 09:03 pts/2 00:00:00 grep --color=auto jenkins

Modify jenkins configuration

#!/bin/bash -ilex
echo "the current shell pwd is $PWD"
echo "the current user is $USER"
echo "start copy file......"
scp index.html [email protected]:/usr/share/nginx/html
echo "the file is copy end"

shell script example

jenkins page settings

1. Add description information to the current build task

2. After setting up source code management, every time you build, the code will be automatically pulled from the svn code repository to the current jenkins workspace directory.

3. Detect whether someone submits the latest code to svn every 2 minutes. If a change in the code is detected, it will be automatically built immediately. Otherwise, it will not be automatically built.

4. Jenkins executes the test1.sh script after pulling the latest code from svn

5. View the build result log

Jenkins build fails and automatically sends email

1. Configure the mail sending server, taking QQ mailbox as an example

jenkins 》 System Management 》 System Settings

The password entered here is not the qq login password, but the 16-digit authorization code that needs to be obtained when the SMTP service is enabled in the settings of the qq mailbox.

QQ Mailbox 》 Settings 》 Account 》

2.Add settings post-build actions

The purpose of Maven

1. Download the jar package that the project depends on from the remote warehouse to the local

2. Compile the java code into the corresponding class file

3. Merge the downloaded jar package and class file into a war package