1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
Low level examples of module code
=================================
Below examples detail out how the api does things internally
Copy an existing job - jenkins.copy_job()
-----------------------------------------
.. code-block:: python
import requests
from pkg_resources import resource_string
from jenkinsapi.jenkins import Jenkins
from jenkinsapi_tests.test_utils.random_strings import random_string
J = Jenkins("http://localhost:8080")
jobName = random_string()
jobName2 = "%s_2" % jobName
url = "http://localhost:8080/createItem?from=%s&name=%s&mode=copy" % (
jobName,
jobName2,
)
xml = resource_string("examples", "addjob.xml")
j = J.create_job(jobname=jobName, xml=xml)
h = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(url, data="dysjsjsjs", headers=h)
print(response.text.encode("UTF-8"))
Create a view - jenkins.views.create()
--------------------------------------
.. code-block:: python
import json
import requests
url = "http://localhost:8080/createView"
str_view_name = "blahblah123"
params = {} # {'name': str_view_name}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {
"name": str_view_name,
"mode": "hudson.model.ListView",
"Submit": "OK",
"json": json.dumps(
{"name": str_view_name, "mode": "hudson.model.ListView"}
),
}
# Try 1
result = requests.post(url, params=params, data=data, headers=headers)
print(result.text.encode("UTF-8"))
Run a parameterized build - jenkins.build_job()
-----------------------------------------------
.. code-block:: python
import json
import requests
toJson = {"parameter": [{"name": "B", "value": "xyz"}]}
url = "http://localhost:8080/job/ddd/build"
# url = 'http://localhost:8000'
headers = {"Content-Type": "application/x-www-form-urlencoded"}
form = {"json": json.dumps(toJson)}
response = requests.post(url, data=form, headers=headers)
print(response.text.encode("UTF-8"))
How JenkinsAPI logs in with authentication
------------------------------------------
.. code-block:: python
from jenkinsapi import jenkins
J = jenkins.Jenkins("http://localhost:8080", username="sal", password="foobar")
J.poll()
print(J.items())
How JenkinsAPI watches post requests
------------------------------------
.. code-block:: python
import http.server as SimpleHTTPServer
import socketserver
import logging
import cgi
PORT = 8081 # <-- change this to be the actual port you want to run on
INTERFACE = "localhost"
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.warning("======= GET STARTED =======")
logging.warning(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
logging.warning("======= POST STARTED =======")
logging.warning(self.headers)
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={
"REQUEST_METHOD": "POST",
"CONTENT_TYPE": self.headers["Content-Type"],
},
)
logging.warning("======= POST VALUES =======")
for item in form.list:
logging.warning(item)
logging.warning("\n")
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Handler = ServerHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
print(
"Serving at: http://%(interface)s:%(port)s"
% dict(interface=INTERFACE or "localhost", port=PORT)
)
httpd.serve_forever()
|