{ "cells": [ { "cell_type": "markdown", "id": "7c946340-f845-4253-919f-313970f7ab82", "metadata": {}, "source": [ "# Jenkins Python Automation" ] }, { "cell_type": "code", "id": "85386a95-3f33-454f-9f38-044246e23589", "metadata": {}, "outputs": [], "execution_count": 59, "source": [ "# JenkinsPythonAutomation\n", "import os\n", "import xml.etree.ElementTree as ET\n", "from jenkinsapi.jenkins import Jenkins\n", "\n", "jenkinsFile = 'Jenkinsfile.build'\n", "#with open(jenkinsFile, 'r') as file:\n", "# script = file.read()\n", "script = 'MockJenkinsScript'\n", "url = 'MockJenkinsScript'\n", "apikey = 'MockApiKey'\n", "uname = os.environ.get('USER')\n", "jenkins = Jenkins(url, uname, apikey, timeout=120)\n", "jobUrl = 'MockJenkinsScript'\n", "job = jenkins.get_job_by_url(jobUrl, job_name='Example')\n", "config = job.get_config()\n", "parsedXml = ET.fromstring(config)\n", "\n", "elm = parsedXml.find('definition').find('script')\n", "elm.text = script\n", "\n", "response = job.update_config(ET.tostring(parsedXml, encoding='unicode'), full_response=True)\n", "job.invoke()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 5 }