Skip to content

Step 10 - Shared Library

To keep our pipeline DRY (Don't Repeat Yourself), we extract the Docker build/push logic into a shared library method dockerBuildPush.

Jenkinsfile

Here is the Jenkinsfile for this step. Source code: 30-10-Jenkinsfile-docker-build-push-to-artifactory-condition-shared-library.

@Library('library') _

pipeline {
    agent any
    options {
      disableConcurrentBuilds()
      disableResume()
      buildDiscarder(logRotator(numToKeepStr: '10'))
      timeout(time: 1, unit: 'HOURS')
    }
    tools {
        maven 'maven-3.6.3'
    }
    parameters {
        choice(name: 'dockerRegistry', choices: ['Dockerhub', 'JfrogArtifactory'], description: 'Select Docker Registry')
    }
    environment {
        DATE = new Date().format('yy.M')
        TAG = "${DATE}.${BUILD_NUMBER}"
    }
    stages {
        stage ('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Docker build and push to Docker Registry') {
            steps {
                script {
                    dockerBuildPush("${params.dockerRegistry}")
                }
            }
        }
    }
    post {
      always {
        deleteDir()
      }
    }
}

Detailed Explanation

Shared Library Import

  • @Library('library') _: This single line at the top loads the Shared Library configured in Jenkins Global Configuration as "library". The underscore _ imports everything so you can use global variables (steps) directly.

Custom Step

  • dockerBuildPush(...): This is NOT a standard Jenkins step. It is a custom step defined in the vars/dockerBuildPush.groovy file of your shared library.
  • It encapsulates the logic for logging in, building, and pushing, keeping the main Jenkinsfile incredibly clean and readable.
  • If you need to change how the build works, you update the library once, and all pipelines using it are updated automatically.

Important Tips

Tip

Use Shared Libraries for logic that is repeated across many different pipelines to reduce code duplication and maintenance burden.

Quick Quiz

Quick Quiz

#

What is the main benefit of using Jenkins Shared Libraries?

#

Which annotation is used to import a library in a Jenkinsfile?

#

Where are global variables (custom steps) typically defined in a shared library structure?

📬 DevopsPilot Weekly — Learn DevOps, Cloud & Gen AI the simple way.
👉 Subscribe here