Jenkins

Jenkins Post-Build Actions

If you are familiar with Jenkins, you mainly work with build actions. This build represents a specific set of tasks executed by the Jenkins server. When you create a new build in Jenkins, you can specify a series of actions to be executed as part of the build. These actions might include:

  • Compiling source code
  • Running tests
  • Packaging the code into a deployable artifact (e.g. a jar file)
  • Deploying the code to a staging or production environment

Build actions are typically defined as part of a Jenkinsfile, a script specifying the build steps for a particular project. You can define build actions using a variety of plugins and scripts, depending on your needs. Standard tools that are used to define build actions include:

  • Shell scripts
  • Docker containers
  • Groovy scripts
  • Ansible playbooks

By defining a set of build actions, you can automate your project’s build, test, and deployment process, making it easier to manage and maintain.

This tutorial will explain what Jenkins post-build actions are and how you can use them.

What are Jenkins Post-Build Actions?

A Jenkins post-build action is a task executed after the build has been completed. Post-build actions are typically used to perform additional processing or reporting on the build results or to trigger other actions based on the build outcome.

Some examples of post-build actions include:

  • Sending an email notification with the build results.
  • Archiving build artifacts for future reference.
  • Triggering a deployment to a staging or production environment.
  • Updating a ticketing system with build information.

Just like regular Jenkins build actions, we can define post-build actions in Jenkinsfiles. We They typically specify post-build actions using a plugin or script, depending on the specific task we wish to perform.

Think of post-build actions as a way to extend the functionality of a Jenkins build beyond the tasks defined as part of the build itself.

Defining Jenkins Post Build Actions

As mentioned, we can define post-build actions in a Jenkinsfile. We can also use the Jenkins web interface to do this.

You can follow the instruction as shown:

  • Navigate to the Jenkins Dashboard and select the project name you wish to add a post-build action.
  • Click on the “Configure” option to configure the job actions.
  • Scroll down to the “Post-build Actions” section.
  • Click the “Add post-build action” button and select the type of action you want to add from the dropdown menu.
  • Configure the settings for the post-build action as needed. The specific settings will depend on the type of action you are adding.
  • Click the “Save” button to save your changes.

For example, the screenshot below shows how to add a simple echo message as a post-build action.

Remember that depending on your Jenkins version and the configuration, this option may need to be added in pipeline job types.

Defining A Jenkins Post-Build Action using Pipeline Syntax

We can also add a post-build action using a Jenkins pipeline, as shown in the syntax below:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        // build steps go here
      }
    }
  }
  post {
    // post-build actions defined here
  }
}

You can define any supported Jenkins action, including sending emails, creating files, sending logs, and more.

Conclusion

In this article, you learned what Jenkins’s post-build actions entail and the two primary methods you can use to define post-build actions in Jenkins.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list