Jenkins remote deployment application

System Configuration

Jenkins detailed installation steps: https://blog.csdn.net/qq_37640410/article/details/110921651

  • Click Manage Jenkins->Configure System (System Configuration) Insert picture description here
    Publish Over SSH ConfigurationInsert picture description here
    Passphrase: private key password, if not, you can leave it
    Path to key: location of
    private key Key: content of private key. If this value is filled in, the content of Path to key will be overwritten.
    SSH Servers: server configuration
    • Name: name (custom)
    • Hostname: server address
    • Username: username
    • Remote Directory: The address of the default remote server
  • Global tool configuration
    Insert picture description here
    • Maven configuration
      Insert picture description here
    • Jdk configuration
      Insert picture description here
    • Maven installation path
      Insert picture description here
      Click the "Apply" and "Save" buttons.

Project deployment

  • Create a new task: name: springboot_design_patternInsert picture description here

  • My view-configuration

    Source code management
    Insert picture description here
    Repository URL: warehouse address
    Credentials: login credentials, configure
    Branches to build from credential management :
    build and
    Insert picture description here
    execute shell scripts in the branch
    Insert picture description here

    #!/bin/bash
    # #!/bin/bash是指此脚本使用/bin/bash来解释执行。其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径。bash只是shell的一种,还有很多其它shell,如:sh,csh,ksh,tcsh,...
    # #!/bin/bash只能放在第一行,如果后面还有#!,那么只能看成是注释。
    
    #服务名称
    
    SERVER_NAME=springboot_design_pattern
    
    #源jar路径,mm打包完成之后,target目录下的jar包名称,也可选择成为war包,war包 可移动到Tomcat的。
    JAR_NAME=springboot_design_pattern-${
          
          version}
    
    
    #target打包生成jar包的目录
    
    JAR_PATH=/var/lib/jenkins/workspace/springboot_design_pattern/default/target    # 以具体的打包位置为准,可以先构建一次项目,通过日志查看打包的目录
    
    #打包完成之后,把iar包移动到运行jar包的目录
    JAR_WORK_PATH=/var/lib/jenkins/workspace/springboot_design_pattern/default/target/classes
    
    # echo "查询进程id-->$SERVER_NAME"
    # PID=`ps -ef | grep "$SERVER_NAME" | awk '{print $2}'`
    # echo"得到进程ID: $PID"
    # echo"结束进程"
    # for id in $PID
    # do
    # kill -9 $id
    # echo "killed $id" 
    # done
    # echo"结束进程完成"
    
    #复制jar包到执行目录_
    
    echo" 复制jar包到执行目录:cp $JAR_PATH/$JAR_NAME.jar $JAR_WORK_PATH"
    cp $JAR_PATH/$JAR_NAME.jar $JAR_WORK_PATH
    cd $JAR_WORK_PATH
    chmod 755 $JAR_NAME.jar
    
    

    Added post-build operation
    Insert picture description here
    Name: remote service name,
    Source files in Publish Over SSH in configuration management-system configuration : packaged path
    Remove prefix: remove prefix
    Remote directory remote directory, after configuration in system configuration, no need to configure
    Exec command shell command executed on the remote machine

    shell.sh

    o "当前所在位置"
    pwd
    SERVER_NAME=springboot_design_pattern
    #容器id
    CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
    #镜像id
    IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}')
    #当前日期
    DATE=`date +%Y%m%d`
    
    #清除旧容器
    if [ -n "$CID" ]; then
    echo "存在$SERVER_NAME容器,CID=$CID"
    echo "停止旧容器"
    docker stop $SERVER_NAME
    echo "删除旧容器"
    docker rm $SERVER_NAME
    fi
    
    # 清楚旧镜像
    if [ -n "$IID" ]; then
    echo "存在$SERVER_NAME镜像,IID=$IID"
    echo "删除镜像"
    docker rmi $IID
    fi
    
    mv ./classes/Dockerfile ./
    rm -rf classes/
    
    #构建镜像
    echo "开始构建镜像"
    docker build -f Dockerfile -t $SERVER_NAME:v${
          
          DATE} .
    echo "构建镜像成功!"
    
    # 运行docker容器
    echo "创建并启动$SERVER_NAME容器..."
    docker run --name $SERVER_NAME -d -p 8090:8088 $SERVER_NAME:v${
          
          DATE}
    echo "$SERVER_NAME容器启动完成"
    echo "删除其他配置信息"
    rm -rf ./Dockerfile ./*.jar
    

    Dockerfile

    FROM java:8
    MAINTAINER zfl zfl_aliases@163.com 2020-12-09
    COPY springboot_design_pattern-1.0-SNAPSHOT.jar app.jar
    EXPOSE 8088
    ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-Duser.timezone=GMT+08", "-jar", "/app.jar"]
    

    Click the "Apply" and "Save" buttons

Project construction

Click Build to Insert picture description here
view the console output
Insert picture description here
remote serverInsert picture description here

Guess you like

Origin blog.csdn.net/qq_37640410/article/details/110931720