Imagine you run a Jenkins pipeline multiple times. Why should the steps be idempotent?
Think about what happens if a step changes something every time it runs.
Idempotent steps mean running them multiple times won't change the outcome or cause errors. This is important for reliability and repeatability.
What will be the output of this Jenkins pipeline shell step if run twice?
sh 'mkdir -p /tmp/myfolder && echo Created'
Check what the -p option does for mkdir.
The -p option makes mkdir create the directory only if it doesn't exist, so no error occurs on the second run.
Which Jenkins pipeline snippet ensures the environment variable MY_VAR is set only if not already defined, making the step idempotent?
Use Groovy's Elvis operator to check if MY_VAR is already set.
Option C uses env.MY_VAR ?: "default" which sets MY_VAR only if it is not already defined, making it idempotent.
A Jenkins pipeline step runs a script that appends a line to a file every time it runs. What problem does this cause?
Think about what happens if you add the same line repeatedly.
Appending without checking causes the file to grow with repeated lines, which can break logic depending on that file.
Which Jenkins pipeline step best ensures an idempotent deployment of a Docker container named myapp?
Consider how to remove any existing container before running a new one without failing.
Option A force removes any existing container named myapp (ignoring errors) before running a new one, ensuring idempotency.