New task of Jenkins notes

@

Jenkins notes

New task in Jenkins notes: https://blog.csdn.net/weixin_...

Configure remote server for Jenkins notes: https://blog.csdn.net/weixin_...

Jenkins: parametric Construction: multi branch | multi module | rollback | print log: https://blog.csdn.net/weixin_...

1 ` necessary conditions: installation environment Docker or JDK+Maven+Git/Svn

2 ` add method: configure from zero and copy configuration

  • Configure from zero

    • Click the function list on the left - new task

    • Fill in the task name, click to build a maven project, and click OK

    • Enter the configuration list

    • Configure build history
    • Set source code management and select git here


      Choose one of a variety of authentication methods

    • Build trigger: set according to your own needs. Simple tasks do not need to be configured.

    • Build environment: the command line of the remote server can be configured here, but it is best to execute the script after the build is completed (Post Steps), and then execute the remote service script through Post Steps to print the log.

    • Pre Steps Clean before install

    • Post Steps you can choose from a variety of publishing steps

      • Execute directly through scripts on the Jenkis server

      [the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-imzrhbwg-1629689671588) (C: \ users \ administrator \ appdata \ roaming \ typora \ typora user images \ image-20210823104307405. PNG)]

      The command to execute the script requires a root path

      scp -P 2021 /data/jenkins/workdir/jobs/xxx/workspace/xxx/target/xxx.jar devops@Intranet IP:/home/devops/xxx/
      
      ssh -p 2021 devops@Intranet IP "/data/initsh/xxx.sh"
      • Execute the script on the target server and configure the running process: after Jenkins is built, connect to the target server, copy the Jar of the Sources file file to the remote service path, and then run the Exec Command script.

        Jump to -- "configure remote server"

        The configuration is as follows:

        • Source files * * / * indicates sskzmz all files and directories in the job's working directory.
        • Remove prefix this operation is for the above source files directory and will remove the matching directory. Usually left blank.
        • Remote directory this operation is based on the set server directory. My server configuration here is / www. so I should write sites/sskzmz here.
        • Exec command command the command executed by the remote server. For example, you can output service nginx restart or / www/xx. sh.
        • Advanced:
        • Exclude files: excluded files (useful when you transfer directories, use wildcards, for example: / *. log,/*.tmp,.Git /)
        • Pattern separator: separator (configure the separator of Transfer Set Source files. If you change here, the above contents also need to be changed)
        • No default excludes: disable default exclusion rules (see help for details)
        • Make empty dirs: this option changes the default behavior of the plug-in. The default behavior is to match whether the file exists. If it exists, create a directory to store it. Selecting this option will directly create a directory to store files, even if it is empty. (personal understanding)
        • Remote directory is a date format: create a folder with a date in the remote directory (you need to configure the date format in the remote directory)
        • Flatten files: only upload files without creating directories (except remote directories)
        • Exec timeout (ms): timeout of running footsteps (MS)
        • Exec in pty: simulate the execution of a terminal
        • Add Transfer Set: add a configuration
      • sh script (the two methods are consistent)

        #!/bin/bash
        #Service name
        SERVER_NAME=xxx
        # Source jar path. After the mvn package is completed, the jar package name under the target directory can also be selected as a war package. The war package can be moved to the webapps directory of Tomcat to run. Here, the jar package is used and executed with the Java jar command  
        JAR_NAME=xxx
        # Source jar path  
        #/usr/local/jenkins_ Home / workspace - > Jenkins working directory
        #demo project directory
        #Directory of jar package generated by target package
        JAR_PATH=/home/devops/xxx/
        # After packaging, move the jar package to the directory where the jar package runs -- > work_daemon,work_ The daemon directory needs to be created in advance
        JAR_WORK_PATH=/data/project/xxx/
        
        echo "Query process id-->$SERVER_NAME"
        PID=`ps -ef | grep "$SERVER_NAME" | awk '{print $2}'`
        echo "Get process ID: $PID"
        echo "End process"
        for id in $PID
        do
            kill -9 $id  
            echo "killed $id"  
        done
        echo "End process complete"
        
        #Copy the jar package to the execution directory
        echo "copy jar Package to execution directory:cp $JAR_PATH/$JAR_NAME.jar $JAR_WORK_PATH"
        cp $JAR_PATH/$JAR_NAME.jar $JAR_WORK_PATH
        echo "copy jar Package complete"
        cd $JAR_WORK_PATH
        #Modify file permissions
        chmod 755 $JAR_NAME.jar
        
        nohup /usr/local/java/bin/java -jar  $JAR_NAME.jar   >> /data/project/xxx/logs/info.log 2>&1 &
    • Extra additional reference script
      #!/bin/bash  
      #Here you can replace it with your own executor, and other codes do not need to be changed  
      APP_NAME=xxxxxxxx.jar  
      
      #Instructions to prompt for parameters  
      usage() {  
          echo "Usage: sh Execute script.sh [start|stop|restart|status]"  
          exit 1  
      }  
      
      #Check that the program is running  
      is_exist(){  
        pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `  
        #Returns 1 if it does not exist and 0 if it exists       
        if [ -z "${pid}" ]; then  
         return 1  
        else  
          return 0  
        fi  
      }  
      
      #Start method  
      start(){  
        is_exist  
        if [ $? -eq "0" ]; then  
          echo "${APP_NAME} is already running. pid=${pid} ."  
        else  
          nohup /app/jdk1.8.0_45/bin/java -server -jar $APP_NAME --spring.profiles.active=test > ./console 2>&1 &  
          echo "${APP_NAME} started"
        fi  
      }  
      
      #Stop method  
      stop(){  
        is_exist  
        if [ $? -eq "0" ]; then  
          kill -9 $pid  
          echo "${APP_NAME} stoped"
        else  
          echo "${APP_NAME} is not running"  
        fi    
      }  
      
      #Output operation status  
      status(){  
        is_exist  
        if [ $? -eq "0" ]; then  
          echo "${APP_NAME} is running. Pid is ${pid}"  
        else  
          echo "${APP_NAME} is NOT running."  
        fi  
      }  
      
      #restart  
      restart(){  
        stop  
        start  
      }  
      
      #Select the corresponding method to execute according to the input parameters. If it is not entered, the instructions will be executed  
      case "$1" in  
        "start")  
          start  
          ;;  
        "stop")  
          stop  
          ;;  
        "status")  
          status  
          ;;  
        "restart")  
          restart  
          ;;  
        *)  
          usage  
          ;;  
      esac 
  • Copy configuration

    • Fill in and select the configuration you need

Keywords: Java jenkins

Added by mac25 on Wed, 08 Dec 2021 22:05:16 +0200