Package: ujenkins
Python client for Jenkins which supports both sync and async syntax with same interface.Comparison to other packagesNameSyncAsyncPythonujenkinsYESYES3.6+aiojenkinsNOYES3.5+python-jenkinsYESNO3.4+jenkinsapiYESNO3.4+InstallationLatest release from PyPIpip3installujenkinsOr latest developing versionpip3installgit+https://github.com/pbelskiy/ujenkinsUsageGet Jenkins version using sync client:fromujenkinsimportJenkinsClientdefexample():client=JenkinsClient('http://server','user','password')version=client.system.get_version()print(version)example()With async client (be carefulAsyncJenkinsClientmust be called inside async function):importasynciofromujenkinsimportAsyncJenkinsClientasyncdefexample():client=AsyncJenkinsClient('http://server','user','password')version=awaitclient.system.get_version()print(version)awaitclient.close()asyncio.run(example())ExamplesIn all code examples below client instance is created by:fromujenkinsimportJenkinsClientclient=JenkinsClient('http://server','user','password')Get timestamp of latest buildclient.builds.get_info('job','lastBuild')['timestamp']Get url of started buildBe careful,JenkinsNotFoundErrorcould be raise in case build with same arg already enqueued.item_id=client.builds.start('my_job')whileTrue:time.sleep(5)try:info=client.queue.get_info(item_id)print(info['executable']['url'])breakexcept(KeyError,TypeError):pass# wait for build will be startedGet all jobsBasicallyclient.jobs.get()returns jobs from root (depth = 0), in case you
want receive all the jobs, there are few approaches for it.Set needed depth, experimentally 10 is enough.jobs=client.jobs.get(depth=10)Output:{'folder':{'_class':'com.cloudbees.hudson.plugins.folder.Folder','jobs':[{'_class':'hudson.model.FreeStyleProject','color':'notbuilt','name':'job_in_folder1','url':'http://localhost:8080/job/folder/job/job_in_folder1/'},{'_class':'com.cloudbees.hudson.plugins.folder.Folder','jobs':[{'_class':'hudson.model.FreeStyleProject','color':'notbuilt','name':'sub_job_in_subfolder','url':'http://localhost:8080/job/folder/job/subfolder/job/sub_job_in_subfolder/'}],'name':'subfolder','url':'http://localhost:8080/job/folder/job/subfolder/'}],'name':'folder','url':'http://localhost:8080/job/folder/'},'job':{'_class':'hudson.model.FreeStyleProject','color':'blue','name':'job','url':'http://localhost:8080/job/job/'}}Or just write your code to recursively form it, example is below.defget_all_jobs(url:str='',parent:str='')->Dict[str,dict]:jobs={}forname,propinclient.jobs.get(url).items():jobs[parent+name]=propif'Folder'inprop.get('_class',''):jobs.update(get_all_jobs(prop['url'],parent+name+'/'))returnjobsall_jobs=get_all_jobs()Working with build artifacts# get content of artifact (bytes)content=client.builds.get_artifact('my_job',31,'photo.jpg')withopen('/tmp/photo.jpg','wb')asf:w.write(content)# enumerate artifactsartifacts=client.builds.get_list_artifacts('my_job',31)forartifactinartifacts:# get content and manually save itcontent=client.builds.get_artifact('my_job',31,artifact['path'])# or absolute url could be used for external downloadprint(artifact['url'])# >> 'http://server/job/my_job/31/artifact/photo.jpg'DocumentationRead the DocsTestingPrerequisites:toxThen just run tox, all dependencies and checks will run automaticallytoxContributingAny contributions are welcome!