Published an older presentation I provided on
https://www.slideshare.net/Ram0603/devops-a-peek-into-high-performing-organizations-108817405
https://www.slideshare.net/Ram0603/devops-a-peek-into-high-performing-organizations-108817405
#!/bin/bash
yum update -y
pip install -U pip
export PATH=$PATH:/usr/local/bin
<hudson.model.StringParameterDefinition> <name>SPRINT</name> <description></description> <defaultValue>10</defaultValue> </hudson.model.StringParameterDefinition>
#!/bin/bash
# This job uses Jenkins REST API to fetch job config, invokes python to increment SPRINT
# and again uses Jenkins REST API to publish updated job config
# It defines an array of jobs that needs to be updated
# It defines a python script that will update job configurations
# It defines a shell function that will compile python function and pass jenkins config, and execute it
# The updated config is POST'ed back to Jenkins
JOB_NAMES=(first-build-job second-build-job third-build-job)
# Step 1: assign python code to increment Sprint value to a shell variable
_increment_sprint_script=$(cat <<'EOF'
import sys, xml.etree.ElementTree as ET
doc = ET.fromstring(sys.stdin.read())
for node in doc.findall('.//hudson.model.StringParameterDefinition'):
name_el = node.find('./name')
if name_el is not None and name_el.text == 'SPRINT':
default_el = node.find('./defaultValue')
if default_el is None: continue
default_el.text = str(int(default_el.text) + 1)
print ET.tostring(doc)
EOF
)
# Step 2: define a function that calls the interpreter with that code
increment_sprint() { python -c "$_increment_sprint_script" "$@"; }
# Step 3: Iterate over all jobs and update them
for JOB_NAME in "${JOB_NAMES[@]}"
do
echo "Updating $JOB_NAME ..."
updated_config=$(curl "$JENKINS_URL/job/$JOB_NAME/config.xml" | increment_sprint)
curl -v -X POST --data-binary "$updated_config" -H 'Content-Type: application/xml' -u "$USERID:$TOKEN" "$JENKINS_URL/job/$JOB_NAME/config.xml";
done
