Jenins 參數化構建
- 功能:構建時可根據傳參的方式 傳入腳本中。
- 場景:可根據需要傳入參數的場景使用。
常用參數化構建
1、任務configure --> General --> 選擇參數化構建選項
- 常用:文本參數、字符參數、密碼參數
- Name:變量名
- Default Value:值

任務configure --> General --> Build --> 執行shell、保存。
echo ${branch1} echo "----------------------" echo ${branch2}

2、任務 Build with Parameters --> 修改傳入變量值 --> Build

3、任務 Console Output --> 執行完后查看任務輸出

擴展參數化構建
- 插件:Extended Choice Parameter與Git Parameter
插件安裝
插件管理 --> 搜索並下載插件
Extended Choice Parameter

Git Parameter

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值。根據場景選擇使用。

任務configure --> General --> Build --> 執行shell、保存。
echo ${branch1}

2、任務 Build with Parameters --> 可選擇值-->Build

3、任務 Console Output --> 執行完后查看任務輸出

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:顯示分支名稱

任務configure --> General --> Source Code Management --> Git,保存。
- Repositories:填寫git版本庫地址
- Branches to build:填寫指定變量名稱
注:提前配置好git與jenkins的連接關系

任務configure --> General --> Build --> 執行shell、保存。
echo ${branch}

2、任務 Build with Parameters --> 修改傳入變量值 --> Build

