After cloning VMs, the next step is to deploy the source code. Follow these steps to automate the deployment using Jenkins:
Here’s the detailed directory structure for your Jenkins pipeline automation setup:
.
└── elitical/
├── source/
│ ├── script/
│ │ ├── source_elitical_dev_deploy_tmpl # Deployment pipeline template
│ │ └── source_elitical_dev_db_tmpl # Database reset pipeline template
│ └── elitical-dev-vms.csv # Input CSV file with VM and Jenkins details
├── target/
├── elitical_dev_generate.py # Python script for generating Jenkins pipeline scripts
└── push_to_github.sh # Shell script for pushing generated scripts to GitHub
You need a CSV file containing the following details for each developer:
name,emp_id,work,branch,host_ip,devbox_ip,mac_address,node_name,jenkins_user,jenkins_password,private_key,email,sonar_project_name,sonar_project_key_id,sonar_login,sonar_username,sonar_password
Two pipeline templates are required:
source_elitical_dev_deploy_tmpl
: Template for deployment pipeline.source_elitical_dev_db_tmpl
: Template for database reset pipeline.The following Python script generates individual Jenkins pipeline scripts based on the .csv
file and pipeline templates:
import csv
import os
# Get the directory of the script
current_dir = os.path.dirname(os.path.abspath(__file__))
source_jenkins_files = [
os.path.join(current_dir, "source/scripts/source_elitical_dev_deploy_tmpl"),
os.path.join(current_dir, "source/scripts/source_elitical_dev_db_tmpl")
]
source_details = os.path.join(current_dir, "source/elitical-dev-vms.csv")
target_dir = "./target/"
with open(source_details) as details:
details_reader = csv.DictReader(details, delimiter=',')
for row in details_reader:
name = row['name']
emp_id = row['emp_id']
work = row['work']
branch = row['branch']
host_ip = row['host_ip']
devbox_ip = row['devbox_ip']
mac_address = row['mac_address']
node_name = row['node_name']
jenkins_user = row['jenkins_user']
jenkins_password = row['jenkins_password']
private_key = row['private_key']
email = row['email']
sonar_project_name = row['sonar_project_name']
sonar_project_key_id = row['sonar_project_key_id']
sonar_login = row['sonar_login']
sonar_username = row['sonar_username']
sonar_password = row['sonar_password']
main_branch = "main"
webapp_branch_name = branch
webserver_branch_name = branch
key_file_name = private_key.split('/')[-1]
for source_jenkins_file in source_jenkins_files:
if 'db' in source_jenkins_file:
suffix = "db"
else:
suffix = "deploy"
target_jenkins_file = target_dir + branch + "-" + suffix
target_jenkins_content = ""
with open(source_jenkins_file, 'r') as source_file:
source_content = source_file.read()
target_content = source_content.replace("emailtobereplaced", email)
target_content = target_content.replace("iptobereplaced", devbox_ip)
if work == "WEBSERVER":
target_content = target_content.replace("serverbranchtobereplaced", webserver_branch_name)
target_content = target_content.replace("appbranchtobereplaced", "master")
elif work == "WEBAPP":
target_content = target_content.replace("appbranchtobereplaced", webapp_branch_name)
target_content = target_content.replace("serverbranchtobereplaced", main_branch)
elif work == "WEBSERVER/WEBAPP":
target_content = target_content.replace("serverbranchtobereplaced", webserver_branch_name)
target_content = target_content.replace("appbranchtobereplaced", webapp_branch_name)
target_content = target_content.replace("branchtobereplaced", branch)
target_content = target_content.replace("agenttobereplaced", node_name)
target_content = target_content.replace("sonarlogintobereplaced", sonar_login)
with open(target_jenkins_file, 'w') as target_file:
target_file.write(target_content)
Execute the Python script elitical_dev_generate.py from the scripts directory.
This script will read the CSV file, generate the individual pipeline scripts based on the templates, and save them in the target directory. bash
python3 elitical_dev_generate.py
The following shell script pushes the generated pipeline scripts to the appropriate GitHub branches:
#!/bin/bash
# Define the path to the downloads folder
downloads_folder="/path/to/target"
# Define an array of branch names
branches=(
"dev-pavan"
"dev-sravani"
"dev-dharmajalla"
"dev-noosagontu"
"dev-ramesh"
"dev-tulasiram"
"dev-venkatesh"
)
# Loop through each branch
for branch in "${branches[@]}"; do
# Checkout the branch
git checkout "$branch"
# Check if the branch checkout was successful
if [ $? -ne 0 ]; then
echo "Failed to checkout branch: $branch"
continue
fi
git pull origin "$branch"
# Remove old files
rm "elitical-deploy"
rm "elitical-db-reset"
# Copy the respective files to the repository
cp "$downloads_folder/$branch-deploy" .
cp "$downloads_folder/$branch-db" .
# Rename the files
mv "$branch-deploy" "elitical-deploy"
mv "$branch-db" "elitical-db-reset"
# Add the files to the staging area
git add .
# Commit the changes
git commit -m "Added db and deploy files"
# Push the changes to the remote repository
git push origin "$branch"
done
# Checkout the main branch again at the end
git checkout main
After pushing the pipeline scripts, configure Jenkins as follows:
project_name-deploy
, db-deploy
) and execute them.