Created
May 20, 2012 22:58
-
-
Save supertom/2759847 to your computer and use it in GitHub Desktop.
download artifacts from jenkins with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cStringIO | |
import json | |
import pycurl | |
import os | |
import sys | |
########## | |
# configuration | |
# list of jobs from jenkins, they are expected to be URL encoded already | |
jobList = [ | |
"job1" | |
,"job2" | |
] | |
build_host = "my_build_host" | |
lastBuildAPIURL = "http://" + build_host + "/job/%s/lastSuccessfulBuild/api/json" | |
lastBuildArtifactLURL = "http://" + build_host + "/job/%s/lastSuccessfulBuild/artifact/%s" | |
localSaveDir = "tmp" | |
artifactExtension=".jar" | |
########## | |
# UDFs | |
def downloadFile(url,filename): | |
print "==> Downloading File: ",filename," URL: ",url | |
fp = open(filename, "wb") | |
curl = pycurl.Curl() | |
curl.setopt(pycurl.URL, url) | |
curl.setopt(pycurl.WRITEDATA, fp) | |
curl.perform() | |
curl.close() | |
fp.close() | |
########### | |
# start | |
print "Fetching files from Jenkins" | |
if not os.path.exists(localSaveDir): | |
print "==> Creating Dir %s" % (localSaveDir) | |
os.makedirs(localSaveDir) | |
for job in jobList: | |
buf = cStringIO.StringIO() | |
jobURL = lastBuildAPIURL % (job) | |
c = pycurl.Curl() | |
c.setopt(c.URL, jobURL) | |
c.setopt(c.WRITEFUNCTION, buf.write) | |
c.perform() | |
jsonstr = buf.getvalue() | |
# print jsonstr | |
jd = json.loads(jsonstr) | |
# print jd | |
buf.close() | |
artifacts = jd['artifacts'] | |
for art in artifacts: | |
if art['fileName'].find(artifactExtension) > -1: | |
artURL = lastBuildArtifactLURL % (job,art['relativePath']) | |
downloadFile(str(artURL),localSaveDir + "/" + str(art['fileName'])) | |
print "Done" | |
buf.close() | |
sys.exit(0) |
So good I made a Python 3 version.
Used this to extrapolate the poorly documented Jenkins api. Thanks!
Hi all,
when I use the function named downloadFile(url,filename), the code raised an error:
curl.perform()
pycurl.error: (18, 'transfer closed with 933650432 bytes remaining to read')
Do you have any ideas?
i can't import cStringIo, can anyone help me?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work, very helpful to me, thanks.