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.
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.
echoprints 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.
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.
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.
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(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.
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: