This guide covers how to create and customize Jenkins pipeline templates using DevOps-OS tooling. The Jenkins pipeline generator helps you create Jenkinsfiles that integrate with your development environment and deployment needs.
- Understanding the Jenkins Pipeline Generator
- Basic Usage
- Pipeline Types
- Customization Options
- Parameterized Pipelines
- Kubernetes Integration
- Credentials Management
- Environment Variables
- Advanced Customization
- Examples
The Jenkins pipeline generator (jenkins-pipeline-generator-improved.py) creates Jenkinsfile scripts that define your CI/CD pipeline using the Jenkins declarative pipeline syntax. The pipelines leverage the DevOps-OS container to provide a consistent environment for building, testing, and deploying your applications.
To generate a basic Jenkins pipeline:
python -m cli.scaffold_jenkins --name "My Pipeline" --type completeThis generates a complete CI/CD pipeline including build, test, and deploy stages.
Output: Jenkinsfile (default)
Change the output file path with --output <path> (e.g. --output pipelines/Jenkinsfile).
The generator supports several types of pipelines:
- Build Pipeline (
--type build): Focuses on building and packaging your application. - Test Pipeline (
--type test): Focuses on running tests and validating your application. - Deploy Pipeline (
--type deploy): Focuses on deploying your application to the target environment. - Complete Pipeline (
--type complete): Combines build, test, and deploy stages. - Parameterized Pipeline (
--type parameterized): Creates a pipeline with parameters for runtime configuration.
--name: The name of the pipeline (e.g., "Backend CI/CD")--languages: Comma-separated list of languages to enable (e.g., "python,java,javascript,go")--output: Output file path for the generated Jenkinsfile
python -m cli.scaffold_jenkins --name "Python API" --languages python --output ./Jenkinsfile
# Output: JenkinsfileParameterized pipelines allow for runtime configuration of the pipeline:
python -m cli.scaffold_jenkins --parameters
# Output: JenkinsfileThis creates a pipeline with parameters that can be configured when the pipeline is run.
Various parameter types are supported:
- Boolean Parameters: For toggle options (e.g., enable/disable a feature).
- Choice Parameters: For selecting from predefined options (e.g., environment).
- String Parameters: For free-form string input (e.g., version tag).
parameters {
booleanParam(name: 'PYTHON_ENABLED', defaultValue: true, description: 'Enable Python tools')
choice(name: 'ENVIRONMENT', choices: ['dev', 'test', 'staging', 'prod'], defaultValue: 'dev')
string(name: 'IMAGE_TAG', defaultValue: 'latest', description: 'Container image tag')
}To include Kubernetes deployment steps in your pipeline:
python -m cli.scaffold_jenkins --kubernetes --k8s-method kubectl
# Output: JenkinsfileFour Kubernetes deployment methods are supported:
- kubectl (
--k8s-method kubectl): Direct deployment using kubectl commands. - kustomize (
--k8s-method kustomize): Deployment using Kustomize for environment-specific configurations. - argocd (
--k8s-method argocd): GitOps deployment using ArgoCD. - flux (
--k8s-method flux): GitOps deployment using Flux CD.
The pipeline generator integrates with Jenkins credentials for secure management of sensitive information:
python -m cli.scaffold_jenkins --type complete --kubernetes
# Output: JenkinsfileThe generated pipelines use Jenkins credentials for:
- Docker Registry: Authenticating with container registries.
- Kubernetes: Accessing Kubernetes clusters.
- Source Control: Authenticating with SCM providers.
- External Services: Authenticating with external services like ArgoCD.
withCredentials([
file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')
]) {
sh 'kubectl apply -f ./k8s/deployment.yaml'
}
withCredentials([
usernamePassword(credentialsId: 'registry-credentials', usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')
]) {
sh 'docker login -u $REGISTRY_USER -p $REGISTRY_PASSWORD'
}All options can be set using environment variables prefixed with DEVOPS_OS_JENKINS_:
export DEVOPS_OS_JENKINS_NAME="API Service"
export DEVOPS_OS_JENKINS_TYPE="complete"
export DEVOPS_OS_JENKINS_LANGUAGES="python,go"
export DEVOPS_OS_JENKINS_KUBERNETES="true"
export DEVOPS_OS_JENKINS_K8S_METHOD="kustomize"
export DEVOPS_OS_JENKINS_PARAMETERS="true"
python -m cli.scaffold_jenkins
# Output: JenkinsfileFor advanced customization, create a custom values JSON file:
{
"build": {
"timeout_minutes": 30,
"artifact_paths": ["dist/**", "build/**"],
"tool_options": {
"maven": {
"goals": ["clean", "package"],
"options": "-DskipTests=false -P production"
},
"gradle": {
"tasks": ["build"],
"options": "--no-daemon"
}
}
},
"test": {
"coverage": true,
"junit_reports": true,
"parallel": 4,
"timeout_minutes": 20
},
"deploy": {
"environments": ["dev", "staging", "prod"],
"approval_required": true,
"rollback_enabled": true
},
"credentials": {
"docker": "docker-registry-credentials",
"kubernetes": "kubeconfig",
"git": "git-credentials",
"argocd": "argocd-credentials"
},
"notifications": {
"slack": {
"channel": "deployments",
"success": true,
"failure": true
},
"email": {
"recipients": ["team@example.com"],
"on_failure_only": true
}
}
}python -m cli.scaffold_jenkins --custom-values advanced-config.json
# Output: JenkinsfileThe generator integrates with the DevOps-OS devcontainer.env.json file to ensure consistency between your development environment and CI/CD pipelines:
python -m cli.scaffold_jenkins --env-file ./devcontainer.env.json
# Output: Jenkinsfilepython -m cli.scaffold_jenkins --name "Python App" --languages python --type complete
# Output: Jenkinsfilepython -m cli.scaffold_jenkins --name "Java Service" --languages java --custom-values maven-config.json
# Output: Jenkinsfilepython -m cli.scaffold_jenkins --name "Microservices" --languages python,javascript,go --kubernetes --k8s-method kustomize
# Output: Jenkinsfilepython -m cli.scaffold_jenkins --name "Deployment" --languages go --kubernetes --k8s-method argocd --parameters
# Output: Jenkinsfilepython -m cli.scaffold_jenkins --name "Container Deploy" --languages go --kubernetes --k8s-method kubectl --registry docker.io
# Output: JenkinsfileThe generated Jenkinsfile defines a declarative pipeline with:
- Agent: Configures the execution environment using the DevOps-OS container.
- Parameters: Defines parameters for customizing pipeline execution (if enabled).
- Environment: Sets environment variables for the pipeline.
- Options: Configures pipeline options like timeout and build history.
- Stages: Defines the stages of the pipeline (build, test, deploy).
- Post: Defines actions to take after pipeline execution.
pipeline {
agent {
docker {
image 'docker.io/yourorg/devops-os:latest'
args '-v /var/run/docker.sock:/var/run/docker.sock -u root'
}
}
parameters {
// Parameters here
}
environment {
// Environment variables here
}
options {
// Pipeline options here
}
stages {
stage('Build') {
steps {
// Build steps here
}
}
stage('Test') {
steps {
// Test steps here
}
}
stage('Deploy') {
steps {
// Deploy steps here
}
}
}
post {
// Post-execution actions here
}
}- Start Simple: Begin with a basic pipeline and add complexity as needed.
- Use Parameters: Use parameters for configurable pipelines.
- Manage Credentials: Use Jenkins credentials for sensitive information.
- Structure Stages: Organize your pipeline into logical stages.
- Handle Failures: Add appropriate post-failure actions.
- Custom Values: Use custom values files for advanced configuration.
- Integration with DevOps-OS: Integrate with your DevOps-OS configuration for consistency.
- Explore the GitHub Actions Generator for creating GitHub Actions workflows.
- Learn about Kubernetes deployments for deploying your applications.
- Implement CI/CD pipelines for technology stacks specific to your project.