(五)Jenkins参数化构建

一、Jenkins参数化构建

  • 功能:构建时可根据传参方式传入脚本中
  • 场景:可根据需要传入参数的场景使用

二、常用参数化构建

1、任务configure --> General --> 选择参数化构建选项

  • 常用:文本参数、字符参数、密码参数
  • Name:变量名
  • Default Value:值

image
任务configure --> General --> Build --> 执行shell、保存。

echo ${branch1}
echo "----------------------"
echo ${branch2}

image
2、任务 Build with Parameters --> 修改传入变量值 --> Build
image
3、任务 Console Output --> 执行完后查看任务输出
image

三、扩展参数化构建

插件:Extended Choice Parameter与Git Parameter
插件安装
插件管理 --> 搜索并下载插件
Extended Choice Parameter
image
Git Parameter
image

四、Extended Choice Parameter 插件使用

1、任务configure --> General --> 选择参数化构建选项 --> Extenclerl Choice Parameter,保存。

  • Name:变量
  • Descriplion:值
  • Parameter Type:Single Select:单选(单个值、可选多选框)
  • Delimlter:分隔符
  • Value:获取选择的值
  • Property File 选择使用 :传入文件路径(文件内容:xxx(Property Key)=aaa,bbb,ccc)

注:Property 可在服务器内定时变更key值。根据场景选择使用。
image
任务configure --> General --> Build --> 执行shell、保存。
echo ${branch1}
image
2、任务 Build with Parameters --> 可选择值-->Build
image
3、任务 Console Output --> 执行完后查看任务输出
image

五、Git Parameter 插件使用

准备工作

1、修改程序运行用户

# 默认使用jenkins用户运行应用,由于jenkins用户下没有家目录无法保存秘钥对,所以改为以root用户运行应用,来实现git的交互。
vim /etc/sysconfig/jenkins
JENKINS_USER="root"

2、添加jenkins服务器root公钥到git服务器的git用户目录下

# 创建秘钥对
ssh-keygen
# 查看公钥
cat ~/.ssh/id_rsa.pub
# git服务器操作:复制公钥到git服务器的git用户的秘钥文件内
su - git
# 将公钥复制到该文件下
vi .ssh/authorized_keys

Git Parameter 插件使用

1、任务configure --> General --> 选择参数化构建选项 --> Git Parameter,保存。
Name:变量名
Parameter Type:Branch:显示分支名称
image
任务configure --> General --> Source Code Management --> Git,保存。

  • Repositories:填写git版本库地址
  • Branches to build:填写指定变量名称

注:提前配置好git与jenkins的连接关系
image
任务configure --> General --> Build --> 执行shell、保存。

echo ${branch}

image
2、任务 Build with Parameters --> 修改传入变量值 --> Build
image

posted @ 2022-05-24 12:18  比特边界  阅读(1295)  评论(0编辑  收藏  举报