Recently, I had to download files from a Jenkins server using shell scripts. Since Jenkins offers permanent urls to the latest stable artifacts along with a nice XML/JSON API, this can be done pretty easily and automated.

Lets assume that your Jenkins server url is http://yourjenkins:8080

The details of the last successful build of your appjob can be viewed at http://yourjenkins:8080/job/appjob/lastSuccessfulBuild/

If you visit http://yourjenkins:8080/job/appjob/lastSuccessfulBuild/api/  you can view the details about accessing the details via XML or JSON.

To view the details in XML, visit http://yourjenkins:8080/job/appjob/lastSuccessfulBuild/api/xml

This url provides all job info in a handy XML format. We are interested in the <relativePath>  nodes in the XML document as this provides the path to the artifacts that we are interested in.

We can use the tree  query parameter to filter this information. Change the url to http://yourjenkins:8080/job/appjob/lastSuccessfulBuild/api/xml?tree=artifacts[relativePath]  and you will see that the document now contains relativePaths only. If you change the parameter to tree=artifacts[*] , you will see all artifact information i.e fileName and the relativePath. For our example, we will focus on relativePath only.

Using wget  we can download this XML document and pipe it to xpath  to extract the relativePath information.

You can use curl  instead of wget  and xmllint  or any other tools instead of xpath  too.


This provides the relativePath node information as below:

The -- NODE --  information can be stripped out using sed  as below:

The output becomes

The response line in the beginning can be removed by the tail  command

The output becomes

And finally, the files can be downloaded using a loop

 

 

2 thoughts on “Download artifacts from Jenkins using Shell script

    1. Thanks 🙂
      I should refine the xpath expression, tried xmllint, but the version in server did not have the xpath option.

Comments are closed.