How Do You Use a Jenkinsfile?

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."

How do you use a Jenkinsfile with Declarative pipeline syntax?

Solution
Overview

A Declarative pipeline syntax has four mandatory sections with each of these reserved words: agent, stages, stage and step.  For more information about this, you may want to view this Blazemeter.com site.

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 Plugin."  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.  For a very simple choice go to step 4.b., but if you want something slightly more complex, go to option 4.c.
4.b.  In the upper righthand portion of the "Script" click on the drop down menu that says "try sample pipeline...", and click "Hello World."  Skip step 4.c. and go to step 5.
4.c.  Enter this text (which is declarative syntax according to this external site):

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

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.

Leave a comment

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