Search code examples
bashcurljenkinsdeb

How can I get and store a changing version number from a Jenkins build using a cURL command?


I'm trying to extract the name of a file with the latest version number so I can move it to another directory using bash. I have a directory called "deb-files" that I'd like to move the downloaded .deb file into. This is a basic version of what I'm looking for:

if [ "$REPOSITORY_NAME" = "repository" ];
    then : curl https://example.com/$REPOSITORY_NAME*.deb

    mv $REPOSITORY_NAME*.deb ./deb-files/;

$REPOSITORY_NAME is populated by user input. Please just assume that they input "repository". Note that the file name is the same name as the repository name. I thought putting a star in front of the extension meant "latest version", but every time I run it, I get an error at the mv part of the code which says it cannot find the file - "No such file or directory". Any ideas? I've tried escaping the star and quoting the variable name. No luck.

In case it matters, I'm trying to access artifacts created by a build in Jenkins.


Solution

  • Neither curl nor the Jenkins server understand the shell wildcard '*'. You'll have to find the exact name of the artifact that you want to download.

    One way is to modify the Jenkins job to create (and archive as an artifact) a symbolic link to the artifact with a fixed name (pkg1_latest.deb).

    Another way is to use the Jenkins REST API to get information about the latest build, but to do this generally, you'll need to parse some JSON.

    curl http://jenkins:8080/job/JOB_NAME/lastSuccessfulBuild/api/json?tree=artifacts[*]
    

    Generates something like:

    {
      "artifacts" : [
        {
          "displayPath" : "platformA/pkg1_VERSION.deb",
          "fileName" : "pkg1_VERSION.deb",
          "relativePath" : "build/platformA/pkg1_VERSION.deb"
        },
        {
          "displayPath" : "platformB/pkg1_VERSION.deb",
          "fileName" : "pkg1_VERSION.deb",
          "relativePath" : "build/platformB/pkg1_VERSION.deb"
        },
        ...
      ]
    }
    

    Then parse the output to find the appropriate relative path. With the relative path, you can retrieve the artifact:

    curl http://jenkins:8080/job/JOB_NAME/lastSuccessfulBuild/artifact/$relpath
    

    Update: Yet another solution is to download a zip archive containing all artifacts, and extract the ones you're interested in.

    curl "http://jenkins:8080/job/JOB_NAME/lastSuccessfulBuild/artifact/*zip*/archive.zip
    unzip archive.zip "$REPOSITORY_PATH*.deb"
    

    The special *zip* in the URL tells Jenkins to create a zip archive of everything.