推薦使用Jenkinsfile代替將groovy腳本直接寫在jenkins job里。
文章來自:http://www.ciandcd.com
文中的代碼來自可以從github下載: https://github.com/ciandcd
推薦將pipeline的groovy腳本放到Jenkinsfile且保存到跟源代碼相同的目錄下。通過這種方法實現了groovy腳本與源代碼都被版本控制,更好地支持構建的可重現性。
本文用到的測試repo:https://github.com/ciandcd/simple-maven-project-with-tests.git
測試repo下的Jenkinsfile:https://github.com/ciandcd/simple-maven-project-with-tests/blob/master/Jenkinsfile
1. Jenkinsfile的內容如下:
node (){
stage 'Build and Test'
env.PATH = "${tool 'M3'}/bin:${env.PATH}"
//checkout scm
git url:"https://github.com/ciandcd/simple-maven-project-with-tests.git"
sh 'mvn clean package'
}
2. 配置jenkins pipleline job來使用Jenkinsfile

3. 運行pipleline job將會看到如下view

