Automatic deployment and configuration of the SVN

The environment

  1. System: centos7.7

  2. Host: 192.168.0.193

  3. Docker: 19.03.5

  4. Maven: 3.6.3

  5. The JDK: 1.8

  6. SVN: 1.7.14

  7. Jenkins: 2.190.3

Install maven

  • Download from official website (3.6.3 is used on this machine)

    [Maven – Download Apache Maven] (maven.apache.org/download.cg…).

  • Home directory Create a Maven folder and upload the downloaded files to the Maven folder. In order not to be restricted by permissions, use the command to grant all permissions

    chmod -R 777
    Copy the code

  • Decompress the installation package

    The tar ZXVF - apache maven - 3.6.3 - bin. Tar. GzCopy the code
  • Configuring environment Variables

    Go to the decompressed directory PWD and copy the absolute directory /home/maven/apache-maven-3.6.3

  • Edit the configuration file and add the following configuration

    vim /etc/profile
    Copy the code
    exportMAVEN_HOME = / home/maven/apache maven -- 3.6.3export PATH=$MAVEN_HOME/bin:$PATH
    Copy the code

  • Hold exit and execute the command for it to take effect

    source /etc/profile
    Copy the code
  • Check whether the installation is successful

    mvn -v
    Copy the code

  • The installation is successful, as shown in the preceding figure

Install the JDK

  • Home directory Create a JKD folder and upload the compressed package to the folder

  • Check whether there is a built-in JDK, if there is uninstall and then install

    rpm -qa | grep jdk
    Copy the code

  • uninstall

    yum -y remove
    Copy the code
    Yum -y remove Java -1.8.0-openjdk-headless-1.8.0.232.b09-0.el7_7.x86_64 yum -y remove Java -1.8.0-openjdk-headless-1.8.0.232.b09-0.el7_7.x86_64 Java -1.7.0-openjdk-headless-1.7.0.241-2.6.20.0.el7_7.x86_64 yum -y remove copy-jdk-configs-3.3-10.el7_5.noarchCopy the code

    Until there is no query

  • Unzip the newly uploaded JDk1.8

    tar -zxvf jdk-8u11-linux-x64.tar.gz
    Copy the code
  • Go to the decompressed directory PWD and copy the absolute path

    / home/JDK/jdk1.8.0 _11Copy the code

  • Edit the system configuration file to configure environment variables

    vim /etc/profile
    Copy the code
    exportJAVA_HOME = / home/JDK/jdk1.8.0 _11export PATH=$PATH:$MAVEN_HOME/bin:$JAVA_HOME/bin
    Copy the code

  • Hold exit and execute the command for it to take effect

    source /etc/profile
    Copy the code
  • Check whether the JDK is installed successfully

    java -version	
    Copy the code

  • The preceding figure shows that the installation is successful

Install SVN

  • Installing the SVN Service

    yum -y install subversion
    Copy the code

    I don’t know when it was installed, it shows that I have installed it

  • Creating a SVN Directory

  • Create a SVN repository named repos

  • Modify the warehouse configuration file

    1. Edit the svnserve. Conf

      vim repos/conf/svnserve.conf
      Copy the code

      Uncomment the following four lines and modify the first line anon-access = None, then save and exit

    2. Modify the passwd file to add a user

      vim repos/conf/passwd
      Copy the code

      Add a user liucong with password 123456 and save and exit

    3. Modify the authz file and assign permissions to create users

      vim repos/conf/authz
      Copy the code

      Set user Liucong to read and write the repos user

  • Starting the SVN Service

    svnserve -d -r /home/svn
    Copy the code
  • Check whether the SVN service is started

    ps ef | grep svn
    Copy the code

  • The warehouse address

    SVN: / / 192.168.0.193 / repos

  • Windows connect

    SVN: / / 192.168.0.193 / repos

    Enter the user name and password set above, remember to disable the firewall before connecting

Jenkins installation and configuration

  • Pull Jenkins

    docker pull jenkins
    Copy the code

  • Look at mirror

    docker images
    Copy the code

  • Create Jenkins directory

    mkdir -p /home/jenkins
    Copy the code
  • Modify the permissions

    chown -R 1000 /home/jenkins/
    Copy the code
  • Start the Jenkins

    docker run -itd -p 9090:8080 -p 50000:50000 --name jenkins --privileged=true  -v /home/jenkins:/var/jenkins_home jenkins:latest
    Copy the code

  • access

    192.168.0.193:9090

    According to the prompt, enter the container for the/var/jenkins_home/secrets/initialAdminPassword files looking for a password

    Docker exec it ddd2e4dcf731 /bin/bash

    View password: cat/var/jenkins_home/secrets/initialAdminPassword

    Password: a762c435c13e41e08df4f5ebe83b2a8a

    Note: in the view/home/Jenkins/secrets/initialAdminPassword can also, because when we start Jenkins with command (see above the green part of the command) specifies the/home/Jenkins directory mounted to the inside of the container/var/jenk Ins_home directory, so all files generated in the container will be generated on the host

  • Enter the installation plug-in.

  • Recommended plug-in installation

  • Emma: Oh, my God. It’s all failed

  • Create an administrator account to facilitate login

  • Click System Management to check the cause of plug-in installation failure, as shown in the picture below. It should be caused by the version being too low

  • As shown in the figure below, download the updated version of the WAR package is provided below, but here we are pulled by Docker, unlike tomcat startup, you can directly change the war package

  • Because docker pulled it directly, I decided to delete the image of the existing version of the container, version 2.190.3

    First stop the existing Jenkins container: docker stop container ID

    Then delete the container: docker rm container ID

    Last delete image: docker RM Image ID

  • Check the latest version of the official website and available for docker pull version

    [Jenkins download and deployment](www.jenkins.io/download/)

  • Docker pulls version 2.190.3

    1. pull

      Docker pull Jenkins: 2.190.3Copy the code

      As shown below, version 2.190.3 is not found

    2. Go to docker’s official website to search, as shown in the picture below

    3. Execute the command above, as shown in the picture below to download, wait until the download is complete and start, repeat the steps above, no repeat textures here

      Docker pull Jenkins/Jenkins: 2.190.3 - centosCopy the code

  • Download successfully. Start

    docker run -itd -p 9090:8080 -p 50000:50000 --name jenkins --privileged=true- v/home/Jenkins: / var/jenkins_home Jenkins/Jenkins: 2.190.3 - centosCopy the code

  • After startup, access 192.168.0.193:9090, view the password login, and reinstall the recommended plug-ins as shown below. Most of them were installed successfully

  • Select continue to create the first user

  • After logging in to the plugin management, the plugin has been installed, found that the Build Time plug-in exists, so I don’t care

  • Enter global tools configuration, configure JKD, Maven, etc…

    Configure settings. XML to find maven’s installation directory, as shown below

    Note: The directory I configured above is still the Maven directory in the Jenkins container. Remember to copy the entire /home/maven/ directory to the /home/jenkins/ directory after modifying the setting. XML file below

    After the configuration shown above, you also need to change the repository address of settings.xml to a domestic site to speed up the download, as shown below

    Vim/home/maven/apache maven - 3.6.3 / conf/Settings. The XMLCopy the code
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        <mirrorOf>central</mirrorOf>       
    </mirror>
    Copy the code

    /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home = /var/jenkins_home /var/jenkins_home/ is a directory that was mounted to /var/jenkins_home/ when the Jenkins container was started En copying to /home/Jenkins produces the same file in the /var/jenkins_home/ directory of the container

    Enter the container to view: as shown below

    Note: The recommended plug-in does not have maven plug-ins. You need to manually search for the installation, enter the plug-in management, and wait until the installation is complete

    Then create a project and upload it to SVN

    Here, I create a SVN repository on SpringTools and upload a project to the repository. The repository address is the same as the repository address after SVN is installed

    Submit the project, as shown in the picture below. Sorry, for the convenience of fast SVN file directory structure is not very standard, please refer to Baidu to establish a standard file directory structure

    Check whether the access is successful. As shown in the following figure, the access is successful. Then configure automatic deployment

  • Create a new Maven project, as shown below

  • The above is the whole process of Jenkins’ pulling and releasing the SVN code. Of course, it is not finished yet. It is just pulling and carrying out basic configuration, without updating and starting our project on the server in real time

    1. First of all, we download a Publish over SSH plug-in from system management ————> Plug-in Management, as shown in the picture below. I have already downloaded it. If you haven’t downloaded it, you can click the optional plug-in to search and download it, which will be used below

    2. Step 2, enter the system configuration for system administration and configure the Docker host related things, as shown in the figure below

      Fill in the above parameters in the location shown above

      PassPhrase: indicates the password of the server

      Name: Any Name

      Hostname: server IP address (192.168.0.193)

      Username: indicates the Username used to log in to the server.

      Remote Diractory: Remote Diractory: Remote Diractory: Remote Diractory: Remote Diractory: Remote Diractory: Remote Diractory: Remote Diractory

      After filling in, click the button in the lower right to test, and then click “Save” to exit

    3. Then click the project to enter the project configuration, as shown below

      Publish over SSH: Publish over SSH: Publish over SSH: Publish over SSH: Publish over SSH

      The parameters are described as follows:

      Name: Select the server we configured earlier in the system administration System configuration

      **/*.jar, which means that all generated JAR packages are moved to the folder under the remote directory configuration

      Remote directory: is the directory I created to put the jar package of the project, as shown in the picture below, is the main file, the code is not needed, here in order to avoid misleading, so the code

      Here I have filled in the absolute path where docker.sh resides, as shown in the figure above

      When you click on Jenkins to build a project, Jenkins will go to SVN and pull the latest one

      The code is compiled and packaged using maven, which we configured, and then executed as shown below

      The parameters we configured will first generate the JAR in the remote directory we configured, and then execute the configured jar

      The script docker.sh also has an advanced button below that needs to be checked as shown in the following image, otherwise the target folder will be generated as shown in the above image

      Note: There is a big hole here, as shown below. Both Pre Steps and Post Steps can be configured as above, so what is the difference? The general meaning of baidu is

      Pre Steps is the pre-processing step, that is, execute the command first, then compile and package the JAR package

      Post Steps are post-processing, that is, package, compile, generate jar package, and then execute command

      I configured the above information in Pre Steps, so I need to build the project twice each time to really see the changes in the SVN code, why? The reason is that the jar package is not packaged and compiled when the command is executed first, so each execution is the last JAR package, and the second build will run the last jar package, so why do you need to build the project twice each time to see the changes in the SVN code, and then changed to Post Steps Compile the package first, then execute the command. The correct step is to compile and execute the script first

    4. The above is the configuration of the whole Jenkins, and the scripts in the server are introduced below. All the files are shown in the picture below. In order to avoid misleading, I have typed the files I created blind, and the two files in the picture below are respectively used when Jenkins configured SVN and Gitlab

      The Dockerfile file is as follows

      FROM java:8
      # the author
       MAINTAINER liucong
      Jar file name = jar file name = jar file name = jar file name = jar file name = jar file name = jar file name
      VOLUME /home/project/spring-boot-01-helloword-svn/
      ADDSpring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. Jar app. The jar
      Run the java-jar command (CMD: this line is executed when the container is started. RUN: this is the path of the jar package >)
      # CMD java -jar /app.jar
      ENTRYPOINT ["java"."-jar"."/app.jar"."--server.port=9999"."-c".">/log/app.log"]
      Set external port to 9999
       EXPOSE 9999
      Copy the code

      The docker.sh script is as follows

      # Operation/project path (Dockerfile)
      BASE_PATH=/home/project/spring-boot-01-helloword-svn
      The source JAR path is where Jenkins was stored after the build
      SOURCE_PATH=/home/jenkins/workspace/spring-boot-01-helloword-svn
      Docker image/container name or jar name is named this
      SERVER_NAME=spring-boot-01-helloword-svn
      Id # container
      CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
      Id # image
      IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}')
      rm -rf $BASE_PATH/ spring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. The jarecho "Latest build code$SOURCE_PATH/ target/spring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. Jar migrated to$BASE_PATH..."
      # Move the project from the Jenkins built directory to our project directory and rename it
       /bin/cp -rf $SOURCE_PATH/ target/spring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. The jar$BASE_PATH/
      Modify file permissions
      #docker stop $SERVER_NAME # Stop running containers
      #docker rm $SERVER_NAME # Delete running container
      #docker rmI $SERVER_NAME
      # Build docker image
              if [ -n "$IID" ]; then
                      echo "There is$SERVER_NAMEMirror, IID =$IID"
                      docker stop $SERVER_NAME # Stop the running container
                      docker rm $SERVER_NAME # Delete the running container
                      docker rmi $SERVER_NAME # delete mirror
                      cd $BASE_PATH
                      docker build -t $SERVER_NAME .
              else
                      echo "There is no$SERVER_NAMEMirror, start building mirror."
                              cd $BASE_PATH
                      docker build -t $SERVER_NAME .
              fi
      #docker stop $SERVER_NAME # Stop running containers
      #docker rm $SERVER_NAME # Delete running container
      #docker rmI $SERVER_NAME
      Run the Docker container
      # --name docker-test The name of the container is docker-test
      -d The container is running in the background
      # -p 3636:3636 Specifies that the port mapped by the container and the port mapped by the host are 3636
      # -v /usr/ms_backend/:/usr/ms_backend/ Mount the /usr/ms_backend/ directory of the host to the /usr/ms_backend/ directory of the container (not less than once)Docker run --name docker run --name$SERVER_NAME -v $BASE_PATH:$BASE_PATH -p 9999:9999 -d $SERVER_NAME
      echo "$SERVER_NAMEContainer created"
      Copy the code

  • The above is all the configuration of docker+ SVN + Jenkins + Maven automatic deployment

    1. First look at the Java file and return Hello Word, as shown below

    2. Jenkins build

    3. access

      http://192.168.0.193:9999/springboot/hello

      Does return Hello Word (port 9999 is configured in Dockerfile, and the image docker runs on also specifies port 9999)

    4. Modifying Java files

      Change to Hello Liucong and submit to SVN

    5. To build again

    6. To visit again

      http://192.168.0.193:9999/springboot/hello

      If Hello Liucong is returned, the process is successfully deployed

    7. This is the deployment configuration of the entire process

Gitlab is automatically deployed, installed and configured

The environment

  1. gitLab:

  2. Environment is the same as above, gitLab is added

For other configurations, refer to the above files. This time, we mainly install the configurations above gitLab, Nginx and Jenkins

GitLab installation

  • GitLab image pull

    docker pull gitlab/gitlab-ce
    Copy the code

    Gitlab-ce is a stable version. If the version number is not specified, the latest version is selected by default

  • Look at mirror

    docker images
    Copy the code

  • Run the mirror

    docker run -d  -p 443:443 -p 80:80 -p 222:22 --name gitlab --restart always -v /home/gitlab/config:/etc/gitlab -v /home/gitlab/logs:/var/log/gitlab -v /home/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce
    Copy the code

    -d: background running

    -p: maps ports inside a container outward

    –name: indicates the name of the container

    -v: mounts the data folder or log configuration file in the container to the host directory

  • configuration

    The gitLab container works fine as described above, but when creating a project on GitLab, the URL access address of the generated project is generated according to the hostname of the container, which is the container ID. As gitlab server, we need a fixed URL to access the address, then need to configure the gitlab. Rb (hosting path: / home/gitlab/config/gitlab rb)

    1. Edit the file and modify the following contents

      vim /home/gitlab/config/gitlab.rb
      Copy the code

      External_url ‘http://192.168.0.193′(Specifies the access address used for the HTTP protocol. The default port number is 80.)

      Gitlab_rails [‘gitlab_ssh_host’] = ‘192.168.0.193’(set SSH access address and port)

      Gitlab_rails [‘gitlab_shell_ssh_port’] = 222(this port is 222 mapped from port 22 when run)

    2. Save the exit

  • Restart the GitLab container

    docker restart gitlab
    Copy the code

  • access

    http://192.168.0.193

    (because my gitlab port is 80, so the browser URL does not need to enter the port number, if the port number is not 80, then open as: IP: port number), enter the new root user password for the first time, set only confirm line, then click login

  • Registered account

  • Create a project, again using the project in the tutorial above as an example

  • Open local Git bash and generate an SSH private key using the following command

    1. Generate the private key

      ssh-keygen -t rsa -C '***********@gmail.com'
      Copy the code

      The public and private keys here generate the following directories

      Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.

      Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.

    2. Look at the private key

      cat ~/.ssh/id_rsa.pub
      Copy the code

      ~ denotes the user directory, for example c:\Users\Administrator on my Windows

      ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCiKZTTL8mMmsh4YZhF4cICbpwJlpXDlzxxrPAyldf7Ywh47EYcMZ9YrcLkifQeiMrPk+a6wn7Q9HKjOcZFBRS8g8QW NMD/+S6u6hXuhgePwFPYxg6mTDDNpyaw YcxislxLIL3rPYbMg2gmX+X2gH7Vm1waiUon2SV780s3dgAq+XlCLQ14dB60+XhMu+27BcxVKdXOku56 JILvmbK4Ww2B2A6Gxsh3xGDOga8bouoNlP2mlQipEs2W+y6+xkrNgS7XoKT/EbPKKvGd/lCEnApcmDNSONt5KCQEWbpQGPLna9vgfb6rQ/dF6g9HMwJZh+nJ [email protected]

    3. Configuration gitLab

      Go to Pofile Stting — SSH Keys — Add SSH Key and paste what you copied in the previous step into the text box corresponding to the Key

    4. Upload a new folder to Git

      Open the command box, go to FDliucong, and set the username and mailbox

      Git config — global user.name

      Git config — global user.email “[email protected]

      Then clone the project

      git cloneSSH: / / [email protected]:222 / liucong/spring - the boot - 01 - helloword. GitCopy the code

      The address is from the following figure

      When completed, the project appears in the FDLIUcong folder

      But this is just an empty project, you need to copy the other code into the spring-boot-01-Helloword directory and upload it to Git by command, as shown below

      First go into the project directory,

      cd spring-boot-010helloword/

      You then add the project with the add command, essentially adding the file to the staging area

      Git add * (* means all files and directories) and commit the project

      Git commit — m

      Finally, push to Git with the push command

      git push origin master

      There it is on Git

    5. The above is the code upload on GitLab. Of course, there is more than this way. The tool I use below is IDEA, which can be selected by myself

Jenkins installation and configuration

  • In order to distinguish, the projects are suffixed respectively to represent the code pulled from SVN and Gitlab

  • Click on the project to configure

    The default master below is the branch name. I’m publishing the trunk by default, but I can also publish a branch

  • Click Build, as shown below

    Building a successful

  • Jenkins script configuration, as well as server script

    The configuration is basically the same as that of the SVN, but the server file directory is different. Therefore, the configuration must be changed

  • The server directory is as follows

    The top is gitlab configuration, and the bottom is SVN configuration. In fact, the file content is the same

    The Dockerfile and docker.sh files are the same as the files in the SVN directory above. Some changes need to be made to change the directory name and port

    Dockerfile file

    FROM java:8
    # the author
     MAINTAINER liucong
    Jar file name = jar file name = jar file name = jar file name = jar file name = jar file name = jar file name
    VOLUME /home/project/spring-boot-01-helloword-gitlab/
    ADDSpring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. Jar app. The jar
    Run the java-jar command (CMD: this line is executed when the container is started. RUN: this is the path of the jar package >)
    # CMD java -jar /app.jar
    ENTRYPOINT ["java"."-jar"."/app.jar"."--server.port=9999"."-c".">/log/app.log"]
    Set the external port number to 7799
     EXPOSE 7799
    Copy the code

    Docker. Sh file

    # Operation/project path (Dockerfile)
    BASE_PATH=/home/project/spring-boot-01-helloword-gitlab
    The source JAR path is where Jenkins was stored after the build
    SOURCE_PATH=/home/jenkins/workspace/spring-boot-01-helloword-gitlab
    Docker image/container name or jar name is named this
    SERVER_NAME=spring-boot-01-helloword-gitlab
    Id # container
    CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
    Id # image
    IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}')
    rm -rf $BASE_PATH/ spring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. The jarecho "Latest build code$SOURCE_PATH/ target/spring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. Jar migrated to$BASE_PATH..."
    # Move the project from the Jenkins built directory to our project directory and rename it
     /bin/cp -rf $SOURCE_PATH/ target/spring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. The jar$BASE_PATH/
    Modify file permissions
     chmod -R 777 $BASE_PATH/ spring - the boot - 01 - helloword - 0.0.1 - the SNAPSHOT. The jarecho "Migration completed"
    #docker stop $SERVER_NAME # Stop running containers
    #docker rm $SERVER_NAME # Delete running container
    #docker rmI $SERVER_NAME
    # Build docker image
            if [ -n "$IID" ]; then
                    echo "There is$SERVER_NAMEMirror, IID =$IID" 
                    docker stop $SERVER_NAME # Stop the running container
                    docker rm $SERVER_NAME # Delete the running container
                    docker rmi $SERVER_NAME # delete mirror
                    cd $BASE_PATH
                    docker build -t $SERVER_NAME .
            else
                    echo "There is no$SERVER_NAMEMirror, start building mirror."
                            cd $BASE_PATH
                    docker build -t $SERVER_NAME .
            fi
    #docker stop $SERVER_NAME # Stop running containers
    #docker rm $SERVER_NAME # Delete running container
    #docker rmI $SERVER_NAME
    Run the Docker container
    # --name docker-test The name of the container is docker-test
    -d The container is running in the background
    # -p 3636:3636 Specifies that the port mapped by the container and the port mapped by the host are 3636
    # -v /usr/ms_backend/:/usr/ms_backend/ Mount the /usr/ms_backend/ directory of the host to the /usr/ms_backend/ directory of the container (not less than once)Docker run --name docker run --name$SERVER_NAME -v $BASE_PATH:$BASE_PATH -p 7799:9999 -d $SERVER_NAME
    echo "$SERVER_NAMEContainer created"
    Copy the code

    build

  • access

    http://192.168.0.193:7799/springboot/hello

    Change the source code, upload it to GitLab, and publish it again

    Go to http://192.168.0.193:7799/springboot/hello

  • Pay attention to

    Dockerfile The port in the following figure must be the same as the port in the following figure. Otherwise, the port cannot be accessed

    Red boxes correspond to red boxes

    A yellow box corresponds to a yellow box