How do I download the artifacts from a Jenkins build to my local machine using Python?
Asked
Active
Viewed 4,302 times
1 Answers
0
I will assume that you want to download via http.
You might want to use GNU wget for example, but if you really want to use Python - check out How do I download a file over HTTP using Python?. Urllib2 provides an easy way to handle http requests.
This is providing that you will not need to perform any additional actions to get to the file (authentication etc.).
-
I am getting below Error: `Traceback (most recent call last): File "C:/Python27/url_doenlaod.py", line 3, in
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 403: Forbidden ` – Chandra Jul 24 '15 at 09:21 -
Go to http://
/user/ – Zloj Jul 24 '15 at 10:28/configure and add credentials 'not bound to a domain'. After that you can use urllib2 password manager: passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, username, password) urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman))) Haven't tested if this will work myself, but it is worth a try. See http://stackoverflow.com/questions/2407126/python-urllib2-basic-auth-problem for a reference -
Actually i have a Folder in jenkins url that i need to Download it through urllib2.But when i am Downloading it is Downloading as a single file but i want the Directory to be Downloaded. how can I achieve that one.. – Chandra Aug 06 '15 at 04:56
-
The above comment is a new requirement – Chandra Aug 06 '15 at 05:22
-
1${JENKINS_URL}/job/${JOB_NAME}/lastSuccessfulBuild/artifact/*zip*/archive.zip will return zipped artifacts for a build. You can then use any of python's libraries that can unzip the archive. Use ${BUILD_NUMBER} i you want something else than 'lastSuccessfulBuild' – Zloj Aug 06 '15 at 06:57
-
what is artifact in the above mentioned.? – Chandra Aug 06 '15 at 07:14
-
I used below one .`username = "username" password = "password" url = 'http://jenkins/view/mine/job/mine_Linux/ws/release/build_35-31683/*zip*/archive.zip' print("calling %s with %s:%s\n" % (url, username, password)) import urllib2, base64 import zipfile request = urllib2.Request(url) base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') request.add_header("Authorization", "Basic %s" % base64string) result = urllib2.urlopen(request) data=result.read() print result,data output=open('mine_driver.zip','wb') output.write(result.read()) output.close()` – Chandra Aug 06 '15 at 07:20
-
It is not downloading.An Empty Compressed file is created. – Chandra Aug 06 '15 at 07:21
-
http://jenkins/view/mine/job/mine_Linux/ws/release/build_35-31683/*zip*/archive.zip Is not the correct link. Why do you enter workspace? I've already provided the correct link: ${JENKINS_URL}/job/${JOB_NAME}/${BUILD_NUMBER}/artifact/*zip*/archive.zip (downloads all build archived artifacts in zip format) http://stackoverflow.com/questions/4028697/how-do-i-download-a-zip-file-in-python-using-urllib2 Has an example that can be copy - pasted and run. Worked for me a minute ago. – Zloj Aug 06 '15 at 09:10
-
ok.Thanks got the solution.... I used workspace the folder i want to download is under workspace/release Folder... – Chandra Aug 07 '15 at 04:00
-
You're welcome :) . May I ask what was the issue with file download from workspace? To be honest, I don't think it's a good idea to use workspace to download files from, as you won't be able to access files from previous builds and workspace may be owrwritten / cleaned up by a new instance of a build. Also, please accept the answer if it did answered your question :) Cheers – Zloj Aug 07 '15 at 07:01