A collection of Groovy utilities and scripts designed to automate and facilitate various operations within Jenkins environments. This library follows Jenkins-compatible practices and includes comprehensive test coverage. Supports Jenkins automation for modern environments.
- Job Management: Enable/disable, clean, migrate, and templatize Jenkins jobs
- Cloud Integration: Manage Jenkins agents on AWS, Azure, and Kubernetes
- Security: Audit and secure Jenkins instances
- Performance Optimization: Monitor and improve Jenkins performance
- Configuration Management: Backup and restore Jenkins configurations
- Pipeline Generation: Create Jenkins pipelines from reusable templates
The project follows standard Groovy project structure:
jenkins-script-library/
├── src/
│ ├── main/groovy/ # Main source code
│ │ └── com/github/thomasvincent/jenkinsscripts/
│ │ ├── cloud/ # Cloud provider integrations
│ │ ├── config/ # Configuration management
│ │ ├── helm/ # Helm management
│ │ ├── jobs/ # Job management
│ │ ├── nodes/ # Node management
│ │ ├── scripts/ # Command-line script entry points
│ │ ├── security/ # Security utilities
│ │ └── util/ # Common utilities
│ ├── test/groovy/ # Unit tests
│ └── integration-test/groovy/ # Integration tests
├── build.gradle # Gradle build configuration
├── config/ # Configuration files
│ └── codenarc/ # CodeNarc rules for code quality
├── gradle/wrapper/ # Gradle wrapper files
├── gradlew # Gradle wrapper script
├── LICENSE # MIT License
├── README.md # This file
└── SECURITY.md # Security policy
This library can be used in two ways:
-
As a dependency in your Gradle/Maven project:
// In your build.gradle dependencies { implementation 'com.github.thomasvincent:jenkins-script-library:1.0.0' }
-
As individual scripts to run directly in Jenkins:
The scripts in
src/main/groovy/com/github/thomasvincent/jenkinsscripts/scripts/can be executed directly in Jenkins Script Console or through the Jenkins CLI.
All command-line scripts support the --help option to display usage information.
# Clean build history for a job with limit of 50 builds and reset build number
groovy CleanBuildHistory.groovy --limit 50 --reset my-jenkins-job# Disable a specific job
groovy DisableJobs.groovy my-jenkins-job
# Disable all buildable jobs
groovy DisableJobs.groovy --all
# Enable a specific job
groovy EnableJobs.groovy my-jenkins-job# Migrate a specific job
groovy MigrateJobs.groovy --url https://target-jenkins.example.com --user admin --password adminPass --job my-job
# Migrate all jobs matching a pattern
groovy MigrateJobs.groovy --url https://target-jenkins.example.com --user admin --password adminPass --pattern "frontend-.*"
# Use a replacement config file to modify properties during migration
groovy MigrateJobs.groovy --url https://target-jenkins.example.com --user admin --password adminPass --job my-job --replacements-file replacements.json# Create a job from a template
groovy CreateJobFromTemplate.groovy --template-job template-job --target-job new-job --params-file params.json// Generate a Jenkinsfile from template
generatePipeline(
template: 'templates/basic-pipeline.jenkinsfile',
parameters: [project: 'my-app'],
destination: 'Jenkinsfile'
)# Analyze job health
groovy AnalyzeJobHealth.groovy --job my-jenkins-job
# Analyze pipeline performance
groovy AnalyzePipelinePerformance.groovy --job my-pipeline-job --builds 10
# Audit job configurations
groovy AuditJobConfigurations.groovy --pattern "*.xml" --output audit-report.json# List all slave nodes
groovy ListSlaveNodes.groovy --all
# List a specific slave node with detailed information
groovy ListSlaveNodes.groovy my-slave-node
# List cloud-based nodes
groovy ListCloudNodes.groovy --cloud-type aws# Start all offline slave nodes
groovy StartOfflineSlaveNodes.groovy --all
# Start a specific offline slave node
groovy StartOfflineSlaveNodes.groovy my-slave-node# List EC2 instances used as Jenkins agents
groovy ManageEC2Agents.groovy --list
# Provision a new EC2 instance from template
groovy ManageEC2Agents.groovy --provision --template "my-ec2-template"
# Terminate an EC2 instance
groovy ManageEC2Agents.groovy --terminate --instance-id i-1234567890abcdef0# List Azure VMs used as Jenkins agents
groovy ManageAzureVMAgents.groovy --list
# Provision a new Azure VM from template
groovy ManageAzureVMAgents.groovy --provision --template "my-azure-template"
# Clean up an Azure VM
groovy ManageAzureVMAgents.groovy --cleanup --node-name azure-agent-01# List Kubernetes pods used as Jenkins agents
groovy ManageKubernetesAgents.groovy --list
# Provision a new Kubernetes pod from template
groovy ManageKubernetesAgents.groovy --provision --template "my-k8s-template"
# Delete a Kubernetes pod
groovy ManageKubernetesAgents.groovy --delete --pod-name k8s-agent-xyz123# Perform a Jenkins instance health check
groovy JenkinsInstanceHealthCheck.groovy
# Audit Jenkins security settings
groovy AuditJenkinsSecurity.groovy --detailed
# Scan for security vulnerabilities
groovy SecurityVulnerabilityScan.groovy --critical-only# Backup Jenkins configuration
groovy BackupJenkinsConfig.groovy --output jenkins-backup.tar.gz
# Optimize agent resources
groovy OptimizeAgentResources.groovy --reclaim-idle
# Optimize job scheduling
groovy OptimizeJobScheduling.groovy --balance-loadThe library also provides a comprehensive programmatic API for use in your own Groovy scripts:
import com.github.thomasvincent.jenkinsscripts.jobs.JobCleaner
import com.github.thomasvincent.jenkinsscripts.jobs.JobMigrator
import com.github.thomasvincent.jenkinsscripts.jobs.JobTemplate
import jenkins.model.Jenkins
// Clean build history for a job
def jenkins = Jenkins.get()
def cleaner = new JobCleaner(jenkins, "my-job", true, 100)
cleaner.clean()
// Create a job from a template
def template = new JobTemplate(
jenkins: jenkins,
templateJobName: "template-job",
parameters: [
'PROJECT_NAME': 'frontend-app',
'GIT_URL': 'https://github.com/myorg/frontend-app.git'
]
)
template.applyTemplate("new-job")
// Migrate a job between Jenkins instances with JenkinsAccessor
def localAccessor = new LocalJenkinsAccessor(jenkins)
def migrator = new JobMigrator(localAccessor, localAccessor)
migrator.migrateJob("source-job", "target-job")import com.github.thomasvincent.jenkinsscripts.cloud.AWSNodeManager
import com.github.thomasvincent.jenkinsscripts.cloud.AzureNodeManager
import com.github.thomasvincent.jenkinsscripts.cloud.KubernetesNodeManager
import com.github.thomasvincent.jenkinsscripts.cloud.OracleCloudNodeManager
import com.github.thomasvincent.jenkinsscripts.cloud.DigitalOceanNodeManager
import jenkins.model.Jenkins
// Manage EC2 instances
def awsManager = new AWSNodeManager(Jenkins.get())
def templates = awsManager.getEC2Templates()
def nodes = awsManager.getEC2Nodes()
awsManager.provisionNewInstance("my-ec2-template")
// Manage Azure VMs
def azureManager = new AzureNodeManager(Jenkins.get())
def azureNodes = azureManager.getAzureNodesInfo()
azureManager.provisionNewVM("my-azure-template")
// Manage Kubernetes agents
def k8sManager = new KubernetesNodeManager(Jenkins.get())
def k8sNodes = k8sManager.getKubernetesNodes()
k8sManager.provisionNewPod("my-k8s-template")
// Manage Oracle Cloud instances
def ociManager = new OracleCloudNodeManager(Jenkins.get())
def ociNodes = ociManager.getOCIComputeInstances()
ociManager.provisionNewInstance("my-oci-template")
// Manage DigitalOcean droplets
def doManager = new DigitalOceanNodeManager(Jenkins.get())
def doNodes = doManager.getDigitalOceanNodes()
doManager.provisionNewDroplet("my-do-template")./gradlew build# Run unit tests
./gradlew test
# Run integration tests
./gradlew integrationTest
# Run all tests
./gradlew checkNote: There's a known issue with the JFFI native dependency (jffi-1.2.17-native.jar) that may cause test failures. If you encounter this, you can skip tests with:
./gradlew build -PskipTestsThe GitHub Actions tests are currently configured to skip problematic tests.
The project includes a Docker-based testing environment to verify compatibility with different Java versions:
# Run tests in Java 8 (default)
./run-tests.sh java8
# Run tests in Java 11 with longer timeout
./run-tests.sh java11 30m
# Run tests in Java 17 with longer timeout
./run-tests.sh java17 30m
# Run all Java versions
./run-tests.sh all 1h
# Run tests with different scopes
./run-tests.sh java8 30m false minimal # Run minimal test set (core utilities only)
./run-tests.sh java8 30m false default # Run default test set
./run-tests.sh java8 30m false all # Run all tests including integration testsThe Docker testing environment includes:
- Pre-downloading dependencies for faster test execution
- Volume caching for Gradle and Maven dependencies
- Shared base configuration for consistent environment
- Support for different test scopes (minimal, default, all)
- Memory limits and health checks for Docker containers
This library supports the following Java versions:
- Java 8 (LTS)
- Java 11 (LTS)
- Java 17 (LTS)
We maintain backward compatibility with Java 8 to support legacy Jenkins installations. The library is compiled with Java 8 compatibility options by default.
The integration tests use JenkinsRule to create a temporary Jenkins instance for testing, so you don't need a running Jenkins server. These tests verify that the scripts work correctly with real Jenkins APIs and data structures. See the integration test documentation for more details.
This project uses CodeNarc for Groovy code quality checks:
./gradlew codenarcMain codenarcTestThe library supports the following cloud providers:
- AWS EC2: Manage EC2 instances as Jenkins agents
- Azure VMs: Manage Azure virtual machines as Jenkins agents
- Kubernetes: Manage Kubernetes pods as Jenkins agents
- Oracle Cloud: Manage Oracle Cloud Infrastructure (OCI) instances as Jenkins agents
- DigitalOcean: Manage DigitalOcean droplets as Jenkins agents
- Google Cloud Integration: Manage Jenkins agents on Google Cloud Platform
- AWS ECS/Fargate Support: Support for container-based agents in AWS
- Multi-Jenkins Management: Enhanced tools for managing multiple Jenkins instances
- Cross-Cloud Migration: Tools for migrating agents between cloud providers
Contributions are welcome! Please follow our Contributing Guidelines.
For security concerns, please review our Security Policy.
This project is licensed under the MIT License - see the LICENSE file for details.