Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins post build step and action - what is the difference

Tags:

jenkins

Might sound like a very basic question - but I am unable to find any article which explains why Jenkins provides a post build step as well as action.

In Jenkins - I do see that the options are different in post build step vs. action, but

  • what is the order of execution?
  • When should we use which option?
  • What are the best practices?
like image 702
eXamScripts Avatar asked Oct 29 '14 10:10

eXamScripts


People also ask

What is post build action in Jenkins?

This feature allows user to associate shell or a batch scripts that perform some tasks on Jenkins depending on the build log output. If the log text has a match some where in the build log file, the script will execute and the post build log will append to the project build log. Java regular expression are allowed.

What is post build action?

The difference between Build and Post-build steps is based partially on logical separation, partially on workflow configuration. From the logical perspective, when you build/compile a project, that's a "Build" step, whereas when you Archive Artifacts, since that happens after the build, that's a "Post-build" step.


3 Answers

At a glance, here is Jenkins' job workflow (without additional plugins)

  1. [On demand, if needed] Install tools, (such as JDK, Ant, Maven, etc).
  2. [Optional] Perform SCM checkout, (such as SVN or Git).
  3. Perform build step(s), (such as build Ant project, compile code, etc).
  4. [Optional] Perform post-build steps(s), (such as Archive Artifacts, send email, etc).

There are plugins that allow to perform actions right after Tools installation, such as Pre-SCM-step and EnvInject plugins. There are also plugins that add a lot more possible Build Steps and Post-build steps.

The difference between Build and Post-build steps is based partially on logical separation, partially on workflow configuration.

From the logical perspective, when you build/compile a project, that's a "Build" step, whereas when you Archive Artifacts, since that happens after the build, that's a "Post-build" step.

But there is also workflow configuration considerations, and it has everything to do with:

  • "When do the the builds fail", and with
  • "Build Status" (such as Success, Unstable, Failed)

When there are multiple "Build" steps, Jenkins:

  • Executes the first build step
  • Checks for exit code of the first build step
    1. If exit code is 0 (success), Jenkins continues to next build step (if there is one)
    2. If exit code is not 0 (failure), Jenkins marks build as FAILED, and continues to Post-build actions.
  • Jenkins executes Post-build steps (regardless if build was marked FAILED or not)

So, in other words:

  • If Build step succeeds, Jenkins can execute the following Build steps (if any).
  • If Build step fails, Jenkins will not execute the following Build steps.
  • If all Build steps succeeded, Jenkins will mark build SUCCESS.
  • If any Build step failed, Jenkins will mark build FAILED.
  • Post-build steps are executed regardless of Build Status (FAILED or not).

Technically, all post-build steps should be executed at all times, however in practice, if a Post-build step exceptions, the job never completes which can lead to some Post-build steps not being executed.

Also, generally Post-build steps do not change the Build Status, but there are some that are specifically designed to do that (for example, when Archiving Artifacts, you can choose to mark build FAILED if not all artifacts are found, even if after all Build steps, the build was marked SUCCESS)

So, knowing the above, it's your responsibility to design your job, and decide what steps need to be executed one after another, only if previous was successful, and will affect the Build Status (i.e. Build steps), and what steps need to happen at all times regardless of result (i.e Post-build steps).

EDIT:

Since I keep getting comments, here is a screenshot of a brand new clean installation of Jenkins (Windows) ver.1.634 (as was mentioned in comments).

On the screenshot, note the following:

  • New Freestyle project.
  • Scrollbar all the way down, there is nothing more on page.
  • Version 1.634 (as requested).
  • Build section with Add build step drop down.
  • Post-build Actions section with Add post-build action drop down.

So, to re-iterate my previous comment:

There is only one post-build "anything"

whether you want to call it "step" or "action" (Jenkins changed the labeling over the years).

Custom plugins can add a lot of extras, but for a clean install a basic job is just as I have described.

enter image description here

like image 196
Slav Avatar answered Oct 02 '22 09:10

Slav


For an item (job) created as a Maven project build steps are not configurable (only the maven goals are configurable for the build), so two new extra categories are available to configure: pre-build step and post-build step.

In my experience pre/post-build step are used to execute actions that can influence the build result (such as Sonar Analysis), and post-build actions for actions to be performed based on the build's result (such as e-mail notifications).

like image 28
tibtof Avatar answered Oct 02 '22 08:10

tibtof


Clarification. Post build Step can be set to only execute when build has a specific status. i.e. if failed do something (send logs to someone?) if passed or unstable do something. i.e. tag the source so that it can be promoted for release? Post build action is ALWAYS executed. There is where you send emails, archive artifacts, send artifacts to master (if using slaves and have the plugin), etc. I use the Post build Step to tag/label the successfully built source code in my GIT workspace. I use a small script to do that. Now I just wish that Post Build step was available in Freestyle jobs since it is such a great option for only tagging successful builds.

like image 25
jdalew Avatar answered Oct 02 '22 09:10

jdalew