Hi David,
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") {
git url:
"https://my-git-server.example.com/gitblit/r/MyJavaApp.git", credentialsId:
"XXXX"
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"
}
}
}
}
Terry
On Friday, April 28, 2017 at 2:27:39 AM UTC-6, David Aldrich wrote:
>
> Hi David
>
> Thanks for your reply.
>
> > What you should do is focus on each one of these tasks separately.
>
> I have difficulty with that approach. I agree that it is possible to
> understand individual steps by looking at existing examples or using the
> snippet generator, but I need help in putting the bits together.
>
> For example, after running a shell script I want to send an email
> notification with the result. My attempt to do so sent a build result of
> 'BUILDING'. This must mean that I am calling the email plugin at the wrong
> time, or not giving it the correct result (perhaps I need a try ... finally
> block). But my point is that it is not clear to me how to connect the
> steps. I would like to have a complete example.
>
> Best regards
>
> David
>
--
You received this message because you are subscribed to the Google Groups
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/35143ea9-75f2-44a9-bc78-38e9c6cac478%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.