jenkins pipline指令碼
阿新 • • 發佈:2019-02-20
#!groovy
pipeline {
//在任何可用的代理上執行Pipeline
agent any
//引數化變數,目前只支援[booleanParam, choice, credentials, file, text, password, run, string]這幾種引數型別,其他高階引數化型別還需等待社群支援。
parameters {
//git程式碼路徑【引數值對外隱藏】
string(name:'repoUrl', defaultValue: '[email protected]:cts/payment-core.git', description: 'git程式碼路徑')
//repoBranch引數後續替換成git parameter不再依賴手工輸入,JENKINS-46451【git parameters目前還不支援pipeline】
string(name:'repoBranch', defaultValue: 'payment-core20170829', description: 'git分支名稱')
//編譯目錄的相對路徑
string(name:'buildPath', defaultValue: 'paypltfrm-service', description: '編譯目錄')
//jar包的相對路徑
string(name:'jarLocation', defaultValue: 'paypltfrm-service/build/libs/paypltfrm-service-1.0-SNAPSHOT.jar', description: 'jar包的相對路徑')
//伺服器引數採用了組合方式,避免多次選擇
choice(name: 'server',choices:'192.168.20.144,\n192.168.20.111,\n192.168.20.145', description: '測試伺服器列表選擇(IP)')
//重啟指令碼的絕對路徑
string(name:'shellpath', defaultValue: '/Data/apps/payment-core', description: '重啟指令碼的絕對路徑')
}
//常量引數,初始確定後一般不需更改
environment{
//git服務全系統只讀賬號cred_id【引數值對外隱藏】
CRED_ID='2f5b080a-e37b-47f2-920c-9594adff4c52'
}
options {
//保持構建的最大個數
buildDiscarder(logRotator(numToKeepStr: '10'))
}
//pipeline的各個階段場景
stages {
stage('清理工作空間') {
steps {
cleanWs()
}
}
stage('程式碼獲取') {
steps {
//根據param.server分割獲取引數,包括IP,jettyPort,username,password
script {
def split=params.server.split(",")
serverIP=split[0]
}
echo "starting fetchCode from ${params.repoUrl},${params.repoBranch}......"
// Get some code from a GitHub repository
git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch
}
}
stage('編譯') {
steps {
//根據編譯路徑打包
echo "starting build in ${workspace}/${params.buildPath} ......"
// Get some code from a GitHub repository
sh "cd ${params.buildPath} && gradle build"
}
}
stage('推送測試包'){
steps {
echo "starting deploy to ${serverIP}......"
//釋出jar包到指定伺服器
sh "scp ${params.jarLocation} root@${serverIP}:${params.shellpath}"
}
}
stage('重啟應用'){
steps {
echo "restart restart app......"
//釋出jar包到指定伺服器
sh "ssh -f -n root@${serverIP} sh ${params.shellpath}/restart.sh"
}
}
}
}
pipeline {
//在任何可用的代理上執行Pipeline
agent any
//引數化變數,目前只支援[booleanParam, choice, credentials, file, text, password, run, string]這幾種引數型別,其他高階引數化型別還需等待社群支援。
parameters {
//git程式碼路徑【引數值對外隱藏】
string(name:'repoUrl', defaultValue: '[email protected]:cts/payment-core.git', description: 'git程式碼路徑')
//repoBranch引數後續替換成git parameter不再依賴手工輸入,JENKINS-46451【git parameters目前還不支援pipeline】
string(name:'repoBranch', defaultValue: 'payment-core20170829', description: 'git分支名稱')
//編譯目錄的相對路徑
string(name:'buildPath', defaultValue: 'paypltfrm-service', description: '編譯目錄')
//jar包的相對路徑
string(name:'jarLocation', defaultValue: 'paypltfrm-service/build/libs/paypltfrm-service-1.0-SNAPSHOT.jar', description: 'jar包的相對路徑')
//伺服器引數採用了組合方式,避免多次選擇
choice(name: 'server',choices:'192.168.20.144,\n192.168.20.111,\n192.168.20.145', description: '測試伺服器列表選擇(IP)')
//重啟指令碼的絕對路徑
string(name:'shellpath', defaultValue: '/Data/apps/payment-core', description: '重啟指令碼的絕對路徑')
}
//常量引數,初始確定後一般不需更改
environment{
//git服務全系統只讀賬號cred_id【引數值對外隱藏】
CRED_ID='2f5b080a-e37b-47f2-920c-9594adff4c52'
}
options {
//保持構建的最大個數
buildDiscarder(logRotator(numToKeepStr: '10'))
}
//pipeline的各個階段場景
stages {
stage('清理工作空間') {
steps {
cleanWs()
}
}
stage('程式碼獲取') {
steps {
//根據param.server分割獲取引數,包括IP,jettyPort,username,password
script {
def split=params.server.split(",")
serverIP=split[0]
}
echo "starting fetchCode from ${params.repoUrl},${params.repoBranch}......"
// Get some code from a GitHub repository
git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch
}
}
stage('編譯') {
steps {
//根據編譯路徑打包
echo "starting build in ${workspace}/${params.buildPath} ......"
// Get some code from a GitHub repository
sh "cd ${params.buildPath} && gradle build"
}
}
stage('推送測試包'){
steps {
echo "starting deploy to ${serverIP}......"
//釋出jar包到指定伺服器
sh "scp ${params.jarLocation} root@${serverIP}:${params.shellpath}"
}
}
stage('重啟應用'){
steps {
echo "restart restart app......"
//釋出jar包到指定伺服器
sh "ssh -f -n root@${serverIP} sh ${params.shellpath}/restart.sh"
}
}
}
}