Here's a real-world example from my server. It checks out from git, runs a maven build and deploys to a platform specified by a jenkins job parameter. Details obscured to protect the incompetent :). Hope this helps get you started. I found getting started with pipeline a little confusing too. Once you figure out the basic structure of a pipeline script, the "Pipeline Syntax" link on the job configuration page is really helpful for generating individual steps.
// Platform specific parameters:
def platforms = [
"dev" : [
"node" : "master",
"home" : "/usr/share/tomcat7/MyJavaApp",
"os" : "linux"
],
"test" : [
"node" : "JENKINS_NODE_1",
"home" : "F:\\MyJavaApp",
"os" : "windows"
],
"prod" : [
"node" : "JENKINS_NODE_2",
"home" : "/usr/local/jenkins_slave/MyJavaApp",
"os" : "linux"
]
]
// Do this on the master node
node('master') {
// Stage is just for display purposes.
// Each stage is timed and displayed in a box on the Jenkins job's status page
stage("Clear Workspace") {
// This is a pipeline "step". This one clears the workspace directory.
deleteDir()
}
stage("Check Out") {
sh 'git checkout MyJavaApp-${DEPLOY_VERSION}'
}
stage("Configure") {
// "dir" says to perform the steps inside in the specified directory. This is relative to the workspace directory.
dir("src/main/resources") {
writeFile file: "my.java.app.property.file.properties", text: "my.java.app.property.file=my_java_app_${DEPLOY_TO}"
}
}
stage("Build") {
// Execute a shell command on linux (master is a linux server)
sh "${tool 'Apache Maven 3.0.4'}/bin/mvn clean package"
}
stage("Save Build Product") {
dir("target") {
// Save a file to be moved to another Jenkins node
stash name: "MyJavaApp_JAR", includes: "MyJavaApp-${DEPLOY_VERSION}-jar-with-dependencies.jar"
}
}
}
stage("Copy to Destination") {
// Stash the configuration
node("master") {
dir("/usr/share/tomcat7/.my_java_app") {
stash name: "config file", includes: "my_java_app_${DEPLOY_TO}.properties"
}
}
// Perform the following steps on the "DEPLOY_TO" node. DEPLOY_TO is specified as a parameter in a Jenkins parameterized job.
node(platforms[DEPLOY_TO].node) {
dir(platforms[DEPLOY_TO].home + "/.my_java_app") {
if (DEPLOY_TO != "dev") {
// Don't mess with dev's config
// copies a file stashed above to the deployment node
unstash "config file"
}
}
dir(platforms[DEPLOY_TO].home) {
unstash "MyJavaApp_JAR"
if (platforms[DEPLOY_TO].os == "windows") {
// Windows platform - use windows batch files to run CLI commands:
bat "del MyJavaApp.jar"
bat "rename MyJavaApp-${DEPLOY_VERSION}-jar-with-dependencies.jar MyJavaApp.jar"
} else {
sh "rm -f MyJavaApp.jar"
sh "mv MyJavaApp-${DEPLOY_VERSION}-jar-with-dependencies.jar MyJavaApp.jar"
}
}
}
}