jenkins2 Jenkinsfile和load

更復雜的pipeline實例,使用了Jenkinsfile和load。node

 

文章來自: http://www.ciandcd.com
文中的代碼來自能夠從github下載:  https://github.com/ciandcd
 
實例代碼來自:https://github.com/jenkinsci/workflow-aggregator-plugin/tree/master/demo/repo
 
1. groovy腳本
https://github.com/jenkinsci/workflow-aggregator-plugin/blob/master/demo/repo/servers.groovy

def deploy(id) {
unstash 'war'
sh "cp x.war /tmp/webapps/${id}.war"
}git

def undeploy(id) {
sh "rm /tmp/webapps/${id}.war"
}github

def runWithServer(body) {
def id = UUID.randomUUID().toString()
deploy id
try {
body.call id
} finally {
undeploy id
}
}web

thisdocker

 

2. Jenkinsfileapp

https://github.com/jenkinsci/workflow-aggregator-plugin/blob/master/demo/repo/Jenkinsfiledom

jettyUrl = 'http://localhost:8081/'webapp

def serversui

stage 'Dev'
node {
checkout scm
servers = load 'servers.groovy'
mvn '-o clean package'
dir('target') {stash name: 'war', includes: 'x.war'}
}this

stage 'QA'
parallel(longerTests: {
runTests(servers, 30)
}, quickerTests: {
runTests(servers, 20)
})

stage name: 'Staging', concurrency: 1
node {
servers.deploy 'staging'
}

input message: "Does ${jettyUrl}staging/ look good?"
try {
checkpoint('Before production')
} catch (NoSuchMethodError _) {
echo 'Checkpoint feature available in CloudBees Jenkins Enterprise.'
}

stage name: 'Production', concurrency: 1
node {
sh "wget -O - -S ${jettyUrl}staging/"
echo 'Production server looks to be alive'
servers.deploy 'production'
echo "Deployed to ${jettyUrl}production/"
}

def mvn(args) {
sh "${tool 'Maven 3.x'}/bin/mvn ${args}"
}

def runTests(servers, duration) {
node {
checkout scm
servers.runWithServer {id ->
mvn "-o -f sometests test -Durl=${jettyUrl}${id}/ -Dduration=${duration}"
}
}
}

 

3. 若是有興趣能夠下載pipeline demo docker image

https://github.com/jenkinsci/workflow-aggregator-plugin/tree/master/demo

相關文章
相關標籤/搜索