How to get jenkins to copy artifacts to a dynamic directory?

afrosteve picture afrosteve · Mar 9, 2011 · Viewed 17.3k times · Source

I'm trying to get Jenkins to copy the artifacts of a build to an archive directory on another server using the scp plugin.

Ideally, I'd like to be able to have the destination be dynamic based on the build version so the result would like something like /builds/<build version>/

For a build version like 1.2.3.4 it would look like:

/builds/1.2.3.4/

From reading the scp plugin page, it doesn't look like this is possible but I figured someone here may have figured it out.

Is there a way to do this?

Is it better to just put the artifacts with the version number embedded in the file name in one directory?

Answer

Sagar picture Sagar · Mar 10, 2011

Like you said, I don't think the scp plugin can do it directly. However, there may be a workaround.

In your build, you have access to the build number using $BUILD_NUMBER (or %BUILD_NUMBER%, as the case may be -> Linux vs Windows).

In any case, as part of your script, you could create a directory with $BUILD_NUMBER as the name, so:

mkdir -p $BUILD_NUMBER

-or-

md %BUILD_NUMBER%

So, for example, a new directory would be /path/to/workspace/1.2.3.4

Once your build is complete, at the end of your script, create the above directory, move your artifact into it, and tar/zip the directory up.

Use this tar/zip file as your job's artifact.

Use the scp plugin to transfer this artifact to your destination machine, and untar/unzip it there (let's say at /path/to/artifact/directory)

What you will have then, is /path/to/artifact/directory/1.2.3.4.

For the next build, let's say 1.2.3.5, you will create a new directory (named 1.2.3.5), move your artifact into it at the end of the build, zip it up and transfer it. When you unzip it at your destination, you'll have a new directory /path/to/artifact/directory/1.2.3.5 with the new build's artifact in it.

I know it sounds confusing, but is actually quite easy to implement.