Request for simple pipeline example to do checkout, run shell script, email result

78 views
Skip to first unread message

David Aldrich

unread,
Apr 27, 2017, 6:06:55 AM4/27/17
to jenkins...@googlegroups.com

Hi

 

Please can anyone point me to, or provide, a simple and complete pipeline script example to do a checkout, run a shell script and email the result?

 

I am using svn, but a git example would be ok.

 

Best regards

 

David

 

David Karr

unread,
Apr 27, 2017, 1:08:20 PM4/27/17
to Jenkins Users
On Thu, Apr 27, 2017 at 3:05 AM, David Aldrich
<David....@emea.nec.com> wrote:
> Hi
>
>
>
> Please can anyone point me to, or provide, a simple and complete pipeline
> script example to do a checkout, run a shell script and email the result?
>
>
>
> I am using svn, but a git example would be ok.

What you should do is focus on each one of these tasks separately.
Each one of these will use pipeline steps and Groovy code. Use the
"Pipeline Syntax" link within the "Pipeline" section of the job
definition to see examples of what pipeline steps are available to
you. On this page, you can generate example code.

Doing a git checkout requires the "git" function. Running a shell
script uses "sh", and sending email could use the "email-ext"
function, although there may be others.

David Aldrich

unread,
Apr 28, 2017, 4:27:39 AM4/28/17
to jenkins...@googlegroups.com
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 jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-
> users/CAA5t8VrFJ2GA3p1hoztayT1t9tH8tagvSefj2eTG5BkqZ_1%2BVQ%40mail
> .gmail.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> Click
> https://www.mailcontrol.com/sr/0yt4T6!T5bTGX2PQPOmvUmuFhuAgpUQ2CB
> KREum+VQGxhSt75yjqKxzlfBkZKVUJ5srBIUSVvPiDL!bKPtQR4w== to report
> this email as spam.

Terry Lacy

unread,
Apr 28, 2017, 12:46:19 PM4/28/17
to Jenkins Users, David....@emea.nec.com
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
Reply all
Reply to author
Forward
0 new messages