# Basic Jenkinsfile Cookbook

This post shows how to customize standard Pipeline “steps” in Jenkinsfile besides their common usage.

List of basic Jenkinsfile steps in this post:

• checkout/git
• emailext
• findFiles
• input
• junit
• parameters/properties
• podTemplates
• sendSlack
• stash/unstash
• withCredentials

### checkout/git step

scm is the global variable for the current commit AND branch AND repository of Jenkinsfile. checkout scm means checking out all other files with same version as the Jenkinsfile associated with running pipeline. To check out another repository, you need to specify the paremeters to checkout step.

Reference:

### emailext step

To send email as HTML page, set content type to HTML and use content as \${FILE,path="email.html"}. In Jenkinsfile, the code should look like this:

Note that it’s single-quoted strings, not double-quoted, being used for body and presendScript parameters in the example code above.

Reference:

Doing in Bash:

Reference:

### input step

Simple input step can be used to ask for approval to proceed. For asking input from a list of multiple choices, you can use the advanced version of input.

Reference:

### junit step

JUnit tests + PMD, FindBugs, CheckStyle. In Blue Ocean interface, these will be displayed in a separate tab.

### parameters/properties step

parameters step adds certain job parameters for the overall pipeline job.

In Scripted pipeline, its equivalent counterpart is properties step, as shown below.

In the Jenkins UI, this will be converted to configurations when you click on “View Configuration” for that job, as shown in screenshot below. Note that the configurations in this page is read-only when using Jenkinsfile. Any modifications made to the page will be ignored, leaving configurations set in Jenkinsfile final (“Infrastructure as Code”).

Reference:

### podTemplate step

This step is used to specify a new pod template for running jobs on Kubernetes cluster.

Reference:

### sendSlack step

Standard Jenkinsfile for testing Slack

### stash/unstash steps

stash step can be used to save a set of files, to be unstashed later in the same build, generally for using in another workspace. unstash will restore the files into the same relative locations as when they are stashed. If you want to change the base directory of the stashed files, you should wrap the stash steps in dir step.

We should use stash/unstash to avoid the common anti-pattern of copying files into some special, globally visible directory such as Jenkins home or one of its subdirectories. Using such anti-pattern will make it hard to support many jobs for many users since, eventually, there will be some name clash and, subsequently, some convoluted naming of those files to avoid such name clashes.

Note that stash and unstash steps are designed for use with small files. If the size is above 5 MB, we should consider an alternative such as Nexus/Artifactory for jar files, blob stores for images.

Example usage of stash and unstash:

Example output:

### withCredentials step

There are different variations of withCredentials step. The most common ones are:

For secret file, the file will be passed into some secret location and that secret location will be bound to some variable. If you want the secret files in specific locations, the workaround is to create symlinks to those secret files.

For “private key with passphrase” Credential type, sshagent is only usage that I know (credential ID is jenkins_ssh_key in this example):

Reference: