Skip to content

Instantly share code, notes, and snippets.

@driscollis
Created June 10, 2025 01:42
Show Gist options
  • Save driscollis/7ba9f83b4f7e88500c513c8ad7b67238 to your computer and use it in GitHub Desktop.
Save driscollis/7ba9f83b4f7e88500c513c8ad7b67238 to your computer and use it in GitHub Desktop.
from lxml import etree, objectify
with open("config_no_build_trigger.xml") as xml_file:
config = xml_file.read()
root = objectify.fromstring(config.encode())
try:
_ = root.properties.__getattr__(
"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"
)
except AttributeError:
# Add org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty
# XML element
root.properties.__setattr__("org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty", "")
try:
trigger_prop = root.properties.__getattr__(
"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"
)
_ = trigger_prop.triggers
except AttributeError:
# Add trigger XML element
trigger_prop = root.properties.__getattr__(
"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"
)
_ = trigger_prop.triggers = objectify.Element("triggers")
try:
trigger_prop = root.properties.__getattr__(
"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"
)
_ = trigger_prop.triggers.__getattr__("hudson.triggers.TimerTrigger")
except AttributeError:
# Add hudson.triggers.TimerTrigger XML element
trigger_prop = root.properties.__getattr__(
"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"
)
_ = trigger_prop.triggers.__setattr__("hudson.triggers.TimerTrigger", "")
try:
trigger = root.properties.__getattr__(
"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"
)
trigger.triggers.__getattr__(
"hudson.triggers.TimerTrigger"
).spec
except AttributeError:
# Add spec XML element
trigger = root.properties.__getattr__(
"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"
)
trigger.triggers.__getattr__(
"hudson.triggers.TimerTrigger"
).spec = objectify.Element("spec")
# Modify the schedule
try:
trigger = root.properties.__getattr__(
"org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty"
)
trigger.triggers.__getattr__(
"hudson.triggers.TimerTrigger"
).spec = "H 18 * * *"
except AttributeError:
print(f"ERROR: Unable to add schedule!")
objectify.deannotate(root)
etree.cleanup_namespaces(root)
config_xml = etree.tostring(root, pretty_print=True, encoding=str)
with open("new_config.xml", "w") as xml_file:
xml_file.write(config_xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment