Step 3: Setting Up CI/CD pipeline
A parameterized pipeline script in Jenkins allows users to pass parameters to the pipeline at runtime, making the pipeline more dynamic. Here's an example of a simple parameterized pipeline script written in Groovy (Jenkins Pipeline DSL):
pipeline {
agent none
parameters {
string(name: 'BRANCH', defaultValue: 'main', description: 'Branch to build')
booleanParam(name: 'CLEAN', defaultValue: true, description: 'Clean before building')
choice(name: 'ENVIRONMENT', choices: ['development', 'staging', 'production'], description: 'Choose deployment environment')
}
timeout
{
}
stages {
stage('Checkout') {
agent
{
label:
}
steps {
echo "Checking out branch: ${params.BRANCH}"
git branch: "${params.BRANCH}", url: 'https://github.com/example/repo.git'
}
}
stage('Clean') {
when {
expression { return params.CLEAN }
}
steps {
echo 'Cleaning workspace...'
deleteDir()
}
}
stage('Build') {
steps {
echo "Building project for environment: ${params.ENVIRONMENT}"
// Add build commands here
}
}
stage('Deploy') {
steps {
echo "Deploying to ${params.ENVIRONMENT} environment..."
// Add deployment commands here
}
}
}
post {
always {
echo 'Cleaning up after build...'
}
}
}
Explanation:
Parameters:
BRANCH: A string parameter that specifies the branch to build (defaults to main).
CLEAN: A boolean parameter to decide if the workspace should be cleaned before building (defaults to true).
ENVIRONMENT: A choice parameter to select the environment for deployment (development, staging, or production).
Stages:
Checkout: Fetches the code from the specified branch.
Clean: Deletes the workspace if the CLEAN parameter is set to true.
Build: Performs the build process.
Deploy: Deploys to the selected environment.
This pipeline can be triggered with different values for the parameters, allowing flexibility in the process.
Multi Branch Pipeline
Manage Plugins - Install Multibranch Plugin
Dashboard - New item - Choose Multibranch Pipeline , under Branch source add source and select your Git repository type, provide the repository url and credential - Scan Multi Branch pipeline
How to maintain version in Jenkins:
Git tags for explicit versioning.
Commit hash for unique version tracking.
Jenkins build numbers for incremental versions.${BUILD_NUMBER}
Version file (version.txt) for more controlled version management.
How to schedule Backup in Jenkins
Install Plugin - Create folder in /var/lib/jenkins/jenkinsbackup
Change ownership - Schedule full or differential backup
#devopsmadeeasy
#devopsengineer
#devopsproject