How to get jenkins to copy artifacts to a dynamic directory?
Asked Answered
B

1

6

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?

Barrel answered 9/3, 2011 at 16:11 Comment(0)
C
4

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.

Capillary answered 10/3, 2011 at 21:55 Comment(5)
Sounds good, I'll try that. I got a similar answer on the jenkins board too. Thank you for your help!Barrel
We're running maven2 builds in Jenkins, so we don't have a script to do all this... i.e. bundle up Jenkins artifacts after maven has run and the build is successful. The job config for Maven2 jobs only allows <includes> syntax for what to archive, but does not let me archive to a network appliance or scp location. Is that possible?Niddering
You can always use the "Execute Shell" before your maven stuff has run, and create a directory named after the $BUILD_NUMBER. You can then use Maven to create your artifact in that directory, or move it once maven is done, again using "Execute Shell". It does not have to be a separate script. Does that make sense, sort of :-\?Capillary
"Execute Shell" is not available for Maven2 jobs. I dug a little deeper and found the "Jenkins M2 Extra Steps Plugin" which gives me the hook I need to do exactly what I want after a successful Maven2 build. wiki.jenkins-ci.org/display/JENKINS/M2+Extra+Steps+PluginNiddering
Oh yeah. Sorry about that. I was thinking you have a normal job with an M2 step. But good find!Capillary

© 2022 - 2024 — McMap. All rights reserved.