This shared library provides reusable components for automating CI/CD processes with Docker and Portainer/Komodo.
βββ src/
β βββ org/dockerutils/
β β βββ DockerUtils.groovy # Docker utilities
β βββ org/portainer/
β β βββ PortainerUtils.groovy # Portainer API utilities
β βββ org/komodo/
β βββ KomodoUtils.groovy # Komodo API utilities
βββ vars/
βββ multiEnvPipeline.groovy # Main multi-environment pipeline
Utility class for working with Docker images.
Builds and pushes a Docker image with multiple tags.
Parameters:
ctx- Reference to CpsScript (Pipeline context)repo- Full repository path without tag (registry/namespace/app)tags- List of tags for the imageuser- Login for registrypass- Password/token for registry
Example:
@Library('docker-lib') _
import org.dockerutils.DockerUtils
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
DockerUtils.buildAndPush(
this,
'registry.example.com/myapp',
['v1.0.0', 'latest'],
'username',
'password'
)
}
}
}
}
}Removes local Docker images after building.
Checks the availability of a Docker registry.
Utility class for working with the Portainer API.
Authenticates with Portainer and obtains a JWT token.
Complete deployment cycle by Git branch name.
Example:
@Library('docker-lib') _
import org.portainer.PortainerUtils
PortainerUtils.deployByBranch(this, [
url: 'http://portainer.example.com',
username: 'admin',
password: 'password',
branchName: 'main'
])Complete deployment cycle by stack name.
Finds a stack by Git branch name.
Finds a stack by name.
Redeploys a specific stack.
Checks the availability of the Portainer API.
Universal method for creating or updating a stack from a compose file.
Utility class for working with the Komodo API.
Authenticates with Komodo and obtains a JWT token.
Complete deployment cycle by Git branch name.
Example:
@Library('docker-lib') _
import org.komodo.KomodoUtils
KomodoUtils.deployByBranch(this, [
url: 'http://komodo.example.com',
username: 'admin',
password: 'password',
branchName: 'main'
])Finds a stack by Git branch name.
Finds a stack by name.
Redeploys a specific stack.
Checks the availability of the Komodo API.
Universal method for creating or updating a stack from a compose file.
Main pipeline for deployment across multiple environments.
- dev - Build image (no deployment)
- staging - Build + automatic deployment to staging
- main - Build + deployment to production
@Library('docker-lib') _
multiEnvPipeline([
registry: 'registry.example.com', // Docker registry URL
credentials: 'docker-reg-creds', // Jenkins credentials ID for Docker
imageRepo: 'myapp/service', // Image name
deployPlatform: 'portainer', // Deployment platform: 'portainer' or 'komodo'
portainerUrl: 'http://portainer.example.com', // Portainer API URL (if deployPlatform = 'portainer')
komodoUrl: 'http://komodo.example.com', // Komodo API URL (if deployPlatform = 'komodo')
deployBranches: ['main', 'staging'], // Branches for deployment
gitCredentials: 'jenkins_gitlab', // Credentials for Git
portainerCreds: 'portainer-creds', // Credentials for Portainer
komodoCreds: 'komodo-creds' // Credentials for Komodo
])// Jenkinsfile
@Library('docker-lib') _
multiEnvPipeline([
imageRepo: 'acme/order-service',
portainerUrl: 'https://portainer.company.com'
])In Jenkins, configure Global Pipeline Libraries:
- Name:
docker-lib - Default version:
master - Retrieval method: Modern SCM β Git
- Project Repository:
https://github.com/your-organization/docker-lib.git
Create the following credentials in Jenkins:
- docker-reg-creds - Username/Password for Docker registry
- jenkins_gitlab - Username/Password for GitLab
- portainer-creds - Username/Password for Portainer
- komodo-creds - Username/Password for Komodo
Ensure you have a Docker agent configured with the label docker-agent-label.
- β Multi-environment - Automatic strategy determination by branch
- β Security - Using Jenkins credentials
- β Optimization - Buildkit, tagging without rebuilding
- β Monitoring - Detailed logging and error handling
- β Integration - Automatic deployment via Portainer/Komodo API
- β Cleanup - Automatic removal of local images
- β Modularity - Separate utility classes for Docker, Portainer and Komodo
- β Reusability - Flexible methods for different deployment scenarios
- β Platform choice - Support for Portainer and Komodo
graph TD
A[Push to Git] --> B{Branch?}
B -->|dev| C[Build Only]
B -->|staging| D[Build + Deploy Staging]
B -->|main| E[Build + Deploy Production]
C --> F[Docker Build]
D --> F
E --> F
F --> G[Docker Push]
G --> H{Deploy?}
H -->|Yes| I[API Auth]
H -->|No| J[End]
I --> K[Find Stack]
K --> L{Platform?}
L -->|Portainer| M[Portainer Deploy]
L -->|Komodo| N[Komodo Deploy]
M --> J
N --> J
- Jenkins 2.190+
- Docker Pipeline Plugin
- Credentials Plugin
- Git Plugin
- Pipeline: Stage View Plugin
docker login failed
Solution: Check the docker-reg-creds credentials
Failed to authenticate with Portainer
Failed to authenticate with Komodo
Solution: Check the URL and credentials for the respective platform
Stack for branch main not found!
Solution: Create a stack in Portainer/Komodo with the appropriate branch name
- Create a feature branch
- Make changes
- Test in the dev environment
- Create a Merge Request
- v1.4.0 - Added Komodo support as an alternative to Portainer
- v1.2.0 - Added PortainerUtils class with full Portainer API
- v1.1.0 - Added cleanup and isRegistryAvailable utilities to DockerUtils
- v1.0.0 - Initial version with basic functionality