Jenkins:参数化构建:分支|模块|回滚|打印日志

Jenkins:参数化构建:分支|模块|回滚|打印日志

@

目录
  • 多分支
  • 分模块
    • 前提
    • 分模块build
    • 参数配置
    • 分模块shell脚本
    • mvn 的基本用法
    • 分模块运行

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.webp)]

参数配置

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.webp)]

分模块shell脚本

maven根据pom分模块

shell执行流程:

  • 获取分支参数、模块参数、工作空间路径(使用默认值)
  • 遍历路径跳转模块目录——读取当前目录pom.xml ——读取setting.xml
  • clean——install(complie)
#!/usr/bin/env bashecho "branch to deploy: " $mybranchecho "modules to deploy: " $submoduleecho "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=truedone

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 bashecho "modules to deploy: " $submodulearray=(${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"     fidone

项目回滚

原理:构建时可以选择是构建或者回滚字段,设置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.webp)]

配置选择框内字段

构建后存档

记录文件的指纹用于追踪

需要记录指纹的文件: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

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部