Question

Jenkins artifact URLs allow abstracting the "last successful build", so that instead of

http://myjenkins.local/job/MyJob/38/artifact/build/MyJob-v1.0.1.zip

we can say

http://myjenkins.local/job/MyJob/lastSuccessfulBuild/artifact/build/MyJob-v1.0.1.zip

Is it possible to abstract this further? My artifacts have their version number in their filename, which can change from build to build. Ideally I'd like to have a some kind of "alias" URL that looks like this:

http://myjenkins.local/job/MyJob/lastSuccessfulBuild/artifact/build/MyJob-latest.zip

MyJob-latest.zip would then resolve to MyJob-v1.0.1.zip.

If Jenkins itself can't do this, perhaps there's a plugin?

Was it helpful?

Solution

Never seen any such plugin, but Jenkins already has a similar functionality built-in.

You can use /*zip*/filename.zip in your artifact path, where filename is anything you choose. It will take all found artifacts, and download them in a zipfile (you may end up with a zip inside a zip, if your artifact is already a zip file)

In your case, it will be:
http://myjenkins.local/job/MyJob/lastSuccessfulBuild/artifact/build/*zip*/MyJob-latest.zip
This will get you the contents of /artifact/build/ returned in zipped archive with name MyJob-latest.zip. Note that if you have more than just that zip file in that directory, other files will be returned too.

You can use wildcards in the path. A single * for a regular wildcard, a double ** for skipping any number of preceding directories.

For example, to get any file that starts with MyJob, ends with .zip, and to look for it in any artifact directory, you could use:

/lastSuccessfulBuild/artifact/**/MyJob*.zip/*zip*/MyJob-latest.zip

Edit:

You cannot do something like this without some form of a container (a zip in this case). With the container, you are telling the system:

  • Get any possible [undetermined count] wildcard match and place into this container, then give me the container. This is logical and possible, as there is only one single container, whether it is empty or not.

But you cannot tell the system:

  • Give me a link to a specific single file, but I don't know which one or how many there are. The system can't guarantee that your wildcards will match one, more than one, or none. This is simply impossible from a logic perspective.

If you need it for some script automation, you can unzip the first level zip and be still left with your desired zipped artifact.

If you need to provide this link to someone else, you need an alternative solution.

Alternative 1:
After your build is complete, execute a post-build step that will take your artifact, and rename it to MyJob-latest.zip, but you are losing versioning in the filename. You can also chose to copy instead of rename, but you end up with double the space used for storing these artifacts.

Alternative 2 (recommended): As a post-build action, upload the artifact to a central repository. It can be Artifactory, or even plain SVN. When you upload it, it will be renamed MyJob-latest.zip and the previous one would be overwritten. This way you have a static link that will always have the latest artifact from lastSuccessfulBuild

OTHER TIPS

There is actually a plugin to assign aliases to build you've run, and I have found it pretty handy: the Build Alias Setter Plugin.

You can use it for instance to assign an alias in the form of your own version number for a build, instead (or rather in addition) to the internal Jenkins-assigned build number. I found that it is usually most practical to use it in conjunction with the EnvInject plugin (or your favorite variant): you would export an env variable (e.g. MY_VAR=xyz) with a value to the target version or moniker, and then use the form ${ENV,var="myvar"} in the "Token Macro alias" config that the plugin provides in your job config.

You can also use it to assign aliases in the form of "lastSuccesful" if you have such a need, which allows you to distinguish between different types of successful (or other state) builds.

Wait thee's more! You can also use the /*zip*/ trick in conjunction with the alias setter as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top