How Do You Use a Jenkins Pipeline with a Scripted Syntax?

Updated and re-tested on 9/24/19
Problem scenario
You read about what Jenkinsfiles are.  Coveros.com defines them as 'Jenkinsfiles, using a domain specific language based on the Groovy programming language, are persistent files that model delivery pipelines “as code", containing the complete set of encoded steps (steps, nodes, and stages) necessary to define the entire application life-cycle.'

You want to use a Scripted Pipeline in Jenkins (via a Jenkinsfile).  What do you do to use the Scripted DSL syntax?

Solution
Prerequisites

i.  You must have Jenkins installed.  See the posting for your type of Linux below for assistance installing Jenkins:

CentOS/RHEL/Fedora
Debian/Ubuntu

Pipeline jobs require more memory than typical Jenkins jobs.  If you need to resize your server (i.e., give it more RAM), click here if you have a server in AWS, and click here if you have a server in GCP.

If you are not using a public cloud, you may want to add physical RAM.  You may want to create virtual memory to help with performance.  To do that, see this posting.

ii.  Install the Pipeline plugin.  Log into the web UI of Jenkins as an administrator.  Go to Manage Jenkins -> Manage Plugins -> Available.  Search for "Build Pipeline."  Check the box and click "Install without restart".  Next, click the option for "Restart Jenkins..." at the bottom of the screen.

Procedures
1.  Log into the web UI for Jenkins as an administrator.
2.a.  If you see "New Item" do 2.b, otherwise do 2.c.
2.b.  Click on "New Item".
2.c.  Click on "Dashboard" if you do not see "New Item."  Then click "New Item".
3.  Enter a name in the field and the top, then click on "Pipeline".  Then click "OK".
4.a.  Scroll down to the "Pipeline" section.  
4.b.  Enter this text (which is mostly taken from this external site):

node {
    stage('Example') {
        if (env.BRANCH_NAME == 'master') {
            echo 'I only execute on the master branch'
        } else {
            echo 'I execute elsewhere'
        }
    }
} //Taken from https://jenkins.io/doc/book/pipeline/syntax/#flow-control
node {
    stage('Example1') {
        if (env.BRANCH_NAME == 'slave') {
            echo 'I only execute on the slave branch'
        } else {
            echo 'I do not execute on the slave branch.'
        }
    }
}

5.  (You can uncheck the "Use Groovy Sandbox" or leave it checked.*)  Click "Save".

6.a.  You can test it by clicking on "Build Now."

6.b.  You are done.  The Jenkinsfile should be checked into a source control repository.

6.c.  Here is optional reading if you want to learn more.

*  This is information on using the Groovy sandbox:

"Jenkins limits the execution of any Groovy script by providing a sandbox.  The option "Use Groovy Sandbox", shown below, is available in the Pipeline tab, and it allows the scripts to be run by any user without requiring administrator privileges.  In this case, the script is run only by using the internal accessible APIs (that allow you to develop your script by using Groovy).
...
When unchecked, if the script has operations that require approval, an administrator will have to provide them.  This method is known as "Script approval".  By default, all Jenkins pipelines run in a Groovy sandbox.  If the option is checked and unauthorized operations are used, the script will fail when run.  Both the whitelist and the blacklist of functions can be checked at Script Security's built-in list. Please refer to In-process Script Approval for more information on this topic."  The above quoted paragraphs were taken from Dzone.com.

Leave a comment

Your email address will not be published. Required fields are marked *