Build Jenkins+springcloud one-click automatic deployment

Use jenkins to realize automatic project deployment notes

1. Purpose

1. One-click realization of project automatic construction, packaging and release (for production environment)

2. Realize that the code is submitted to the code warehouse and automatically packaged and deployed immediately (for the test environment)

3. The test script code is at the end

2. Operating environment

  • hundreds

  • jdk

  • maven

  • jenkins

  • gitee

  • git

  • docker

2. Implementation idea

1. Production environment

  1. Merge the code development branch to the master branch, ready to release the latest code

  2. Jenkins one-click release

  3. Jenkins pulls the latest code from the code repository

  4. Packaging via maven

  5. Package the jar package into a mirror image and upload it to your own mirror warehouse private server

  6. Jenkins notifies the application server to pull the latest image

  7. The application server pulls the latest image and runs the new container

  8. Delete old containers and images

  9. If the release fails, roll back to the previous version

2. Test environment, development environment

  1. Submit code to code warehouse dev or other branches

  2. Automatically trigger jenkins to build the project

  3. Jenkins pulls the latest code from the code repository

  4. Copy the latest code to the test server

  5. Automatically connect to the test server for maven packaging

  6. Package the jar package into a mirror image

  7. Delete old containers and images

  8. Run the new container from the newly packaged image

3. Preliminary preparation

  1. Two centos servers can ping each other (or two virtual machines)

  2. Prepare a springcloud or springboot project for automatic release testing (I am preparing a multi-module project here, the directory structure is as follows)

    picture

  3. jdk 1.8 installation package, maven 3.6 package

  • jdk download address: https://wp.boytian.com/#/s/p8Fv password tiantian123

  • maven download address: https://wp.boytian.com/#/s/2lc1 password tiantian321

4. Install jdk environment and maven environment

It is best to install both, here is a demonstration of a server

1. jdk environment installation

  1. Create jdk storage directorymkdir -p /home/jdk

  2. Upload jdk to this directory, enter this directory and decompress, decompress command:tar -zxvf jdk-8u144-linux-x64.tar.g

    picture

2. maven installation

  1. Create maven directory and maven warehouse directory
  • mkdir -p /home/maven

  • mkdir -p /home/maven/repo

  1. Upload maven to /home/maven/the directory, enter the directory and decompress, decompress command:tar -zxvf apache-maven-3.6.3

3. Configure the environment variables of jdk and maven

  1. modify /etc/profilethe file, the commandvim /etc/profile

  2. If the directory creation is consistent with mine, add the following configuration at the bottom, otherwise you can modify it yourself

    #输入i 进入编辑模式
    
    # java配置
    export JAVA_HOME=/home/jdk/jdk1.8.0_144
    export PATH=$JAVA_HOME/bin:$PATH
    export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/bin
    
    #maven 配置
    export MAVEN_HOME=/home/maven/apache-maven-3.6.3
    export PATH=$MAVEN_HOME/bin:$PATH
    
    
  3. Refresh the configuration to take effect

  • source /etc/profile
  1. To test whether the environment is effective, the commandjava mvn -v

    picture

  2. Another server host is also configured

5. Configure secret-free login (two servers log in without secret each other)

  1. Enter ssh-keygen -t rsaand hit three carriage returns

  2. Enter ssh-copy-id -i ~/.ssh/id_rsa.pub ip地址Enter

  3. Enter the password verification of the second server to realize password-free login

  4. In the same way, configure the second server to log in to the first server without password

Reference: https://blog.csdn.net/u013415591/article/details/81943189

6. Install the jenkin service on the first server

1. Execute the following commands in sequence on the first server installation
  • sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

  • sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

  • yum install jenkins

  • ln -s /home/jdk/jdk1.8.0_144/bin/java /usr/bin/java

  • sudo ln -s /home/maven/apache-maven-3.6.3 /usr/local/maven

  • systemctl start jenkins

(1, 2, 3) is to install jenkins, (4, 5) is the soft link environment command to the usr directory (6) start Jenkins

2. Modify the authority of jenkins to root authority, so as to facilitate the problem of insufficient authority to execute commands in the future
# 打开配置文件
vim /etc/sysconfig/jenkins
# 修改$JENKINS_USER
$JENKINS_USER="root"

#修改Jenkins相关文件夹用户权限
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins

#重启
systemctl restart jenkins

picture

picture

1619053352839

7. Access the jenkins service for some basic configuration

  1. Browser access: ip:8080

picture

1618994599246

  1. Get the password command from the server:cat /var/lib/jenkins/secrets/initialAdminPassword

picture

1618995408605

  1. Install custom plugins

    picture

picture

1618995513826

  1. Create an administrator account

    picture

  2. After clicking Finish, restart the jenkins service, command:systemctl restart jenkins

  3. Jenkins configures the global jdk and maven environment

    picture

picture

1618996436490

picture

1618996573737

picture

1618996616938

  1. Install the gitee plugin, push code to gitee is mainly triggered by this plugin

    picture

  2. Configure gitee interaction credentials

    picture

    picture

    picture

  3. Global configuration gitee

    picture

8. Prepare to deploy the project

  1. Create gitee code repository

picture

1619050043509

9. Configure jenkins to start configuring automatic deployment

  1. Create an automated build process choosing Freestyle

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tAOlG9N1-1637893426234)(https://mmbiz.qpic.cn/mmbiz_png/0bmE6dS9CgJQUEbkCyje2ajToYicZGj4rVheOTDiahK1yzXSvdpwsAOsNeiaibia91ZwxkAG0QLEpv6hJmdNEGIeksA/640?wx_fmt=png )]

1618997421632

  1. Select parametric build

    Define some variables, which are used in the scriptpicture

picture

1619050337908

picture

1619054535420

3. Configure the git repository

picture

1619050733823

picture

1619050777451

picture

1619050871896

  1. Build automatic triggers, configure jenkinspicture

  2. Build triggers, configure gitee

picture

1619051250412

  1. Test whether the build works

    Configure jenkins to add build stepspicture

picture

1619051394272

Click Apply to save

  1. Test manual builds

picture

1619051457074

picture

1619051489297

picture

1619051513195

  1. Test automated builds

    Manually push the code to see if it will start an automatic build

    picture

    picture

    picture

    picture

10. Automated deployment scripts

The automatic build configuration is completed, and then write a script for the real deployment project

The content of the script is as follows. Currently, the function is implemented but it is still not perfect. It will be improved later

  1. Project deployment feet

    
    # 项目模块统一变量设置
    
    # 公共服务
    CLOUD_CONTAINER="cloud"
    CLOUD_IMAGE="cloud_server"
    CLOUD_PORT=8002
    # 微信小程序模块
    FAMILY_CONTAINER="family"
    FAMILY_IMAGE="family_server"
    FAMILY_PORT=8003
    # 鉴权模块
    AUTH_CONTAINER="auth"
    AUTH_IMAGE="auth_server"
    AUTH_PORT=8004
    # 网关
    GATEWAY_CONTAINER="gateway"
    GATEWAY_IMAGE="gateway_server"
    GATEWAY_PORT=9999
    
    
    # 删除应用服务器旧的代码(在第二个服务器创建好/home/project/fym-platform/目录,否则报错)
    ssh -Tq ${host} <<EOF
    rm -rf /home/project/fym-platform/*
    EOF
    
    # 拷贝新代码到应用服务器
    echo "拷贝代码开始..."
    scp -r ./* ${host}:/home/project/fym-platform
    echo "拷贝代码结束..."
    
    # 登录到项目服务器执行命令
    ssh -Tq ${host} <<EOF
      cd /home/project/fym-platform
     echo "项目开始构建打包"
      mvn clean package -Dmaven.test.skip=true -P${environment}
    
     echo "jar文件拷贝"
      mkdir -p /web/project/fym-platform
      cp -r gateway-server/target/gateway-server*.jar  Dockerfile /web/project/fym-platform/
      cp -r fym-service/*/*/target/*server*.jar /web/project/fym-platform/
    
     echo "=============== 开始部署容器 ==============="
      # 停止并删除旧的容器
      docker ps -a|grep $CLOUD_CONTAINER &&  docker stop $CLOUD_CONTAINER && docker rm $CLOUD_CONTAINER || echo "not exist"
      docker ps -a|grep $GATEWAY_CONTAINER &&  docker stop $GATEWAY_CONTAINER && docker rm $GATEWAY_CONTAINER || echo "not exist"
      docker ps -a|grep $FAMILY_CONTAINER &&  docker stop $FAMILY_CONTAINER && docker rm $FAMILY_CONTAINER || echo "not exist"
      docker ps -a|grep $AUTH_CONTAINER &&  docker stop $AUTH_CONTAINER && docker rm $AUTH_CONTAINER || echo "not exist"
    
     # 删除旧的镜像
      docker images |grep  $CLOUD_IMAGE  && docker rmi -f $CLOUD_IMAGE  || echo  "not exist"
      docker images |grep  $GATEWAY_IMAGE && docker rmi -f $GATEWAY_IMAGE || echo  "not exist"
      docker images |grep  $FAMILY_IMAGE && docker rmi -f $FAMILY_IMAGE || echo  "not exist"
      docker images |grep  $AUTH_IMAGE && docker rmi -f $AUTH_IMAGE || echo  "not exist"
    
     # 构建新的镜像
      docker build --build-arg JAR_FILE=cloud-server-1.0.jar -t $CLOUD_IMAGE  /web/project/fym-platform/
      docker build --build-arg JAR_FILE=gateway-server-1.0.jar  -t $GATEWAY_IMAGE /web/project/fym-platform/
      docker build --build-arg JAR_FILE=family-server-1.0.jar  -t $FAMILY_IMAGE /web/project/fym-platform/
      docker build --build-arg JAR_FILE=auth-server-1.0.jar  -t $AUTH_IMAGE /web/project/fym-platform/
    
     # 运行镜像容器
      docker run -it -d --name $CLOUD_CONTAINER -p $CLOUD_PORT:$CLOUD_PORT $CLOUD_IMAGE
      docker run -it -d --name $GATEWAY_CONTAINER -p $GATEWAY_PORT:$GATEWAY_PORT $GATEWAY_IMAGE
      docker run -it -d --name $FAMILY_CONTAINER -p $FAMILY_PORT:$FAMILY_PORT $FAMILY_IMAGE
      docker run -it -d --name $AUTH_CONTAINER -p $AUTH_PORT:$AUTH_PORT $AUTH_IMAGE
    EOF
    
    echo "部署成功"
    
    
    
  2. jar image packaging script

    FROM primetoninc/jdk:1.8
    ARG JAR_FILE
    MAINTAINER   [email protected]
    COPY ${JAR_FILE} app.jar
    ENV JAVA_OPTS="-Xmx512m -Xms512m -Xmn512m -Xss256K -XX:SurvivorRatio=8 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:+UseConcMarkSweepGC -XX:ConcGCThreads=4 -XX:+CMSClassUnloadingEnabled -Dfile.encoding=UTF-8 -XX:+DisableExplicitGC -XX:+PrintGC -XX:+PrintGCTimeStamps -Xloggc:./gc.log"
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
    
    
    
  3. Modify the jenkins shell configuration just now as follows

    #!/bin/bash
    chmod u+x run.sh # 一行是赋予脚本执行权限
    ./run.sh #一行是执行脚本
    
    

picture

1619052201097

11. Test automatic build and manual build

  1. Manually trigger the build (the same is true for automatic triggering when submitting code), check the output console, and the maven dependencies have been downloaded and packaged

picture

1619053964419

picture

1619054134274

picture

1619054194717

  1. View the second server docker image

picture

1619054218370

picture

1619054239768

  1. View nacos service startup status

    picture

  2. Test whether the project is deployed successfully

Guess you like

Origin blog.csdn.net/weixin_44078653/article/details/121554142