Living in the pipeline cannot be accomplished without steps and commands. The Jenkins workflow-basic-steps plugin provides several commonly used steps/commands for writing pipelines. In this part of the completest Jenkins tutorial, we go deep into the commands provided by this plugin. You should note that many Jenkins plugins may add some steps and commands. Those steps are available in the following Jenkins stack.

https://github.com/ssbostan/jenkins-stack-kubernetes

https://github.com/ssbostan/jenkins-stack-docker

Follow our social media:

https://www.linkedin.com/in/ssbostan

https://www.linkedin.com/company/kubedemy

https://www.youtube.com/@kubedemy

https://telegram.me/kubedemy

Jenkins Basic Pipeline Steps:

The following commands can be used in the steps block, all sub-blocks in post block, all blocks used to implement pipeline steps, and implement Jenkins libraries.

echo prints a message.

The following code shows the echo and how-to call methods.

dir is used to change the current working directory. Any codes that exist inside the dir block are executed within that directory.

pwd is used to return the current working directory. It accepts a boolean argument called tmp that produces a temporary directory of the job workspace.

isUnix returns True if the running agent is a Unix-like machine (Linux, macOS, etc.) and False if the running agent is Windows or something else.

fileExists returns True if the given file exists.

readFile reads the file and returns its content. It also accepts an optional argument called encoding to encode the file content with the specified encoding algorithm. Encoding may use for reading binary files and already encoded files.

writeFile writes the content to the given file. If the optional argument, encoding, is specified, it decodes the content before writing to the file.

deleteDir deletes the current directory, usually the job workspace. It deletes all files and subdirs in the active path. If you want to delete the specific directory, you should first dir() to it and use deleteDir() inside its block. The deleteDir command is usually used in the post stages to clean up the workspace for the next build.

catchError catches the error and continues the pipeline. The pipeline’s default behaviour is to fail the whole pipeline if any error occurs during each step. In most cases, you must continue the pipeline to run some commands, send notifications, etc.

catchError(
    stageResult: "SUCCESS",
    buildResult: "FAILURE",
    catchInterruptions: true,
    message: "Error"
) {
    // your pipeline codes.
}

catchInterruptions is used to catch pipeline interruptions, like aborting the pipeline manually. By default, if you abort the pipeline while the pipeline is in the catchError block, the pipeline is not aborted, and other steps will be run. To prevent this behaviour and stop the pipeline as soon as you made an interrupt, set this parameter to false.

warnError is like catchError, but if any error occurs when running the pipeline inside its block, it sets the stageResult and the buildResult to UNSTABLE status.

warnError(
    catchInterruptions: true,
    message: "Unstable build message"
) {
    // your pipeline codes.
}

mail is used to send an email inside the pipeline.

mail(
    subject: "Subject",
    body: "Body",
    from: "jenkins@localhost",
    to: "[email protected]"
)

Please note that you should setup an SMTP server on the Jenkins machine or a port forwarder to send requests to an external SMTP server.

unstable sets the stage result to unstable.

unstable(message: "Your stage is unstable!")

timeout is used to set an execution time limit for commands executed inside its block. For example, if you want to run a command to check something, “service availability, healthcheck, etc.” it should respond in less than 30s. In such cases, you have to use the timeout command. The default unit is MINUTES and supports NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, and DAYS.

In addition to the traditional timeout scheme, it supports the activity-based timeout. The activity-based scheme can be enabled by setting the activity parameter to true.

timeout(
    time: 15,
    unit: "SECONDS",
    activity: true
) {
    // your pipeline codes.
}

If the timeout is reached, the pipeline will be terminated with ABORTED status. You can use catchError or warnError to catch it.

sleep pauses the pipeline for the specified amount of time. The default unit is SECONDS, which can be changed like the timeout command.

sleep(10)
sleep(time: 1, unit: "MINUTES")

retry retries its body (up to N times) if any exception happens during its body execution. If the final attempt returns an exception, it will exit with a failure status. For example, you want to check your database before running application tests. In such a situation, you need something like the following pipeline.

Final words:

We have many things to learn about the Jenkins and Jenkins pipelines because it has many plugins which add various commands, functions, classes, etc. In the following articles, I will explain more about Jenkins, pipeline directives and commands, and plugins. You can find previous ones in the following GitHub repository. Good luck.

You can find all tutorial materials in the following GitHub repository:

https://github.com/ssbostan/jenkins-tutorial

If you like this series of articles, please share them and write your thoughts as comments here. Your feedback encourages me to complete this massively planned program.

Follow my LinkedIn https://www.linkedin.com/in/ssbostan

Follow Kubedemy LinkedIn https://www.linkedin.com/company/kubedemy

Follow Kubedemy Telegram https://telegram.me/kubedemy

Leave a Reply

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