问题
Suppose that I have a (fictional) set of projects: FOO and BAR. Both of these projects have some sort of multi-configuration option.
FOO has a matrix on axis X
which takes values in { x1, ..., xn }
(so there are n builds of FOO). BAR has a matrix on axis Y
which takes values in { y1, ..., ym }
(so there are m builds of BAR).
However, BAR needs to copy some artifacts from FOO. It turns out that Y
is a strictly finer partition than n. For example, X
might take the values { WINDOWS, LINUX }
and Y
might be { WINDOWS_XP, WINDOWS_7, DEBIAN_TESTING, FEDORA }
or whatever.
Is it possible to get BAR to do some sort of table lookup to work out what configuration of FOO it needs when it copies artifacts across? I can easily write a shell script to spit out the mapping, but I can't work out how to invoke it when Jenkins is working out what it needs to copy.
At the moment, a hacky solution is to have two axes on FOO, on for X
and one for Y
, and then filter out combinations that don't make sense. But the resulting combination filter is ridiculous and the matrix is very sparse. Yuck.
A solution that I don't like is to parametrise FOO on Y
instead: this would be a huge waste of compile time. And, worse, the generated artefacts are pretty big, so even if you did some sort of caching, you'd still have to keep unnecessary copies floating around.
回答1:
Can't say I fully understand the intricacies if your matrices, but I think I can help you with your actual question
"I can easily write a shell script to spit out the mapping, but I can't work out how to invoke it when Jenkins is working out what it needs to copy"
The Archive the artifacts and Copy artifacts from another project post-build actions can take java style wildcards, like module/dist/**/*.zip
as well as environment variables/parameters, like ${PARAM}
for the list or artifacts. You can use commas ,
to add more artifacts.
The on-page help for Copy artifacts from another project states how to copy artifacts of a specific matrix configuration: To copy from a particular configuration, enter JOBNAME/AXIS=VALUE
, this is for the Project Name
attribute. That project name
attribute can also contain params as ${PARAM}
So, in your BAR job, have a Copy Artifacts build step, with Project Name
being FOO/X=${mymapping}
. What this will do is: every time a configuration of BAR
is run, it will copy artifacts only from FOO
with configuration of X=${mymapping}
.
Now you need to set the value of ${mymapping}
dynamically every time BAR
is run. A simple script like this may do the trick:
[[ ${Y:0:7} == "WINDOWS" ]] && mymapping=WINDOWS || mymapping=LINUX
Finally, you need to use EnvInject plugin to make this variable available to the rest of the build steps, including the Copy Artifacts step.
So, every time BAR
configuration runs, it will look at its own configuration axis Y
, and if that axis starts with WINDOWS
, it will set the ${mymapping}
to WINDOWS
, else set it to LINUX
. This ${mymapping}
is then made available to the rest of the build steps. When Copy Artifacts
build step is executed, it will only copy artifacts from FOO
where the X
axis matches ${mymapping}
(i.e. either WINDOWS
or LINUX
).
Full Setup
- Install EnvInject plugin.
- In BAR job configuration, tick
Prepare an environment for the run
(part of EnvInject plugin). - Make sure both checkboxes for keeping existing variables are checked.
- In
Script Content
copy your script:[[ ${Y:0:7} == "WINDOWS" ]] && mymapping=WINDOWS || mymapping=LINUX
- Under Build steps, configure Copy Artifacts build step.
- Set Project name parameter to
FOO/X=${mymapping}
- Configure the rest as usual.
来源:https://stackoverflow.com/questions/22558326/being-clever-when-copying-artifacts-with-jenkins-and-multi-configurations