Jenkins: parametric Construction: Branch | module | rollback | print log

Keywords: Java Back-end

@

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_...

Customize and build different parameters according to your own needs

Multi branch

Install git parameter plug in

There are more than one plug-in of the same type. You can choose one you are familiar with

Enter system management - plug-in management - optional plug-ins - Direct Search - installation

configuration parameter

Configuration - God webhook - parametric construction process - adding parameters - Git parameters - configure the following parameters:

Name: parameter variable name

Parameter type: select branch / branch or label

Default: set as the main branch or other branches

Source code management - set the parameter name in the previous step of the branch parameter $mybranch - save others if correct

Automatically resolve version based on git address

Select build branch

Sub module

premise

Parent and child projects must be labeled with build, which is required regardless of module

  • Parent project
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
</build>
  • Subproject
<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
</build>

Sub module build

[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-ew3doc2v-1638005232824) (C: \ users \ administrator \ appdata \ roaming \ typora \ typora user images \ image-20211122175236237. PNG)]

Parameter configuration

Extended Choice Parameter - Parameter Type - select Check Boxes

Number of Visible Items: module restrictions

Delimiter: delimiter

[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-ji4bhxji-1638005232825) (C: \ users \ administrator \ appdata \ roaming \ typora \ user images \ image-20211127164143904. PNG)]

Modular shell script

maven is divided into modules according to pom

shell execution process:

  • Get branch parameters, module parameters, workspace path (use default values)
  • Traverse the path and jump to the module directory -- read the current directory pom.xml -- read setting.xml
  • clean-install(complie)
#!/usr/bin/env bash

echo "branch to deploy: " $mybranch
echo "modules to deploy: " $submodule
echo "workspace is: " $WORKSPACE

# /, escape character, get, module array
array=(${submodule//,/ }) 
#Traversal execution
for module in ${array[@]}
do
   echo "Try to compile other module"
   cd  $WORKSPACE/${module} && mvn -B -f pom.xml -s /data/apache-maven-3.8.1/conf/settings.xml -gs /data/apache-maven-3.8.1/conf/settings.xml clean install -Dmaven.test.skip=true
done

Basic usage of mvn

Usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
 -B,-Batch mode runs non interactively (batch) mode (disable output color)
 -D,-definition<arg>Define system properties
 -f,-file <arg>Forced use of standby POM File (or with pom.xml Directory of, pom The file path must follow -f After parameter
 -e,-errors Generate execution error message
 -X,-debug Generate execute debug output
 -l,-log-file <arg>Log files for all build outputs will go (disable output color)
 -q,-quiet Quiet output-Show errors only
 -v,-version display version information
 -h,-help display help information
 -s    --settings <arg> Alternate path to user profile;
 -gs --global-settings <file> Alternate path to global profile;

Sub module operation

Run the SCP command, SSH to the remote server, and run the restart script in two ways,

  • One if, else lists all services

    !/usr/bin/env bash

    echo "modules to deploy: " $submodule

    array=(${submodule//,/ })
    for module in ${array[@]}
    do

     echo "Try to restart other module start"
     scp -P 2021 /data/jenkins/workdir/jobs/base-service/workspace/module/$module-service/target/$module-service.jar devops@xxx.9.134.177:/home/devops/$module/
    
    ssh -p 2021 devops@xxx.9.134.177 "/data/initsh/$module-restart.sh"
    
    echo "Try to restart other module end"
    

    done

  • For traverses all modules, and all projects on the server should be regular. This method is usually used to customize parameters: multiple services traverse the same server, one task for each node, and multiple services for each node. A grouping setting can be made
#!/usr/bin/env bash

echo "modules to deploy: " $submodule

array=(${submodule//,/ }) 
for module in ${array[@]}
do
    if [ "$module" == "module" ]; then
    
        echo "Try to restart other module start"
        scp -P 2021 /data/jenkins/workdir/jobs/base-service/workspace/module/module-service/target/module-service.jar devops@xxx.9.134.177:/home/devops/module/

        ssh -p 2021 devops@xxx.9.134.177 "/data/initsh/module-restart.sh"
    
        echo "Try to restart other module end"

     fi
done

Project rollback

Principle: during construction, you can choose to build or roll back the field, set the value of the Choice field, and set the project build version through Choice

Parameter configuration

God webhook - add parameter - Extended Choice Parameter

  • Extended Choice Parameter: select a parameter
  • Number of Visible Items
  • Delimiter: delimiter

[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-gpxk94ao-1638005232826) (C: \ users \ administrator \ appdata \ roaming \ typora \ user images \ image-20211122170156675. PNG)]

Configure fields in the selection box

Post build Archive

The fingerprint of the record file is used for tracking

File to record fingerprint: jar saved in Jenkins working directory

Project construction selection

Print log

It is very inconvenient to open a task separately from build and run,

Principle: the server remotely commands to print logs to Jenkis,

Command to print log:

tail -$LogRow logPath/log$Date.log

-N

-Date

This is the suffix of the log. Print the specified log file according to the server log name, and view the logs within 15 days at most

View the log of a day. If you enter. 2021-11-22, you will view the log of 2021-11-22. Note that there is a. Symbol in front of it. You can't omit it. You can view the log within 15 days at most

SSH command

tail -$LogRow /data/project/yilucloud-analysis/logs/error$Date.log

Cancel build, run


Reference article:

https://www.cnblogs.com/dadon...

Posted by Aus on Mon, 06 Dec 2021 20:17:57 -0800