Jenkins:參數化構建:分支|模塊|回滾|打印日志


@

Jenkins筆記

Jenkins筆記之新建任務:https://blog.csdn.net/weixin_42526326/article/details/119865834

Jenkins筆記之配置遠程服務器:https://blog.csdn.net/weixin_42526326/article/details/119866221

Jenkins:參數化構建:多分支|多模塊|回滾|打印日志:https://blog.csdn.net/weixin_42526326/article/details/121580465

根據自己的需求自定義構建不同的參數

多分支

安裝Git Parameter Plug-In

同類型的插件不止一種,選擇自己熟悉的即可

進入系統管理——插件管理——可選插件——直接搜——安裝
在這里插入圖片描述

配置參數

配置——Gods Webhook——參數化構建過程——添加參數——Git 參數——配置下面的參數:

名稱:參數變量名

參數類型:選擇分支/分支或者標簽

默認值:設置為主分支或者其他的分支

在這里插入圖片描述

源碼管理——設定分支參數$mybranch 上一步驟中的參數名——其他在正確的情況下——保存

自動根據git地址解析版本
在這里插入圖片描述

選擇構建分支

在這里插入圖片描述

分模塊

前提

父項目和子項目必須添加build標簽,這個分不分模塊都是需要的

  • 父項目
<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>
  • 子項目
<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>

分模塊build

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-EW3doc2V-1638005232824)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211122175236237.png)]

參數配置

Extended Choice Parameter —— Parameter Type—— 選擇Check Boxes(多選框)

Number of Visible Items : 模塊限制

Delimiter:分隔符

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ji4bHXJI-1638005232825)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211127164143904.png)]

分模塊shell腳本

maven根據pom分模塊

shell執行流程:

  • 獲取分支參數、模塊參數、工作空間路徑(使用默認值)
  • 遍歷路徑跳轉模塊目錄——讀取當前目錄pom.xml ——讀取setting.xml
  • clean——install(complie)
#!/usr/bin/env bash

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

# /, 轉義符,獲取,模塊數組
array=(${submodule//,/ }) 
#遍歷執行
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

mvn 的基本用法

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

選項:
 -B,-批處理模式以非交互方式運行(批處理)模式(禁用輸出顏色)
 -D,-定義<arg>定義系統屬性
 -f,-file <arg>強制使用備用POM文件(或帶有pom.xml的目錄),pom文件路徑必須緊跟在 -f 參數后面
 -e,-errors產生執行錯誤消息
 -X,-debug產生執行調試輸出
 -l,-log-file <arg>所有構建輸出的日志文件會去(禁用輸出顏色)
 -q,-quiet安靜的輸出-僅顯示錯誤
 -v,-version顯示版本信息
 -h,-help顯示幫助信息
 -s	--settings <arg> 用戶配置文件的備用路徑;
 -gs --global-settings <file> 全局配置文件的備用路徑;

分模塊運行

運行SCP命令,SSH連接遠程服務器,運行重啟腳本,采用兩種方式,

  • 一if ,else列出所有的服務

    !/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 遍歷所有的模塊,服務器所有的項目都要規整。通常采用這種方式,自定義參數:同一個服務器多個服務遍歷,每個結點一個任務,每個結點多個服務,可以做一個分組的設置

#!/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

項目回滾

原理:構建時可以選擇是構建或者回滾字段,設置Choice字段的值,通過Choice設置項目build版本

參數配置

Gods Webhook——添加參數——Extended Choice Parameter

  • Extended Choice Parameter: 選擇參數

  • Number of Visible Items:項目可見數

  • Delimiter:分隔符

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Gpxk94Ao-1638005232826)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211122170156675.png)]

配置選擇框內字段
在這里插入圖片描述

構建后存檔

記錄文件的指紋用於追蹤

需要記錄指紋的文件:Jenkins 工作目錄保存的jar
在這里插入圖片描述

項目構建選擇

在這里插入圖片描述

打印日志

需要單獨開一個任務,與build、run都要區分開,非常的不方便,

原理:服務器遠程命令打印日志到Jenkis,

打印日志的命令:

tail -$LogRow logPath/log$Date.log

-N

在這里插入圖片描述

-Date

這個是日志的后綴,根據服務器日志名打印指定的日志文件,最多查看15天以內的日志

查看某一天的日志,如果輸入.2021-11-22,就是查看2021-11-22的日志,注意前面是有一個.符號不能省略,最多查看15天以內的日志
在這里插入圖片描述

SSH命令

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

在這里插入圖片描述

取消build,run

在這里插入圖片描述
在這里插入圖片描述

參考文章:

https://www.cnblogs.com/dadonggg/p/8444366.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM