Forum Discussion
Will it work with partial file name? How can I handle it?
There will be different versions in my file name in every run...
Hi,
> Will it work with partial file name?
Could you please provide more details or some example of what do you mean ?
- Kate8 years agoContributor
I mean that my script is expected to go to my jenkins release builder page and grab latest build (download .exe file)
Yesterday we had, for ex, build 1.2, and my file name was kind of 'myapp-1.2-install.exe'
Today it is 'myapp-1.3-install.exe'
Tomorrow it may be 'myapp-1.3.1-install.exe'
And so on...
So I need a flexible solution that will let me download a file by partial link, containing, let's say, 'myapp' and 'install.exe'
I can probably use regular expressions for that, but I do not have enough practical knowledge on that... So it will take toooo long for me.
- HKosova8 years agoSmartBear Alumni (Retired)
Jenkins has the Remote access API that lets you query all kinds of data as XML or JSON. That page mentions Python wrappers but I didn't try them.
For example, if you want the artifacts of the last successful build, you can use:
http://<jenkins-host>/job/<job-name>/lastSuccessfulBuild/api/json?tree=artifacts[*]
http://<jenkins-host>/job/<job-name>/lastSuccessfulBuild/api/xml?tree=artifacts[*]and parse the returned text. Here's an example of how to download & parse JSON.
In case of JSON, the data looks like this:
{ "artifacts": [ { "displayPath": null, "fileName": "myapp-1.2-install.exe", "relativePath": "path/to/file/myapp-1.2-install.exe" }, { "displayPath": null, "fileName": "myapp-1.2-install.zip", "relativePath": "path/to/file/myapp-1.2-install.zip" }, ... ] }
The relativePath of an artifact is the file path relative to the Jenkins server root. So the full URL is host name + relativePath.
If there's just one artifact, simply grab artifacts[0].relativePath. If there are multiple artifacts (e.g. .exe, .zip, .tar.gz, etc.), you'll need to loop through them to find the one you need, for example, by a partial file name. Searching XML may be easier than JSON because you can use XPath queries.
- Colin_McCrae8 years agoCommunity Hero
Python can import JSON and then read things out of the resultant Python object the JSON blob creates.
It's not directly parsing the JSON from the file. And I have no idea how it compares to searching XML objects with xpath, but it can be done.
http://stackoverflow.com/questions/7771011/parse-json-in-python
Just thought I'd add it in. :)
Related Content
- 5 years agokaiiii
Recent Discussions
- 4 hours agosimonaferrara
- 13 hours agoSubhraDas
- 2 days agotramuntana