"Internet Architecture" Software Architecture-Jenkins Construction and Maven gitlab Automated Deployment Process (Part 2) (6)

The environment was basically set up last time, and the next step is to start the automated deployment of the entire process. Source code: https://github.com/limingios/netFuture/tree/master/jenkins/

Environmental review

application IP address service port Install the app Installation method
gitlab 192.168.66.100 gitlab 10080 gitlab docker
jenkins 192.168.66.101 jenkins 8888 jdk8 maven3.2 git2.8 shell
nexus 192.168.66.102 nexus 8081 nexus docker
tomcat 192.168.66.103 tomcat 8080 tomcat docker
IP address port username password
192.168.66.100 10080 root 123456789qwe
192.168.66.101 8888 root 8d31833e277c4b579a3be35fe2bdc7d4
192.168.66.102 8081 admin admin123
192.168.66.103 8080

Deploy local projects to 100 on gitlab

  • Native code

The code is relatively simple for the process


  • Submit locally to gitlab

Modify the configuration, you can submit the code non-locally

You can create a new user to build a warehouse, or use root to build a warehouse.

Prompt that no ssh key is uploaded

View the local ssh key, use git bash directly in the window environment

ssh-keygen -t rsa -C "[email protected]"
cat /c/Users/Administrator/.ssh/id_rsa.pub

View the ssh secret key of jenkins, use the 101 environment directly

ssh-keygen -t rsa -C "[email protected]"
cat /root/.ssh/id_rsa.pub

The local windows and 101 jenkins have been added

Local windows code submission

git init
git add .
git commit -m "注释语句"
git remote add origin ssh://[email protected]:10022/root/idig8.git
git push -u origin master

Deploy local project to Jenkins 101

  • Global security configuration

  • Create task

Write the password at will, wait for the trigger setting in gitlab
Use the following URL to trigger build remotely: JENKINS_URL/job/idig8/build?token=TOKEN_NAME or /buildWithParameters?token=TOKEN_NAME

100 gitlab trigger setting
http://192.168.66.101:8888/job/idig8/build?token=123456
The value of token is set on jenkins.

Wait for a special preparation of the pipeline

Save

101 configuration private server nexus

cd .m2
pwd
vi settings.xml

settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <pluginGroups></pluginGroups>
  <proxies></proxies>

  <servers>
      <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <mirrors> 
    <mirror> 
      <id>nexus-releases</id> 
      <mirrorOf>*</mirrorOf> 
      <url>http://192.168.66.102:8081/nexus/content/groups/public</url> 
    </mirror>
    <mirror> 
      <id>nexus-snapshots</id> 
      <mirrorOf>*</mirrorOf> 
      <url>http://192.168.66.102:8081/nexus/content/repositories/snapshots</url> 
    </mirror> 
  </mirrors> 
 
  <profiles>
   <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>nexus-releases</id>
          <url>http://nexus-releases</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-snapshots</id>
          <url>http://nexus-snapshots</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
         <pluginRepository>
                <id>nexus-releases</id>
                 <url>http://nexus-releases</url>
                 <releases><enabled>true</enabled></releases>
                 <snapshots><enabled>true</enabled></snapshots>
               </pluginRepository>
               <pluginRepository>
                 <id>nexus-snapshots</id>
                  <url>http://nexus-snapshots</url>
                <releases><enabled>true</enabled></releases>
                 <snapshots><enabled>true</enabled></snapshots>
             </pluginRepository>
         </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
      <activeProfile>nexus</activeProfile>
  </activeProfiles>
 
</settings>

Install sshpass, ssh login cannot specify a password in the command line. The emergence of sshpass solved this problem. sshpass is used for password authentication of non-interactive SSH. It is generally used in sh scripts without the need to enter the password again.

 yum -y install sshpass

Test gitlab and jenkins

Formally written

  • pipeline
#!groovy
pipeline {
    
    
    agent any
    //环境变量,
    environment {
    
    
        REPOSITORY="ssh://[email protected]:10022/root/idig8.git"
        MODULE="idig8"
        SCRIPT_PATH="/root/"
        remoteHost_tomcat= '192.168.66.103'
    
    }
    //流水线是如何提前,都是通过很多个stages下面的stage
    stages {
    
    
        stage('获取代码'){
    
    
            steps{
    
    
                echo " start fetch code from git ssh://[email protected]:10022/root/idig8.git"
                deleteDir()
                git "${REPOSITORY}"
            }
        }
        stage('代码静态检查') {
    
    
            steps{
    
    
                echo " start code check"
            }
        }
        stage('编译+单元测试') {
    
    
            steps{
    
    
                echo " start compile"
                 sh"""
         cd $workspace/idig8/common-parent/
         mvn  -U clean install -Dmvn.test.skip=true -Ptest
         """
            }
        }
        
        stage('jar包上传到nexus上') {
    
    
            steps{
    
    
                echo " start maven update jar"
                 sh"""
         cd $workspace/idig8/common-parent/common-utils
         mvn clean deploy
         cd $workspace/idig8/common-parent/common-dao
         mvn clean deploy
         cd $workspace/idig8/common-parent/common-service
         mvn clean deploy
         """
            }
        }
        
        stage('移动目录到用户目录下') {
    
    
            steps{
    
    
                echo "mv idig8.war"
                 sh"""
         mv -f $workspace/idig8/common-parent/common-web/target/common-web.war ${SCRIPT_PATH}${MODULE}.war
         """
            }
        }
        stage('拷贝文件到tomcat指定目录下') {
    
    
        
            steps{
    
    
                echo "scp 目标文件"
                 sh"""
         sshpass -p 'vagrant' ssh -p 22 -o stricthostkeychecking=no root@${remoteHost_tomcat} 'rm -rf /root/tomcat/tomcat-persistence/tomcat/data/${MODULE}*';
         sshpass -p 'vagrant' scp -P 22 -o stricthostkeychecking=no ${SCRIPT_PATH}${MODULE}.war root@${remoteHost_tomcat}:/root/tomcat/tomcat-persistence/tomcat/data/${MODULE}.war;
         """
         echo "打包完毕美滋滋 刷新tomcat查看吧"
            }
        }
        
    }
}

As long as the push code pipeline starts to work automatically, it's really beautiful

As a result, the preparation of the 20-round pipeline was finally successful.
Delete all the directories of idig mounted by docker, and then replace the new jar package into it.

PS: I completed the automated deployment and finally completed it. I have been doing it in my own virtual machine environment for 3 days. I feel a sense of accomplishment. I prefer shell script deployment and learning. The pipeline is not that troublesome. Most of it was done by shell, thanks to two years of shell development. I have not forgotten that the things I have to understand in the first 2 years are crucial to my future path. The ssh public key also plays a very important role in it. I hope that the old iron can easily complete the deployment according to my ideas. Thanks to Lao Tie for his support. I originally wanted to write the two articles together, but later they were separated because the content was too substantial. In order to do this every day until 2 o'clock in the evening, it is not easy to get off work. Then it's a goodbye~ too late to shake hands!

Guess you like

Origin blog.csdn.net/zhugeaming2018/article/details/108215127