Jenkins Shared Library: Docker Build & Push Steps

This Jenkins shared library provides reusable steps for building and pushing Docker images from your pipelines.

📦 Included Steps

Step Description
dockerBuild Builds a Docker image
dockerPush Pushes a Docker image to a registry

Each step is implemented as a Global Variable, making them directly usable in declarative pipelines.


🧩 How to Set Up in Jenkins

1. Push the Library to a Git Repository

Host this repository in a Git server accessible to your Jenkins instance (e.g. GitHub, GitLab, Gitea, etc.).


2. Register the Library in Jenkins

Go to Manage Jenkins → Configure System → Global Pipeline Libraries, then click Add.

Fill in the following:

  • Name: mylib (or any name you'd like to reference in Jenkinsfiles)
  • Default version: main (or specify a tag/branch)
  • Retrieval method: Modern SCM
  • Source Code Management: Git
    • Project Repository: https://your.git.repo/jenkins-shared-library.git
    • Credentials: (only if private)

Click Save.


How to Use in Your Jenkinsfile

In any Jenkins pipeline, include the shared library using @Library and then call the steps:

@Library('mylib') _

pipeline {
    agent any

    environment {
        DOCKER_REGISTRY = 'your.registry.example.com'
        IMAGE_NAME = 'your/image-name'
        IMAGE_TAG = "${DOCKER_REGISTRY}/${IMAGE_NAME}:${params.BUILD_ID}"
    }

    parameters {
        string(name: 'BUILD_ID', defaultValue: 'latest', description: 'Docker image tag')
    }

    stages {
        stage('Build Docker Image') {
            steps {
                dockerBuild(
                    imageName: "${IMAGE_TAG}",
                    dockerfile: "deployments/docker/Dockerfile",
                    context: "deployments/docker"
                )
            }
        }

        stage('Push Docker Image') {
            steps {
                dockerPush(
                    imageName: "${IMAGE_TAG}",
                    registry: "${DOCKER_REGISTRY}",
                    credentialsId: "your-docker-credentials-id"
                )
            }
        }
    }
}
Description
No description provided
Readme 27 KiB
Languages
Groovy 100%